Arduino(7): L293D controls two motor steering and speed

The previous implementation of the Arduino(6): L293D drive motor  uses L293D to control the fan motor, using linear resistors to control the speed, which is only one of the small features of the L293D. To fully control the DC motor, control its speed and direction of rotation. This can be done through the following two technologies:
  • PWM: used to control speed
  • H-Bridge : used to control the direction of rotation

[PWM]

Using PWM (Pulse Width Modulation), you can control the speed of the DC motor by changing its input voltage. PWM is a technique that uses a series of ON-OFF pulses to adjust the average value of the input voltage. In an easy-to-understand language, it is a switch that frequently changes the voltage. The program is switched at high speed for a fixed time, and the voltage is switched many times. When the opening time is longer, the speed of the motor will be faster; if it is closed, The longer the time, the faster the motor will be. The proportion of time we will give the voltage is called the Duty Cycle. The higher the duty cycle, the higher the average voltage applied to the DC motor (high speed), and the lower the duty cycle, the lower the average voltage applied to the DC motor (low speed). For example: brightness control of the light, motor speed control, screen brightness control, speaker size sound / sound frequency control.

[H-Bridge]

H-Bridge, according to Wikipedia's explanation, H-Bridge is an electronic circuit that reverses the voltage across the load or output. I believe that after reading the above explanation, many people still don't understand what H-Bridge is. Let's use the following examples to explain it. It may be easier to understand. H-Bridge takes 4 switches and one motor as an example. The connection method is as follows:

source: Wikipedia

When switches S1 and S4 (left of the figure below) are closed and S2 and S3 are open, the positive voltage will cause the motor to rotate forward in accordance with the direction of the current. When the switches of S1 and S4 are open and the switches of S2 and S3 are closed (right side of the figure below), this voltage is reversed, causing the motor to turn the opposite. When using this circuit, switches S1 and S2 should not be closed at the same time, because this will cause the input voltage to be short-circuited, the current will flow from the positive electrode through the transistor directly back to the negative pole, and the same S3 and S4 cannot be closed at the same time. This condition is called shoot-through and it is possible to burn out the transistor. In the actual drive circuit, it is common to use a hardware circuit to conveniently control the switching of the transistor.


 source: Wikipedia


Pins 2 and 7 of L293D control the current direction, which affects the output of pins 3 and 6 (motor wiring), that is, controls the direction of rotation of the motor. The following table shows the operation of the motor when the two Input potentials are high.
Input 1Input 2turn around
Low (0)Low (0)Motor stop
High(1)
Low (0)Forward
High(0)
Low (1)Reverse
Low (1)Low (1)Motor stop

[material]

• Breadboard x 1 
• Arduino UNO R3 x 1 
• DC 5V motor x 2 
• L293D IC x 1 
• External power transformer x 1 
• 18650 battery x 2 
• Cable x N

[Wiring and circuit diagram]

Arduino
L293D
Motor 1
motor 2
Pin 3
1
-
Pin 4
2
-
-
-
3
-
one side
GND
4
-
-
-
6
-
another side
Pin 5
7
-
-
-
8
-
-
Pin 11
9
-
-
Pin 10
10
-
-
-
11
one side
-
GND
12
-
-
-
14
another side
-
Pin 9
15
-
-
5V
16
-
-
Note: The negative pole of the external power supply is grounded together with Arduino GND and L293D 4, 12.



[code]

// motor 1
#define M1Pin1 10
#define M1Pin2 9
#define E1Pin 11

// motor 2
#define M2Pin1 5
#define M2Pin2 4
#define E2Pin 3

void setup() {
  pinMode(M1Pin1,OUTPUT);
  pinMode(M1Pin2,OUTPUT);
  pinMode(E1Pin,OUTPUT);

  pinMode(M2Pin1,OUTPUT);
  pinMode(M2Pin2,OUTPUT);
  pinMode(E2Pin,OUTPUT);

  motorstop();
}

void loop() {
  directionControl();
  motorstop();
  delay(2000);
  speedControl();
  motorstop();
  delay(3000);
}

void motorstop()
{
  // stop
  digitalWrite(M1Pin1, LOW);
  digitalWrite(M1Pin2, LOW);
  digitalWrite(M2Pin1, LOW);
  digitalWrite(M2Pin2, LOW);
}

void directionControl()
{
  // setup max speed
  analogWrite(E1Pin, 255);
  analogWrite(E2Pin, 255);

  // Start
  digitalWrite(M1Pin1, HIGH);
  digitalWrite(M1Pin2, LOW);
  digitalWrite(M2Pin1, HIGH);
  digitalWrite(M2Pin2, LOW);
  delay(2000);
  
  // change direction
  digitalWrite(M1Pin1, LOW);
  digitalWrite(M1Pin2, HIGH);
  digitalWrite(M2Pin1, LOW);
  digitalWrite(M2Pin2, HIGH);
  delay(2000);
}

void speedControl()
{
  // start
  digitalWrite(M1Pin1, LOW);
  digitalWrite(M1Pin2, HIGH);
  digitalWrite(M2Pin1, LOW);
  digitalWrite(M2Pin2, HIGH);
  
  // speedup
  for (int i = 0; i < 256; i++)
  {
    analogWrite(E1Pin, i);
    analogWrite(E2Pin, i);
    delay(20);
  }
  
  // slowdown
  for (int i = 255; i >= 0; --i)
  {
    analogWrite(E1Pin, i);
    analogWrite(E2Pin, i);
    delay(20);
  }
}

[Results]


[Reference]

● Wikipedia:H-Bridge
● Lastminuteengineers:Interface L298N DC Motor Driver Module with Arduino

Post a Comment

Previous Post Next Post