Back to Tutorial

MotorBot with Esp32


  •  09-06-2025

🔧 Components Needed:

ComponentQuantity
ESP32 Dev Board1
L298N Motor Driver Module1
DC Gear Motors (TT or BO motors)2
3 x Li-ion 18650 Cells (3.7V each)1 battery pack (11.1V total)
Battery Holder or 3S Battery Pack1
Breadboard (optional, for testing)1
Jumper Wires (Male-to-Male/Female)~15-20
Robot Chassis + Wheels1 set
Switch (optional, for power control)1

🔌 Connections:

✅ Power Supply:

  • Connect battery pack positive to +12V terminal on L298N.
  • Connect battery pack negative to GND on L298N.
  • Remove the jumper on L298N between +12V and 5V (important! Use external 5V for ESP32).
  • Use buck converter (recommended) to step down 11.1V to 5V and power ESP32 through 5V and GND.

✅ L298N to Motors:

  • Connect:
    • Motor A terminals (OUT1 & OUT2) to Left motor.
    • Motor B terminals (OUT3 & OUT4) to Right motor.

✅ L298N to ESP32 (Control Pins):

ESP32 GPIOL298N IN Pin
GPIO 16IN1
GPIO 17IN2
GPIO 18IN3
GPIO 19IN4

HOW TO OPERATE BOT

#include <BluetoothSerial.h>
#include <L298NX2.h>
#define LED_BUILTIN 2
//-------------L298---------------------------------------------------

// motor 1 settings
#define ENA 4    // this pin must be PWM enabled pin if Arduino board is used
#define IN_1 16  //rx2
#define IN_2 17  //tx2
// motor 2 settings
#define ENB 5    // this pin must be PWM enabled pin if Arduino board is used
#define IN_3 18  //d18
#define IN_4 19  //d19


//L298NX2 mybot(IN_1,IN_2,IN_3,IN_4); //for full speed
L298NX2 mybot(ENA, IN_1, IN_2, ENB, IN_3, IN_4); //for speed
//---------BT--------------------
BluetoothSerial EspBT;

//------Bluetooth RC Controller Define ----

#define back_light 21   //D21
#define front_light 22  //D22
#define horn_Buzz 26    //D26

char bdata; //for bluetooth command store

void setup() {
  Serial.begin(9600);
  Serial.println(F("init"));
  EspBT.begin("BT_MSKCAR2025_01");
  //custom pins for light and buzzer
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(back_light, OUTPUT);
  pinMode(front_light, OUTPUT);
  pinMode(horn_Buzz, OUTPUT);

  //testing
  mybot.setSpeed(100);
  mybot.forward();
  delay(100);
  mybot.stop();
  //test done
}
//-----------------------------------------
void loop() {
  while (EspBT.available() > 0) {
    bdata = EspBT.read();

    Serial.println(bdata);
//make condition based on BLE data
    switch (bdata) {
      case 'F':
        Serial.println("Forward");
        mybot.forward();
        break;
      case 'B':
        Serial.println("Reverse");
        mybot.backward();
        break;
      case 'L':
        Serial.println("Left");
        mybot.forwardA();
        mybot.backwardB();
        break;
      case 'R':
        Serial.println("Right");
        mybot.forwardB();
        mybot.backwardA();
        break;
      case 'S':
        Serial.println("Stop");
          mybot.stop();
        break;
      case '0':
        Serial.println("Stop");
        mybot.stop();
        break;
      case '3':
        Serial.println("LOW speed");
        mybot.setSpeed(100);
        break;
      case '5':
        Serial.println("Mid speed");
        mybot.setSpeed(150);
        break;
      case '9':
        Serial.println("Mid speed");
        mybot.setSpeed(255);
        break;
    }
  }
}
iotwebplanet.com - 1

Share this post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Tutorial