DC motor control using ESP32

This project showing how to control the DC motor by using an ESP32 development board with Arduino IDE. Here using a motor driver L293D. This module allows us to control the speed and direction of the motors. The PWM signals controlling the speed of DC motor.

In general, the speed of a DC motor is directly proportional to the supplied voltage. So if we reducing the supplied voltage from 9V to 4.5V similarly the speed of DC motor becomes half. But in practical, changing the supplied voltage is not easy.So here we giving the input from a speed controller PWM. The speed controller PWM varying the average voltage supplied to the motor. The PWM controller accepts the control signal and adjusts the duty cycle. The duty cycle is directly proportional to the speed of the motor.

The following waveform shows the PWM signals in different duty cycles.

pwm_wave

Components Required

  • ESP32 Development board
  • DC motor
  • L293D module
  • 100Ω resistor
  • Battery
  • Breadboard wires
  • Breadboard
  • 2x jumper wire

L293D Motor Driver

The L293D motor driver consists of an H bridge circuit.T he H bridge circuit is for controlling low current rated motor. The L293D IC has 16 pins and 8 pins on each side for simultaneously control two DC motors. There are four input pins, four output pins and two enable pins for each motor.

L293D_pin
The pin characteristics of L293D IC is given below

Pin 1 – Enable pin for 1 or 2.When it is high the motor connected with pin 3 and 6 will rotate.
Pin 2- Input 1, when this pin is HIGH the current will flow through output 1.
Pin 3 – Output 1, this pin is connected with one terminal of the motor.
Pin 4/5 – GND pins
Pin 6 – Output 2, this pin is connected with one terminal of the motor.
Pin 7 – Input 2, when this pin is HIGH the current will flow through output 2.
Pin 8 – VSS, this pin is used to give power supply to connected motors from 5V to 36V maximum depends on Motor connected.
Pin 9 – Enable pin for 3 or 4.When it is high the motor connected with pin 11 and 14 will rotate.
Pin 10 –Input 4, when this pin is HIGH the current will flow through output 4.
Pin 11 – Output 4, this pin is connected with one terminal of the motor.
Pin 12/13 – GND pins
Pin 14 – Output 3, this pin is connected with one terminal of the motor.
Pin 15 – Input 3, when this pin is HIGH the current will flow through output 3.
Pin 16 – VCC, for supply power to IC i.e 5V.

The DC motor requires a high power to work. So the motor should be powered using an external power source of (6V to 12V)

Circuit Diagram

DC motor

  • Setup the ESP32 board in Arduino IDE

If you want to learn more about the ES P32 development board visit the official website of esp32:
There is an add-on for the Arduino IDE that allows you to program the ESP32 using the Arduino IDE and its programming language. Follow this tutorial to prepare your Arduino IDE:

Code Snippet

/*********
  Rui Santos
  Complete project details at http://randomnerdtutorials.com  
*********/

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

// Setting PWM properties
const int freq = 30000;
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 200;

void setup() {
  // sets the pins as outputs:
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enable1Pin, OUTPUT);
  
  // configure LED PWM functionalitites
  ledcSetup(pwmChannel, freq, resolution);
  
  // attach the channel to the GPIO to be controlled
  ledcAttachPin(enable1Pin, pwmChannel);

  Serial.begin(115200);

  // testing
  Serial.print("Testing DC Motor...");
}

void loop() {
  // Move the DC motor forward at maximum speed
  Serial.println("Moving Forward");
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH); 
  delay(2000);

  // Stop the DC motor
  Serial.println("Motor stopped");
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  delay(1000);

  // Move DC motor backwards at maximum speed
  Serial.println("Moving Backwards");
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW); 
  delay(2000);

  // Stop the DC motor
  Serial.println("Motor stopped");
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  delay(1000);

  // Move DC motor forward with increasing speed
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  while (dutyCycle <= 255){
    ledcWrite(pwmChannel, dutyCycle);   
    Serial.print("Forward with duty cycle: ");
    Serial.println(dutyCycle);
    dutyCycle = dutyCycle + 5;
    delay(500);
  }
  dutyCycle = 200;
}