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.

  1. 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”.
  2. 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.
  3. 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.
  4. Pin 4 and 5 as you can see are all ground pins and should be connected to the ground of your circuit.
  5. (look at option 4)
  6. (look at option 3)
  7. (look at option 2)
  8. 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.
  9. Pin 9 is also the enable pin for the right part just like pin 1 is for the left part.
  10. pin 10 and 15 are also the input pins for the right part and is to be connected to your microcontroller.
  11. 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.
  12. pin 12 and 13 are also the ground pins.
  13. (look at option 12)
  14. (look at option 11)
  15. (look at option 10)
  16. 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
Note that pin 3 and 5 of the Arduino UNO Support PWM(Pulse width modulation) and thats why we used those pins. You can change it to any of the PWM pins too. The reason for using PWM is because it will allow us to be able to control the speed of the motor. Watch the attached video to get more understanding.

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.
Sketch: motor speed and direction control
[code lang=”js”] //this program demostrates speed control of a dc motor connected to the left part of an l293d driver. //the motor will accelerate and deccelerate in one direction, change direction and then accelerate and decelerate in the opposite direction int foward =3;//this pin is connected to pin 2 of the IC int backward =5;//this pin is connected to pin 7 of the IC void setup() { pinMode(foward, OUTPUT); pinMode(backward, OUTPUT); //both of the above are output because the //commands will leave the Arduino to the L293d } void loop() { //the 1st and 2nd for() loops move the motor in the first direction. //the 1st increase the speed and the second one decreases the speed in the same direction for(int i=0;i<=255;i=i+5){ analogWrite(foward, i); digitalWrite(backward, LOW); delay(100); } for(int i=255;i>=0;i=i-5){ analogWrite(foward, i); digitalWrite(backward, LOW); delay(100); } //the 3rd and 4th for() loops move the motor in the second direction(opposite to the previous direction) //the 3rd on increases the speed and the 4th one dereases the speed in the same direction. for(int i=0;i<=255;i=i+5){ digitalWrite(foward, LOW); analogWrite(backward, i); delay(100); } for(int i=255;i>=0;i=i-5){ digitalWrite(foward, LOW); analogWrite(backward, i); delay(100); } } [/code]

Related Articles

Using the dual axis analog joystick module

Where are the game center squad?
Do you remember those guys behind the UCOM game pad who would be like “oh as for me I play FIFA well with analog” and at the end of the day they would still loose the match, eiiiii?
You know what, today we will be doing something more technical and fantastic with one of the joystick modules.
We will learn how to harvest the x and y coordinates out of the joystick and use it to control things in real life…..

Addressing the Newbie Passions!

Since the days the worlds were formed by the Great I Am, humanity has never stopped expressing the creative power that was embeded in us since day 1! From before the agricultural age through the 1st industrial revolution to the 4th revolution on whose gates I stand and write today, there has always been the need to invent new things or make a way of life better than a previous.

The 555 timer IC.

As a hobbyist, engineer, or maybe an electronics geek or wherever you find yourself, you can’t really have the time and pleasure to know what goes on in all the million ICs in the world. However you can delve with me into this simple 555 timer to see whats going on in it. This can give you a certain level of understanding about almost all ICs.

Responses

Your email address will not be published. Required fields are marked *

  1. Sure ,That’s very interesting. Have always wanted a way to control the motor in both directions. Big ups👍👍