Bluetooth RC Car with ESP32

Talha Khan
4 min readAug 20, 2022

--

So far we have covered how to program the ESP32 with the Arduino IDE here, how to use the Bluetooth Classic function of the board here, and finally how to control a DC motor via our smartphone using the Bluetooth feature here. Now, in this tutorial, we will bring all of that knowledge together to design a Bluetooth controlled remote controlled car with the ESP32. The Bluetooth RC car is a fun project for students and hobbyists to hone their skills and put their knowledge of microprocessors as well as the ESP32 to good use.

Required Components

  1. ESP32
  2. 9V-12V Battery
  3. Car Chassis
  4. Smartphone
  5. Breadboard
  6. L293D Motor Driver IC
  7. 7805 Voltage Regulator IC
  8. Jumper Wires

These components can easily be bought from any online electronics store. One thing to note is that the Car chassis that you buy should suitably be the double tyred one with a third 360 degrees turning wheel at the front center, this would make it easier for the motor to run and turn the car without any excessive load or friction.

Circuit

In this project, the ESP32 will be powered using the VIN pin on the board, for which we need a 5V supply. This can be done by using the 7805 voltage regulator IC with the battery. Note that in order to power the board from the VIN pin, we also need to attach one of the two GND pins to ground. Also, we need the same supply to feed into the VCC1 of the motor driver IC while the VCC2 is to be supplied directly from the battery.

Also, we will need to utilize both the channels of the L293D IC because of the presence of two DC motors. Finally, we will not be using separate enable pins to enable the IC channels, instead we will connect them to the node to which the VCC1 is connected.

In this circuit, we will attach the motor pins from the ESP32 at pins 2,7,10, and 15 of the L293D.

Code

#include <BluetoothSerial.h>

BluetoothSerial SerialBT;
byte command;

// Motor A
int motor1Pin1 = 27;
int motor1Pin2 = 26;

// Motor B
int motor2Pin1=32;
int motor2Pin2=33;

/* Check if Bluetooth configurations are enabled in the SDK */
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

void setup()
{
//Motor1 Pins
pinMode(motor1Pin1,OUTPUT);
pinMode(motor1Pin2,OUTPUT);

//Motor2 Pins
pinMode(motor2Pin1,OUTPUT);
pinMode(motor2Pin2,OUTPUT);

Serial.begin(115200);
SerialBT.begin("ESP32");
Serial.println("Bluetooth Started! Ready to pair...");
Serial.print("Testing DC Motor...");
}

void loop()
{
if(SerialBT.available())
{
command = SerialBT.read();
Serial.write(command);
}
// Going Forward
if(command=='1')
{
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);

}
//Going Backward
if(command=='2')
{
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);

}
//Turning Left
if(command=='3')
{
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);

}
//Turning Left
if(command=='4')
{
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);

}
//Stopping
if(command=='0')
{
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);

}

delay(20);

}

In this code, we first include the Bluetooth library

#include <BluetoothSerial.h>

Afterwards, we define the pins to provide the input and direction control for both the motors

// Motor A
int motor1Pin1 = 27;
int motor1Pin2 = 26;

// Motor B
int motor2Pin1=32;
int motor2Pin2=33;

Since we will be using Bluetooth communication to control the car, we will check to see if the Bluetooth feature is working properly

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

In the void setup() block, we will define our motor pins as OUTPUT while also initializing the serial communication and Bluetooth

  pinMode(motor1Pin1,OUTPUT);
pinMode(motor1Pin2,OUTPUT);

//Motor2 Pins
pinMode(motor2Pin1,OUTPUT);
pinMode(motor2Pin2,OUTPUT);

Serial.begin(115200);
SerialBT.begin("ESP32");

Moving on to the void loop() block, we will write the code to check if we have any data being received through Bluetooth and store that data in a variable

if(SerialBT.available())
{
command = SerialBT.read();
Serial.write(command);
}

Finally, we will check the received byte to see if it satisfies any of our conditions for the RC car motion.

Concluding Thoughts

In this project, one can get confused with regards to the motor pins and also the connections of the DC motors of the car chassis. In such a scenario wherein a motor turns in a direction opposite to the expected, you can always fix the direction of the DC motor by interchanging either the output wires of the motor or the input motor pins from the ESP32.

At the end, your car should look something like this

P.S, for any paid content writing and/or article writing tasks, and for any free guidance about the ESP32 board, do reach out to me at khantalha7367@gmail.com

--

--

Talha Khan

An electrical engineering student and tech enthusiast working to spread ideas and innovation