Chapter 17 Motor & Driver

In this chapter, we will learn about DC motors and DC motor drivers and how to control the speed and direction of a DC motor.

Project 17.2 Control Motor with Potentiometer

Control the direction and speed of the motor with a potentiometer.

ESP32-WROVER x1

Chapter01_00

GPIO Extension Board x1

Chapter01_01

Breadboard x1

Chapter01_02

Motor x1

Chapter17_02

9V battery (prepared by yourself) & battery line

Chapter17_03

Jumper M/M

Chapter01_05

L293D

Chapter17_00

Rotary potentiometer x1

Chapter09_00

Component knowledge

L293D

L293D is an IC chip (Integrated Circuit Chip) with a 4-channel motor drive. You can drive a unidirectional DC motor with 4 ports or a bi-directional DC motor with 2 ports or a stepper motor (stepper motors are covered later in this Tutorial).

../../../_images/Chapter17_01.png

Port description of L293D module is as follows:

Pin name

Pin number

Description

In x

2, 7, 10, 15

Channel x digital signal input pin

Out x

3, 6, 11, 14

Channel x output pin, input high or low level according to In x pin, get connected to +Vmotor or 0V

Enable1

1

Channel 1 and channel 2 enable pin, high level enable

Enable2

9

Channel 3 and channel 4 enable pin, high level enable

0V

4, 5, 12, 13

Power cathode (GND)

+V

16

Positive electrode (VCC) of power supply, supply voltage 3.0~36V

+Vmotor

8

Positive electrode of load power supply, provide power supply for the Out

pin x, the supply voltage is +V~36V

For more detail, please refer to the datasheet for this IC Chip.

When using L293D to drive DC motor, there are usually two connection options.

The following connection option uses one channel of the L239D, which can control motor speed through the PWM, However the motor then can only rotate in one direction.

../../../_images/Chapter17_021.png

The following connection uses two channels of the L239D: one channel outputs the PWM wave, and the other channel connects to GND, therefore you can control the speed of the motor. When these two channel signals are exchanged, not only controls the speed of motor, but also can control the steering of the motor.

../../../_images/Chapter17_031.png

In practical use the motor is usually connected to channel 1 and 2 by outputting different levels to in1 and in2 to control the rotational direction of the motor, and output to the PWM wave to Enable1 port to control the motor’s rotational speed. If the motor is connected to channel 3 and 4 by outputting different levels to in3 and in4 to control the motor’s rotation direction, and output to the PWM wave to Enable2 pin to control the motor’s rotational speed.

Circuit

Use caution when connecting this circuit, because the DC motor is a high-power component, do not use the power provided by the ESP32 to power the motor directly, which may cause permanent damage to your ESP32! The logic circuit can be powered by the ESP32 power or an external power supply, which should share a common ground with ESP32.

Schematic diagram

Chapter17_04

Hardware connection

If you need any support, please feel free to contact us via: support@freenove.com

Chapter17_05

Note

he motor circuit uses A large current, about 0.2-0.3A without load.We recommend that you use a 9V battery to power the extension board.

Sketch

Sketch_17.2_Control_Motor_by_L293D

../../../_images/Chapter17_06.png

Download code to ESP32-WROVER, rotate the potentiometer in one direction and the motor speeds up slowly in one direction. And then rotate the potentiometer in the other direction and the motor will slow down to stop. And then rotate it in an inverse direction to accelerate the motor.

../../../_images/Chapter17_07.png ../../../_images/Chapter17_08.png

The following is the sketch:

 1/**********************************************************************
 2  Filename    : Control_Motor_by_L293D
 3  Description : Use PWM to control the direction and speed of the motor.
 4  Auther      : www.freenove.com
 5  Modification: 2024/06/19
 6**********************************************************************/
 7int in1Pin = 12;      // Define L293D channel 1 pin
 8int in2Pin = 14;      // Define L293D channel 2 pin
 9int enable1Pin = 13;  // Define L293D enable 1 pin
10int channel = 0;
11
12boolean rotationDir;  // Define a variable to save the motor's rotation direction
13int rotationSpeed;    // Define a variable to save the motor rotation speed
14
15void setup() {
16  // Initialize the pin into an output mode:
17  pinMode(in1Pin, OUTPUT);
18  pinMode(in2Pin, OUTPUT);
19  ledcAttachChannel(enable1Pin,1000,11,channel);//Set PWM to 11 bits, range is 0-2047
20}
21
22void loop() {
23  int potenVal = analogRead(A0);      // Convert the voltage of rotary potentiometer into digital
24  //Compare the number with value 2048, 
25  //if more than 2048, clockwise rotates, otherwise, counter clockwise rotates
26  rotationSpeed = potenVal - 2048;
27  if (potenVal > 2048)
28    rotationDir = true;
29  else
30    rotationDir = false;
31  // Calculate the motor speed
32  rotationSpeed = abs(potenVal - 2048);
33  //Control the steering and speed of the motor
34  driveMotor(rotationDir, constrain(rotationSpeed,0,2048));
35}
36
37void driveMotor(boolean dir, int spd) {
38  // Control motor rotation direction
39  if (dir) {
40    digitalWrite(in1Pin, HIGH);
41    digitalWrite(in2Pin, LOW);
42  }
43  else {
44    digitalWrite(in1Pin, LOW);
45    digitalWrite(in2Pin, HIGH);
46  }
47  // Control motor rotation speed
48  ledcWrite(enable1Pin, spd);
49}

The ADC of ESP32 has a 12-bit accuracy, corresponding to a range from 0 to 4095. In this program, set the number 2048 as the midpoint. If the value of ADC is less than 2048, make the motor rotate in one direction. If the value of ADC is greater than 2048, make the motor rotate in the other direction. Subtract 2048 from the ADC value and take the absolute value and use this result as the speed of the motor.

 1  int potenVal = analogRead(A0);      // Convert the voltage of rotary potentiometer into digital
 2  //Compare the number with value 2048, 
 3  //if more than 2048, clockwise rotates, otherwise, counter clockwise rotates
 4  rotationSpeed = potenVal - 2048;
 5  if (potenVal > 2048)
 6    rotationDir = true;
 7  else
 8    rotationDir = false;
 9  // Calculate the motor speed
10  rotationSpeed = abs(potenVal - 2048);
11  //Control the steering and speed of the motor
12  driveMotor(rotationDir, constrain(rotationSpeed,0,2048));
13}

Set the accuracy of the PWM to 11 bits and range from 0 to 2047 to control the rotation speed of the motor.

1ledcAttachChannel(enable1Pin,1000,11,channel);//Set PWM to 11 bits, range is 0-2047

Function driveMotor is used to control the rotation direction and speed of the motor. The dir represents direction while spd refers to speed.

 1void driveMotor(boolean dir, int spd) {
 2  // Control motor rotation direction
 3  if (dir) {
 4    digitalWrite(in1Pin, HIGH);
 5    digitalWrite(in2Pin, LOW);
 6  }
 7  else {
 8    digitalWrite(in1Pin, LOW);
 9    digitalWrite(in2Pin, HIGH);
10  }
11  // Control motor rotation speed
12  ledcWrite(enable1Pin, spd);
13}