Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP32 and L298N-Based Wi-Fi Controlled Robotic Car

View and Edit Project In Cirkit DesignerImage of ESP32 and L298N-Based Wi-Fi Controlled Robotic Car

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

  1. 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
  2. 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
  3. Motor and wheels

    • Description: A DC motor with attached wheels for driving the robotic car.
    • Pins: vcc, GND
  4. Battery 12V

    • Description: A 12V battery used to power the circuit.
    • Pins: +, -

Wiring Details

ESP32 (30 pin)

L298N DC motor driver (First Instance)

L298N DC motor driver (Second Instance)

Motor and wheels

Battery 12V

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