Controlling the speed and direction of a DC motor.
Dc motors were one of the coolest things most of us used to play with as kids. You should even recognize this particular one if you were also an electronic savage like me or used to play with some electronic toy cars and others.
To me what made it more cool was the fact that I could make it move in both directions, thus, clockwise and anticlockwise. But I could only change the direction by manually swapping the polarit of the batteries I used to power it, and that is so not cool if you think well about it. Ask yourself, then how are those toy cars and modern electric cars able to move forward and bakwards without anybody having to manually change the polarities of the motors?
And also concerning the speed of the motor the observation I made was “when I use one battery, the speed is low but when I increase the number of batteries the speed increases as well”.
So at this point I automatically knew two great things about DC motors,
- Speed
- Direction
But the real question was “How do I control both the speed and direction of these motors when I build a sytem, machine or robot with it?”. It was just a few years ago when I started to advance in electronics that I discovered some wonderful solutions which I want to share some with you today
Motor Drivers!
Yes there exist stuff like motor drivers and today I want to show you how to use one of the common ones I find amazing.
Motor drivers are Integerated circuits that are used to control the speed and direction of DC motors in automated systems, robots and machines. They serve as an intermidiate between whatever microcontroller or microprocessor used in the system and the motors themselves.
So the microcontroller will control the motor driver and the motor driver will control the motors too. The most common drivers you will mostly find are the “L29….” H-Bridge series. So in this tutorial I am going to use the “L293d IC” in particular to control the speed and direction of one DC motor.
The L293d motor driver!
The L293D motor driver is a 16 pin H-Bridge IC that can control the speed and direction of a maximum of 2 separate motors at the same time. I presume you at least know what IC’s are and how to count the number of pins on it but if you now want to know more about IC’s(Integerated circuits) do well to checkout this wonderful article I wrote sometime ago.
Pin number 1 is the pin on the top left of the IC when the notch is positioned at the top. And also note that the first 8 pins(1-8) are used to control one motor and the next 8 pins(9-16) are used to control a second motor.
So in this tutorial I will just go ahead and show you the functions of all the pins on this L293d ic so that you can know how to build the circuit with it.
The L293D schematics and pinouts.
- Pin 1 is the enable pin for the first part of the IC. If you want to control a dc motor with the left part of the IC this pin has to be connected to a “HIGH”.
- Pin 2 and pin 7 are the two input pins for the left part of the IC. These two pins are connected to the microcontroller and they are the very pins that tell the IC how to control the motors.
- Pin 3 and pin 6 are also the two output pins of the first part that IC. They are to be connected to the terminals of the motor you want to control.
- Pin 4 and 5 as you can see are all ground pins and should be connected to the ground of your circuit.
- (look at option 4)
- (look at option 3)
- (look at option 2)
- Pin 8 is the power pin for the motors. The voltage that powers both motors is to be connected here. The voltage range is between 4.5 volts and 36 volts so it means you can control both big and small motors.
- Pin 9 is also the enable pin for the right part just like pin 1 is for the left part.
- pin 10 and 15 are also the input pins for the right part and is to be connected to your microcontroller.
- pin 11 and 14 are also the output pins for the right part of the IC. You will connect the second motor to theses terminals if you want to control two motors.
- pin 12 and 13 are also the ground pins.
- (look at option 12)
- (look at option 11)
- (look at option 10)
- wonderful. Dont confuse yourself with pin 8 and 16. Pin 16 is the pin that powers the IC itself not the motors. You only need 5volts to power the IC.
Build the motor driver control circuit
Good, so at this point you should be farmiliar with the IC already. If you dont have one yet do well to check the Aaenics store to see if there is some in stock and order some for your projects.
So in this particular circuit we are only controlling one motor with the driver and hence only the left part of the L293D IC is used. Nevertheless don’t forget that though pin 16 is on the right hand side, it is the pin that powers the IC itself and it has to be connected to the 5v supply of the microcontroller as well.
Lets put some life in the circuit now
Of course you cant have a microcontroller in your circuit and not program it. But the question is what are you going to program the microcontroller to do?
We established previously that, the microcontroller has to control the motor driver and then the motor driver will also control the motor at last.
So the link between the microcontroller and the motor driver in our circuit is:
- Pin 2 of the IC has been connected to pin 3 of the Arduino UNO
- pin 7 of the IC has been connected to pin 5 of the Arduino UNO
The sketch below is a program that uses a for() loop and the analogWrite() function to demonstrate speed and direction control of the dc motor. It is very important to undertand the code so that you can modify it to fit in any project you want to use it for too. Watch the video above to see the demonstration.
Great work Effah 🤩
Sure ,That’s very interesting. Have always wanted a way to control the motor in both directions. Big ups👍👍
Oh sure man! I think there is more awesome stuff coming for us!