Circuit Documentation
Summary
This circuit is designed to control a robotic car using an ESP32 microcontroller and two L298N DC motor drivers. The car is powered by a 12V battery and is capable of moving in various directions based on input from a PS4 controller. The ESP32 receives commands from the PS4 controller and translates them into motor control signals to drive the motors connected to the L298N motor drivers.
Component List
ESP32 (30 pin)
- Description: A powerful microcontroller with built-in Wi-Fi and Bluetooth capabilities.
- Pins: EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, GND, Vin, D23, D22, TX0, RX0, D21, D19, D18, D5, TX2, RX2, D4, D2, D15, 3V3
L298N DC motor driver
- Description: A dual H-bridge motor driver that allows control of two DC motors.
- Pins: OUT1, OUT2, 12V, GND, 5V, OUT3, OUT4, 5V-ENA-JMP-I, 5V-ENA-JMP-O, +5V-J1, +5V-J2, ENA, IN1, IN2, IN3, IN4, ENB
Motor and wheels
- Description: A DC motor with attached wheels for driving the robotic car.
- Pins: vcc, GND
Battery 12V
- Description: A 12V battery used to power the circuit.
- Pins: +, -
Wiring Details
ESP32 (30 pin)
- D32: Connected to ENA of L298N DC motor driver
- D33: Connected to IN1 of L298N DC motor driver
- D25: Connected to IN2 of L298N DC motor driver
- D26: Connected to IN3 of L298N DC motor driver
- D27: Connected to IN4 of L298N DC motor driver
- D14: Connected to ENB of L298N DC motor driver
- GND: Connected to GND of L298N DC motor driver and battery 12V
- Vin: Connected to 5V of L298N DC motor driver
- D23: Connected to ENB of L298N DC motor driver
- D22: Connected to ENA of L298N DC motor driver
- D21: Connected to IN2 of L298N DC motor driver
- D19: Connected to IN4 of L298N DC motor driver
- D18: Connected to IN3 of L298N DC motor driver
- D5: Connected to IN1 of L298N DC motor driver
L298N DC motor driver (First Instance)
- ENA: Connected to D32 of ESP32
- IN1: Connected to D33 of ESP32
- IN2: Connected to D25 of ESP32
- IN3: Connected to D26 of ESP32
- IN4: Connected to D27 of ESP32
- ENB: Connected to D14 of ESP32
- GND: Connected to GND of ESP32 and battery 12V
- 5V: Connected to Vin of ESP32
- OUT1: Connected to GND of motor and wheels
- OUT2: Connected to vcc of motor and wheels
- 12V: Connected to 12V of battery 12V
- OUT3: Connected to vcc of motor and wheels
- OUT4: Connected to GND of motor and wheels
L298N DC motor driver (Second Instance)
- ENA: Connected to D22 of ESP32
- IN1: Connected to D5 of ESP32
- IN2: Connected to D21 of ESP32
- IN3: Connected to D18 of ESP32
- IN4: Connected to D19 of ESP32
- ENB: Connected to D23 of ESP32
- GND: Connected to GND of ESP32 and battery 12V
- OUT1: Connected to vcc of motor and wheels
- OUT2: Connected to GND of motor and wheels
- 12V: Connected to 12V of battery 12V
- OUT3: Connected to GND of motor and wheels
- OUT4: Connected to vcc of motor and wheels
Motor and wheels
vcc: Connected to OUT2 of L298N DC motor driver (First Instance)
GND: Connected to OUT1 of L298N DC motor driver (First Instance)
vcc: Connected to OUT3 of L298N DC motor driver (First Instance)
GND: Connected to OUT4 of L298N DC motor driver (First Instance)
vcc: Connected to OUT1 of L298N DC motor driver (Second Instance)
GND: Connected to OUT2 of L298N DC motor driver (Second Instance)
vcc: Connected to OUT4 of L298N DC motor driver (Second Instance)
GND: Connected to OUT3 of L298N DC motor driver (Second Instance)
Battery 12V
- +: Connected to 12V of L298N DC motor driver (First Instance) and L298N DC motor driver (Second Instance)
- -: Connected to GND of L298N DC motor driver (First Instance), L298N DC motor driver (Second Instance), and ESP32
Code Documentation
#include <PS4Controller.h>
#define FORWARD 1
#define BACKWARD 2
#define LEFT 3
#define RIGHT 4
#define FORWARD_LEFT 5
#define FORWARD_RIGHT 6
#define BACKWARD_LEFT 7
#define BACKWARD_RIGHT 8
#define TURN_LEFT 9
#define TURN_RIGHT 10
#define STOP 0
#define BACK_RIGHT_MOTOR 0
#define BACK_LEFT_MOTOR 1
#define FRONT_RIGHT_MOTOR 2
#define FRONT_LEFT_MOTOR 3
struct MOTOR_PINS {
int pinIN1;
int pinIN2;
int pinEn;
int pwmSpeedChannel;
};
std::vector<MOTOR_PINS> motorPins = {
{16, 17, 22, 4}, // BACK_RIGHT_MOTOR
{18, 19, 23, 5}, // BACK_LEFT_MOTOR
{26, 27, 14, 6}, // FRONT_RIGHT_MOTOR
{33, 25, 32, 7}, // FRONT_LEFT_MOTOR
};
#define MAX_MOTOR_SPEED 200
const int PWMFreq = 1000; /* 1 KHz */
const int PWMResolution = 8;
#define SIGNAL_TIMEOUT 1000 // This is signal timeout in milliseconds. We will reset the data if no signal
unsigned long lastRecvTime = 0;
// Callback function that will be executed when data is received
void notify() {
String inputData;
inputData = inputData + "values " + PS4.RStickX() + " " + PS4.RStickY() + " " + PS4.LStickX();
Serial.println(inputData);
if (PS4.RStickX() < -75 && PS4.RStickY() > 75) {
processCarMovement(FORWARD_LEFT);
} else if (PS4.RStickX() > 75 && PS4.RStickY() > 75) {
processCarMovement(FORWARD_RIGHT);
} else if (PS4.RStickX() < -75 && PS4.RStickY() < -75) {
processCarMovement(BACKWARD_LEFT);
} else if (PS4.RStickX() > 75 && PS4.RStickY() < -75) {
processCarMovement(BACKWARD_RIGHT);
} else if (PS4.LStickX() > 75) {
processCarMovement(TURN_RIGHT);
} else if (PS4.LStickX() < -75) {
processCarMovement(TURN_LEFT);
} else if (PS4.RStickY() > 75) {
processCarMovement(FORWARD);
} else if (PS4.RStickY() < -75) {
processCarMovement(BACKWARD);
} else if (PS4.RStickX() > 75) {
processCarMovement(RIGHT);
} else if (PS4.RStickX() < -75) {
processCarMovement(LEFT);
} else {
processCarMovement(STOP);
}
lastRecvTime = millis();
}
void onConnect() {
Serial.println("Connected!.");
}
void onDisConnect() {
processCarMovement(STOP);
Serial.println("Disconnected!.");
}
void processCarMovement(int inputValue) {
switch (inputValue) {
case FORWARD:
rotateMotor(FRONT
