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.1 Control Motor with Potentiometer

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

Component List

ESP8266 x1

Chapter01_00

USB cable

Chapter01_01

Breadboard x1

Chapter17_00

Breadboard Power module x1

Chapter17_01

Jumper wire M/M x12

Chapter17_02

Rotary potentiometer x1

Chapter17_04

Motor x1

Chapter17_05

L293D

Chapter17_06

9V battery (prepared by yourself) & battery line

Chapter17_03

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_07.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_08.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_09.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 ESP8266 to power the motor directly, as this may cause permanent damage to your RPi! The logic circuit can be powered by the ESP8266’s power or an external power supply, which should share a common ground with ESP8266.

Schematic diagram

Chapter17_10

Hardware connection.

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

Chapter17_11

Sketch

Sketch_17.1_Control_Motor_by_L293D

../../../_images/Chapter17_12.png

Download code to ESP8266, 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_13.png ../../../_images/Chapter17_14.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: 2022/05/11
 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
10
11boolean rotationDir;  // Define a variable to save the motor's rotation direction
12int rotationSpeed;    // Define a variable to save the motor rotation speed
13
14void setup() {
15  // Initialize the pin into an output mode:
16  pinMode(in1Pin, OUTPUT);
17  pinMode(in2Pin, OUTPUT);
18  pinMode(enable1Pin, OUTPUT);
19}
20
21void loop() {
22  int potenVal = analogRead(A0);// Convert the voltage of rotary potentiometer into digital
23  rotationSpeed = potenVal - 512;
24  if (potenVal > 512)
25    rotationDir = true;
26  else
27    rotationDir = false;
28  // Calculate the motor speed
29  rotationSpeed = abs(potenVal - 512);
30  //Control the steering and speed of the motor
31  driveMotor(rotationDir, constrain(rotationSpeed,0, 512));
32}
33
34void driveMotor(boolean dir, int spd) {
35  if (dir) {// Control motor rotation direction
36    digitalWrite(in1Pin, HIGH);  
37    digitalWrite(in2Pin, LOW);
38  }
39  else {
40    digitalWrite(in1Pin, LOW);
41    digitalWrite(in2Pin, HIGH);
42  }
43  analogWrite(enable1Pin, spd); // Control motor rotation speed
44}

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

 1int potenVal = analogRead(A0);// Convert the voltage of rotary potentiometer into digital
 2rotationSpeed = potenVal - 512;
 3if (potenVal > 512)
 4  rotationDir = true;
 5else
 6  rotationDir = false;
 7// Calculate the motor speed
 8rotationSpeed = abs(potenVal - 512);
 9//Control the steering and speed of the motor
10driveMotor(rotationDir, constrain(rotationSpeed,0, 512));

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  if (dir) {// Control motor rotation direction
 3    digitalWrite(in1Pin, HIGH);  
 4    digitalWrite(in2Pin, LOW);
 5  }
 6  else {
 7    digitalWrite(in1Pin, LOW);
 8    digitalWrite(in2Pin, HIGH);
 9  }
10  analogWrite(enable1Pin, spd); // Control motor rotation speed
11}