In this tutorial, we will learn to control a DC motor’s speed and direction using the L293D motor driver IC. The L293D IC is a powerfully built motor driver IC. It is a dual-channel H bridge motor driver IC which can be easily used to drive two motors. We will discuss in detail about this motor driver IC and how to interface it with Arduino. Then, we will show you an Arduino sketch that will control the speed and direction of the DC motors every easily. So let us begin. Show If you are using Arduino L293D motor driver shield, you can read this article also:
DC Motors IntroductionDC motors are electro-mechanical machines which convert electrical energy into mechanical (rotational) energy. If you are looking to develop a robot such as a line follower robot, obstacle avoidance robot, these DC motors will be the first choice for you. They are available in direct drive or geared types. But, both types require a dc voltage to operate. Therefore, we apply a DC voltage to drive DC motors. DC Motor Speed ControlThe speed of rotation of motors is directly related to the input voltage. The higher the input voltage, the higher will be the rotational speed of the motor. But the voltage should be within the operating voltage range. One important point to note here is that if we want to control the speed of a DC motor, we will need to provide a variable voltage to the DC motor. For example, If the operating voltage range of a motor is between 3 – 6 volts. If we apply 3 volts input, the motor will run at its lowest rated speed. Similarly, if we apply 6 volts, the DC motor will run at its highest rated speed. In short, we can control the speed of rotation by giving a variable input voltage to a DC motor. Usually, a pulse width modulation technique is used to generate a variable dc voltage from constant dc voltage source. DC Motor Direction ControlAs you know that in case of DC power supply, there is a concept of polarity such as positive and negative terminals of a battery. The polarity of input voltage source determines the direction of rotation of a DC motor. For instance, if we connect the positive terminal of battery with one terminal and the negative terminal of battery with another terminal of DC motor, it will start rotating in clockwise direction. But if we interchange the connection of the battery by connecting opposite polarity, the DC motor will start rotating in another direction as shown in the simulation below. Generally, an H-Bridge circuit is used to provide opposite polarity power to a DC motor without changing actual power supply connections. An H-Bridge consists of four switches, resistors, and two control signals as shown in the figure. Two logic probes control the direction of a DC motor. When left probe logic signal is on, the motor rotates in an anti-clockwise direction and when right probe logic signal is on, the motor rotates in a clockwise direction. L293D Motor Driver ICL293D IC is known as a motor driver. It is a low voltage operating device like other ICs. L293D provides the continuous bidirectional Direct Current to the Motor. The Polarity of current can change at any time without affecting the whole IC or any other device in the circuit. L293D has an internal H-bridge installed for two motors. H-Bridge is an electrical circuit that enables the load in a bidirectional way. L293D bridge is controlled by external low voltage signals. It may be small in size, but its power output capacity is higher than our expectation. It could control any DC motor speed and direction with a voltage range of 4.5 – 36 Volts. Its diodes also save the controlling device and IC from back EMF. To control the max 600mA amount of current an internal “Darlington transistor sink” installed in it, which could be used to control a large amount of current by providing a small amount of current. It has also internal “pseudo-Darlington source” which amplifies the input signal to control the high voltage DC motor without any interception. PinoutThe diagram below shows the pin out of L293D IC: Enable Pins
Input Pins
Output Pins
Power Pins
Control PinsThe L293D IC features both speed and direction control pins to control two motors simultaneously. Direction Control Input PinsThe direction control pins are the four input pins (Pin2 IN1, Pin7 IN2, Pin10 IN3, Pin15 IN4) on the IC. Through these input pins we can determine whether to move the dc motor forward or backwards. IN1 and IN2 control motor A’s spinning direction whereas IN3 and IN4 control motor B’s spinning direction. The table below shows the logic signals required for the appropriate spinning action for motor A. IN1IN2Motor Action1 (HIGH)1OFF10 (LOW)Backward01Forward00OFFAs seen from the table, whenever one of the inputs is in a HIGH state (5V) then the motor will spin. Otherwise, when both the inputs are LOW (ground) state or both are in HIGH state then the motor stops. In order for motor A to spin forward, IN1 should be LOW and IN2 should be HIGH. For backwards motion, IN1 should be HIGH and IN2 should be LOW. Motor B is also controlled in a similar way. Speed Control PinsThe speed control pins pin1 (EN1,2 or ENA) and pin 9 (EN3,4 or ENB) on the IC, control the speed of the dc motor and turn it ON and OFF. EN1,2 controls the speed of one motor and EN3,4 controls the speed of the other motor. If both of the pins are in a logic HIGH (5V) state, then both the motors are ON and spinning at maximum speed. If both of the pins are in a logic LOW (ground) state, then both the motors are OFF. Through the PWM functionality, we can also control the speed of the motor. You can know more about the L293D motor drivers here:
Features
Interface L293D DC Motor Driver IC with ArduinoNow, as we have seen how to control the dc motor through the motor driver IC, let us do a demonstration by showing you how to control two DC motors using this IC. Required Equipment
Assemble the circuit as shown in the connection diagram below. Arduino with L293D IC and DC Motors connection diagramWe are using TT DC gear motors for this project. They require an operating voltage of 3-12V DC where the recommended operating voltage is 3-6V DC. Thus we will use 4xAA batteries (4×1.5V = 6V) to supply power for the DC motors. Connect positive terminal of power supply with pin8 and negative terminal with common ground. Then, we have connected the input pins Pin2 (IN1), Pin7 (IN2), Pin10 (IN3) and Pin15 (IN4) with digital pins of the Arduino UNO. We have used GPIO9, GPIO8, GPIO7 and GPIO6 respectively to connect with each of the input pins of the motor driver IC. The enable pins pin1 (ENA) and pin9 (ENB) are connected with the digital PWM pins of the Arduino UNO board. We have used GPIO10 to connect with EN1,2 and GPIO5 with EN3,4. Each of the motor’s terminal is connected at Pin3 (OUT1), Pin6 (OUT2) for one motor and Pin11 (OUT3), Pin14 (OUT4) for the second motor respectively. Connect pin16 of the driver IC with 5V from Arduino. Additionally, all grounds should be common. Controlling DC Motor Speed and Direction using L293D Motor Driver ICOpen your Arduino IDE and go to File > New. A new file will open. Copy the code given below in that file and save it. Arduino SketchThis basic sketch will show us how to control a DC motor’s speed and direction of rotation using the L293D motor driver IC.
How the Code Works?Firstly, we will define the L293D motor driver IC’s control pins connection with the Arduino UNO board. We have used the same pins as shown in the connection diagram above. Make sure the enable pins are connected with PWM enabled pins of the Arduino board. For the input pins you can use any digital pin of the Arduino UNO board.
setup()Inside the setup() function, first we will configure all the control pins as output pins. This will be done by using the pinMode() function. The pin will be passed as the first parameter and OUTPUT will be passed as the second parameter.
Then by using the digitalWrite() function we will set up all the input pins to a LOW state so that initially both the motors are off.
loop()Inside the loop() function, we will call the user defined functions setDirection() and changeSpeed() after a delay of 1 second. The first function will be used to control the direction of the motors and the second function will control the speed.
setDirection()The setDirection() function is responsible to control the direction of the two motors. The motors can either turn on, off or change their directions. This is done by providing the input pins (IN1, IN2, IN3 and IN4) with different logic signals. As we have already discussed before, through these input pins we can determine whether to move the dc motor forward or backwards.
First we will set up both the motors at their maximum speed. This will be done by using the analogWrite() function and passing the enable pin as the first parameter and ‘255’ as the second parameter. Here 255 denotes the highest PWM value.
Then we will first turn both the motors ON, then change their direction and then turn them OFF. These three actions will be done after a delay of 5 seconds each. Use digitalWrite() to change the logic state of the pin by passing the pin as the first parameter and the logic state as the second parameter. Follow the table given in the Direction Control Pins, section to set the input pins to required states appropriately.
changeSpeed()The changeSpeed() function is responsible to control the speed of the two motors. As we have already discussed before, the speed control pins labeled ENA and ENB on the module, control the speed of the dc motor.
First, by using the digitalWrite() function turn on the motors by setting the relevant logic states to the input pins.
Then we will increase the speed of the motors steadily. By using a for loop we will increment the PWM value from 0 to 255 (maximum speed). This is done through the analogWrite() function which takes in the enable pin as the first parameter and the PWM value as the second parameter. 0Likewise, we will decrease the speed of the motors from a maximum (255) to zero. 1Lastly, we will turn the motors off by providing a LOW signal to all the input pins.
DemonstrationTo see the demonstration of the above code, upload the code to Arduino. Before uploading the code, make sure to select Arduino UNO from Tools > Board. Also, select the correct COM port to which the Arduino board is connected from Tools > Port. Once the code is uploaded to your board, the motors will start rotating. First, the motors will start rotating backwards for 5 seconds then they will start rotating forwards for 5 seconds. Then the motors stop. After a delay of 1 second, the motors start speeding up and reach maximum speed then they start slowing down and finally stop. Then the loop starts again. |