Interfacing the ESP32 with the L293D Motor Driver IC

Talha Khan
5 min readAug 17, 2022

--

Interfacing the ESP32 with the L293D Motor Driver IC

Driving a DC motor with a microcontroller board is a simple operation which requires only a few components. For our project, we are gonna be using the ESP32 DEVKIT V1 development board along with a motor driver IC. If you want to learn about how to program the ESP32 using the Arduino IDE, then I suggest reading this article. Also, if you want to learn about the in-built Bluetooth functionality of the board, the read this piece. Since both these will be needed for the project, one should have a basic know how of both topics previously covered. In this project, we will be controlling a DC motor, driven by the L293D IC, by commands sent from a smartphone via Bluetooth

L293D Motor Driver IC

The L293D IC is a high current, high voltage four channel driver. It has applications as a DC motor driver, Stepper motor driver, and Latching relay driver. It has a total of 16 pins consisting of logic gates capable of driving two DC motors simultaneously. We have a Vcc1 pin for the 5V for the logic gates while a Vcc2 pin with a voltage range from 4.5V-36V is used to provide power to the motors being driven. The IC also has four ground and heat sink pins attached to it. An image of the pinout of L293D is given as follows

To get much more in-depth information about the L293D motor driver IC, I suggest reading the datasheet of the device {here}

Tools

  1. ESP32
  2. DC Motor
  3. Jumper Wires
  4. L293D Motor Driver IC
  5. Breadboard
  6. 5V supply

Circuit

For this project, we will apply 5V to both the Vcc1 and Vcc2 pins. Now the ESP32 does not provide a 5V power supply pin, so to provide this voltage I will personally use my STM32F103C8T6 Bluepill board. You can use an Arduino or even a 9V battery to power up the Vcc2 and can use a voltage divider to provide the 5V to the Vcc1. Also, we will also use a single motor so only pin 1–7 will be utilised.

In here, we have a common connection for both Vcc while pins 1,2, and 7 will be the enable and driving pins respectively

Code

#include <BluetoothSerial.h>

BluetoothSerial SerialBT;
byte command;

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

/* 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);
pinMode(enable1Pin,OUTPUT);
digitalWrite(enable1Pin,HIGH);

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

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

}
//To change direction of motor
if(command=='2')
{
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);

}

//Stopping the motor
if(command=='0')
{
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);

}

delay(20);

}

For the line by line explanation of this code

Since it is a Bluetooth controlled motor, we will include the Bluetooth library

#include <BluetoothSerial.h>

Next up we will define the variables to store numbers of the pins to be used as output for the motor driver, and also a variable to store any incoming serial data

byte command;

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

Afterwards, we check if our Bluetooth is configured properly.

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

Now we include the necessary code in the void setup() block. We will configure the pins as OUTPUT so that they can act as INPUT for the L293D pins.

  pinMode(motor1Pin1,OUTPUT);
pinMode(motor1Pin2,OUTPUT);
pinMode(enable1Pin,OUTPUT);
digitalWrite(enable1Pin,HIGH);

We will also configure the Serial rate along with initializing the Bluetooth function.

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

Now we will write the necessary code for the void loop() block. First we will check if we have any incoming serial data from the paired device. We will store that data in the variable named “command”.

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

Afterwards, we will check the received data and see if it satisfies any of our conditions for the motor.

// Going in one direction
if(command=='1')
{
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);

}
//To change direction of motor
if(command=='2')
{
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);

}

//Stopping the motor
if(command=='0')
{
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);

}

Any data which does not satisfy any of the above conditions will simply have no effect on the motor.

Results

The final circuit for this project turned out as follows

After Bluetooth connection is established, sending the relevant command makes the motor rotate as seen.

You can also make the motor rotate in the opposite direction by sending relevant command.

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