Instructables

Easy, Reversible Motor Control for Arduino (or any Microcontroller)

Featured
Picture of Easy, Reversible Motor Control for Arduino (or any Microcontroller)
This project uses just three main components to provide forward and reverse control for a single motor.  You can easily interface it to an Arduino or any other microcontroller.

It's so simple - you can wire it up "free-form" without a circuit board in about 15 minutes.


Features:
All parts available at Radio Shack for under $9
Supports PWM for variable speed control
Handles up to 5 amps peak / 2.5 amps continuous (5 amps continuous with heatsink)
Controlled using just two pins - "enable" and "direction"

Limitations:
Requires at least 7.5 volts to operate
Relay is rated for "only" 100,000 cycles and may not be appropriate for some high vibration projects
Doesn't provide motor "braking"

The most common way to provide reversible motor control is with an H-Bridge.  A basic H-Bridge is made up of 4 transistors - but commonly end up requiring more like 10 components when you include things like flyback diodes and secondary transistors.  

I wanted something simpler for a CNC project I'm working on - so I came up with this design.  I'm fairly sure I'm not the "inventor" of this circuit - but it's not widely documented.  As far as I can tell it doesn't have a name.

I am hereby naming it the RAT Controller.  RAT being an acronym for Relay And Transistors.
 
Remove these adsRemove these ads by Signing Up

Step 1: Stuff You'll Need

Picture of Stuff You'll Need
All parts are available at Radio Shack - expect to pay a bit under $9 for the main components.
The same parts are available online for under $4.

12VDC Coil DPDT Miniature PC Relay
Radio Shack Part: 275-249
If purchasing online - try searching for "OMI-SH-212D"

2 x TIP120 Darlington Transistors
Radio Shack Part: 276-2068

2 x 220 Ohm Resistors
Values do not need to be exact.

Optional: Heatsink
A TO-220 size heatsink such as Radio Shack 276-1363 will allow this motor controller to provide 5 amps continuously as opposed to just peak. You'll also need a #6 screw and nut. See the final "Notes" step for information on installing or making your own heatsink out of a pop can.

You'll also need:

Soldering Iron
And solder - any gauge is fine.

Hookup Wire
You'll need some kind of hookup wire to make connections and interface with your microcontroller.
22 Gauge Solid Core Hookup wire works well and easily fits into Arduino headers.
Available at Radio Shack - Catalog # 278-1221


Step 2: Schematic and Theory of Operation

Picture of Schematic and Theory of Operation
This circuit uses a DPDT (Double Pole Double Throw) relay to switch which direction the motor is turning.

The motor is connected to both normally closed and normally open (in reverse) sides of the relay.  This in effect reverses the wiring whenever the relay is turned on or off.

Since the microcontroller can't quite produce enough current to drive the relay - a  transistor (TIP120) is used to switch it on and off.

The "Base" of the first TIP120 is the "Direction Pin" - turning it on and off switches the direction of the motor.

A second TIP120 switches power to common on the relay.  This is used to turn the motor on and off.

The "Base" of the second TIP120 is the "Enable Pin" - turning it on causes the motor to actually run.

The enable pin may be switched on and off very quickly for PWM (pulse width modulation) speed control.

Both control pins are connected to the microcontroller via 220 Ohm resistors to limit current.

The minimum voltage to drive this circuit is determined by the "pickup" voltage of the relay.  This is listed as 9.6v - but I've found it to function properly as low as about 7.5v.

Don't worry if the schematic doesn't make total sense.  We'll go through all the connections one-by-one.

Step 3: Bridge NO and NC Pins (Part 1)

Picture of Bridge NO and NC Pins (Part 1)
Position the relay in front of you as shown in the picture.

Use a piece of hookup wire and your soldering iron to connect the pins as shown.

This connection bridges one of the Normally Open (NO) relay pins to one of the Normally Closed (NC) relay pins.



Step 4: Bridge NO and NC Pins (Part 2)

Picture of Bridge NO and NC Pins (Part 2)
Again - use a piece of hookup wire and your soldering iron to connect the pins shown.

This connection bridges the other Normally Open (NO) and Normally Closed (NC) relay pins.

Step 5: Connect Coil Pin To Common Pin

Picture of Connect Coil Pin To Common Pin
One last time - use a piece of hookup wire and your soldering iron to connect the pins shown.

This connects one of the relay's Coil pins to one of its Common pins.  Both of these pins will later be provided with positive voltage.

Step 6: Connect TIP120 Collector to Relay Coil Pin

Picture of Connect TIP120 Collector to Relay Coil Pin
Solder the middle pin of one of the TIP120s to the relay pin on your lower right (as pictured).

This connects the TIP120 Collector pin to the relay's other Coil pin.

Step 7: Nudge TIP120 Into Position

Picture of Nudge TIP120 Into Position
Carefully push the TIP120 towards your left and against the relay as shown.

This isn't just cosmetic - the TIP120 needs to be in this position for a connection we'll make later.

Step 8: Connect Second TIP120 Collector to Relay Common

Picture of Connect Second TIP120 Collector to Relay Common
Solder the middle pin of the second TIP120 to the bottom pin second from the left on the relay (as pictured).

This connects the second TIP120 Collector pin to one of the relay's Common pins.

Step 9: Connect TIP120 Emitters

Picture of Connect TIP120 Emitters
Push the second TIP120 up against the relay's case.

Bend the left-most pin of each TIP120 towards each other until they touch.

Solder the pins together as pictured.

This connects the Emitter pins of the two TIP120 transistors.


Step 10: Connect Resistors

Picture of Connect Resistors
Trim the leads of two 220 Ohm resistors to about 1/4 inch using scissors.

Solder a resistor to the end of the right-most pin of each TIP120 as pictured.

These resistors are connected to the Base of the transistors.  They limit current flow between the transistors and your microcontroller to safe levels.

Step 11: Review Connections

Picture of Review Connections
Congratulations!  You've completed the basic wiring - let's review how to hook things up.

+ Power
Connect this pin to your power source of 7.5v or higher.

GND
This pin needs to be connected to both ground of your power supply -and- ground on your microcontroller.

Enable
Connect this pin to a pin on your microcontroller.  Turning on this pin turns on the motor.
If you use a microcontroller pin with PWM - you can use it for variable speed control.

Direction
Connect this pin to a pin on your microcontroller. Turning on or off this pin switches motor direction.

Motor 1 and Motor 2
These pins connect to your motor leads.

Step 12: Hook It Up!

Picture of Hook It Up!
Connect all leads as listed in the prior step using hookup wire and your soldering iron.

Be sure to connect the GND pin to Ground on both your power source, and your microcontroller.

If you're using an Arduino - connect the Direction Pin to Arduino Pin 8 and the Enable Pin to Arduino Pin 9.

Step 13: Upload the Code and Test

Picture of Upload the Code and Test
Place the code below in an Arduino sketch - and upload it.

If you're not using an Arduino - review the code below to figure out what's going on.  It's not rocket science.

You should have a working motor controller!


//pin 8 = direction
//pin 9 = enable

void setup() {
pinMode(8, OUTPUT); //set direction pin as output
pinMode(9, OUTPUT); //set enable pin as output
}

void loop() {

//start off going forward at 50% throttle
digitalWrite(8, HIGH); //forward
analogWrite(9,128); //50% PWM
delay(2000);

//full speed ahead!
digitalWrite(9, HIGH); //full speed
delay(2000);

//and stop for a while
digitalWrite(9, LOW); //turn enable pin off
delay(1000);

//now lets go backwards
digitalWrite(8, LOW); //backward
analogWrite(9,128); //50% PWM
delay(2000);

//and stop for a while
digitalWrite(9, LOW); //turn enable pin off
delay(1000);

Step 14: Notes

Picture of Notes
If you're having problems with the controller refusing to reverse - it may be that your input voltage is too low.

The relay's coil seems to dictate the maximum voltage this circuit can handle.   It's rated at 130% of nominal - or 15.6v.

Unlike many commercial motor drivers - this driver does not have any "protection" - so if you abuse it too much - it will fail.  Fortunately - the component most likely to burn out is the transistor with the "Enable" pin - so you're only out a $1.50.

It should be possible to build a version of this driver that supports lower voltages by swapping out the relay with one having a lower "pickup" voltage.  I chose the one featured in this project since Radio Shack stocked it.

This project uses TIP120 "Darlington" transistors. These transistors are actually two transistors chained together into one. This gives them much higher "gain" - meaning they can use a very small current to switch a much larger current.  A TIP120 on its own provides a super-simple way to do single-direction motor control.

The TIP120 is rated at 5 amps - but will overheat without a heatsink if run this hard continuously.  I've verified the Radio Shack 276-1363 heatsink can be installed without re-soldering everything (you may need to bend stuff a little).  The heatsink should be installed on the transistor with the "Enable" pin using a #6 bolt and nut (screw it on tight!).

You can alternately make your own heatsink out of a piece of aluminum can.  Just cut a 1"x1" piece of the can using scissors - bend up the sides a little, and drill an 5/32" hole to mount it.  This may not work as well as a proper heatsink - but will definitely help.

Swapping out both the TIP120 and relay with higher-rated parts (readily available online) should let you build a much beefier version of this motor control fairly cheaply.

Have fun!
atai071 month ago

hello .. I have tried your circuit but the motor only moves in one direction only .. the delay is still there ... but I still hear the sound Click on the relay ..

so where is my mistake ... can you explain

I use tip 122 ..

relay as you use

batery 12 v 1.2 AH

plz help me ...

tq

nothinglabs (author)  atai071 month ago
If the relay is engaging - but the motor direction isn't changing - I would triple-check how you have the relay wired. (assuming you're using the same kind of relay as described)

Good luck!

-Rich

Can I connect two motors to the pins of relay instead of one for my project

or I need to make two similar circuits?

and, any other 6v relay should work fine, should nt it?

It worked pretty well until I burned it up. :)

Hi, i stumbled uppon a problem. I can hear my relay switch but when i hook up a 6/9v dc motor to it my motor doesn't turn. I'm using 7.4v.

nothinglabs (author)  ArthurCornelis2 months ago
The relay is only used to switch the direction of the motor - as opposed to actually toggling the power.

The tip120 connected to the "enable" pin is responsible for toggling the actual power - so I'd start troubleshooting there.

Good luck!

-Rich
zhungu4 months ago
Hello again. is there a way it can be modified to have "brake"?
nothinglabs (author)  zhungu4 months ago
Maybe.

I think typically motor controllers "brake" by shorting the motor leads. I don't think that's possible with this setup.

However - you could try quickly toggling the direction pin (maybe 20hz - 40hz) - and then doing some level of PWM on the enable pin.

Can't say if this will work - but it might. It will certainly make some noise though - and might not be very "nice" to the various components...
zhungu5 months ago
Hie. would this relay work as well? How do i know which ones are the normally open, normally closed pins and so on? Thanks

http://www.jaycar.com.au/productView.asp?ID=SY4065
nothinglabs (author)  zhungu5 months ago
I believe that relay would work assuming you provide the needed voltage.

I would suspect there is some indication on what pins do what on the relay itself. Otherwise - I might just experiment using a multimeter.
crob096 months ago
Hope you don't mind the question but I'm wondering how this circuit could be used to control a high power device, it looks like the 12V-power charges the relay coil and drives the motor, I maybe wrong but is there a way to connect another power source to drive say a 36VDC motor?
Thanks for any help,
nothinglabs (author)  crob096 months ago
Should be able to - just find a DPDT relay and "drive" darlington of the values you need.

The darlington that triggers the relay can probably stay a tip 120.

On this page I mention I higher power darlington option:
http://www.nothinglabs.com/openmelt/

(the solid state relay on that page might also be of interest)

-Rich
That link takes me to an awesome project thanks!!
Maybe I'm confused about the Relay, I notice they have two values one for the coil (I think) and the other for the max load, will the Max-load value work for the coil too, for example below the relay is rated at DC5V 28VDC?
Sorry I really don't see how I connect a higher voltage to this, My relays are "JQC-3F(T73) DC 5V 5PIN 250VAC 28VDC Power Relay"
I suppose this won't work also because of them being five pin opposed to six?
nothinglabs (author)  crob096 months ago
For this project you'll want DPDT relays - which I don't think those are.

Good summary on what this means here:

http://en.wikipedia.org/wiki/Relay

Relays are commonly rated at a voltage / current required to engage (you can usually go somewhat higher or lower) - and the maximum voltage / current it can switch (often different for AC and DC).

-Rich
Cool I've ordered some of the DPDT relays and the proper voltage.
Thank you for the summary link, you're right it has tones of good information.
Also thank you for the reply to my question and great job on the Tutorial.
Is it alright if I post something like this on my MOSFET page on electric-canada.com?
nothinglabs (author)  crob095 months ago
Good luck with the build.

Please feel free to use / share any aspect of this instructable however you like!
Ohh yeah one more thing I was looking at your website and the Arduino Laser show is awesome!!!!
http://www.nothinglabs.com/laser/
TomDunlap10 months ago
Just finished a prototype of this to run the motor for the solar forge I'm building. Here is a clip of it running a re-purposed Skil cordless drill:
nothinglabs (author)  TomDunlap10 months ago
nice!
RISL1 year ago
hello friend i have a question about your proyect and it is posible to use with a step direction 3d printer, the 3d printer use an arduino but is the only thing i need or is necesary a interpreter, i use a sprinter firmware, i need some change or only i have to conect direct.
nothinglabs (author)  RISL1 year ago
it should be able to drive any standard DC motor.

Not totally sure - but your printer may user stepper motors - which won't work with this project.
saip1 year ago
i dont understand all this circuit connections....can u suggest me any website where i could learn about all this stuff.......plz help me.................
itshuang1 year ago
Thanks for your suggestion. I found a wiring that might work for my need: http://www.quasarelectronics.co.uk/kit-files/help-files/uhf-remote-control-reversible-dc-motor-with-limit-switches.pdf

If I use the forward and reverse leads (2.5~3v) to activate the top and bottom relays (e.g a SPDT relay, such as this one: http://www.mdfly.com/index.php?main_page=product_info&products_id=444) respectively, and power(+) connected to the top NC, and power(-) to the bottom NO, do you think it will work? I appreciate your comments to a newbie like me.
itshuang1 year ago
While I am searching for ways to make my high torque DC motor reversible, I came across this instructable. It is amazing. What I am trying to do is to use a RC car's Transmitter(Tx)/Receiver(Rx) to control my DC motor's rotation(forward/reverse). The outputs from the Rx are one forward and one reverse which may be "high" depending on what is triggered on the Tx side. It works for low power DC motors. However, it is too weak to drive a high torque motor. So I need to get extra power to drive my motor reversibly. Is this control can be modified in some way to meet my need if I use the outputs from Rx as triggering signals (Say forward connects to one transistor and reverse links to another transistor)?
nothinglabs (author)  itshuang1 year ago
Hard to say - but maybe.

You might try connecting both leads from the RC car to the "enable" pin through their own diodes. Then - additionally connect just one of the leads directly to the reverse pin.

I think this -might- work (or it might make smoke).

have fun / good luck!

-Rich
I am planning to buy a new DC motor driver board which can run 5amp motor but I don't know how to connect it with Atmega 8A chip. Please see the pdf I attached for Atmega 8A.

Here are the 7 pins in DC motor controller:-

Pin No. Pin Functionality
1 GND Ground
2 IN-1 Logic input for the motor direction.
3 Diagnostic 1
(DG-1) Output pin with logic 1 output in normal operation. Represents side of the internal
H bridge corresponding to IN-1. Pin is pulled to logic low by the motor driver in
case of over temperature or overload due to short circuit.
4 PWM Used to apply Pulse Width Modulation to control motor velocity
5 Diagnostic 2
(DG-2) Output pin with logic 1 output in normal operation. Represents side of the internal
H bridge corresponding to IN-2. Pin is pulled to logic low by the motor driver in
case of over temperature or overload due to short circuit.
6 IN-2 Logic input for the motor direction.
7 CS* Current Sense output to measure the current flowing through the driver
schmidtbag1 year ago
Great guide. I'm using motors that operate up to 24V 36A (I'm running them at 6A though) so I used different relays and transistors, which work fine. The problem is (which I found out the hard way) was the transistors don't have the built-in transistor that the TIP120 has and so blew up from the EMF. Thankfully I have more and only 1 transistor died. Anyways, which diode would you recommend and where do I place them in the schematic you have above? Thanks
nothinglabs (author)  schmidtbag1 year ago
Glad you like the instructable!

I haven't used an EMF protection diode with this circuit before (and haven't done too much with them in general) - so take this with a grain of salt.

I would place the diode between the transistor's collector and the positive input voltage. Wire it so the arrow points towards the positive voltage input (the other way round will cause a short).

more info here:
http://www.learnabout-electronics.org/ac_theory/inductors02.php

Sizing is a good question. There is some info at link above. The back EMF voltage can be higher than the driving voltage. Might look for something that can handle the input current at some multiple of the voltage than your'e driving it at. Might be able to get away with something less beefy since it's not getting hit constantly.

Good luck!

-Rich
asharma711 year ago
I used this circuit with the following parts:
HLS-4078-12vDC
www.helishun.com/hls-4078.html
1k resistor
and tip122
everything works fine but reverse. i suspected 6 volt battery was a problem but i am using a 12 volt battery now.

Please help
Unknown.jpegUnknown.jpegUnknown-1.jpeg
nothinglabs (author)  asharma711 year ago
I would use a multimeter to make sure you're getting higher than 9v coming -out- of the reverse tip122.

You'll loose some voltage through the transistor - and if the battery is too small - it'll drop more when it tries to turn the motor.

Easy test is to leave enable off - and then just toggle reverse back-and-forth in code. Make sure you hear the relay go "click"
Great project! I wire switches like this all the time. Never thought of making one of these. I will surely use this a lot.
Thanks!
this is an awesome project!! but i CANNOT get the motor to run in reverse for the life of me...i am 100% sure i have the schematic right and it built right... i am 100% sure my code is correct and im even using a 12 volt battery... it will do forward and forward with pwm but NOT backward... its killing me.. please help!! i spent like 20 bucks getting parts and id hate to go and find another solution to this
nothinglabs (author)  paseanlysan1 year ago
are you using the exact relay specified?

what kind of 12v battery are you using?
jackxeus1 year ago
I cannot figure out what I'm doing wrong! The circuit powers my motor, but it won't switch the direction of rotation. I am using a 12VDC, 1Amp wall wart power supply, so I assume that's providing enough power. I hear the relay switching, but the motor continues to rotate in the same direction.

Any ideas?
nothinglabs (author)  jackxeus1 year ago
I would tripple check your wiring - specifically the wires cross-connecting the two sets of relay outputs.

good luck!

-Rich
compgeek972 years ago
does it matter what relay is used as long as it's 12v 5a? i found 2 OMRON MY2NJ on ebay for cheaper than the ones used in this instructable. I'm a kid with no job so I kinda need to cut all the corners I can :D
nothinglabs (author)  compgeek972 years ago
From the specs - it looks like those guys have a 28v trigger voltage - which is too high.

suspect it would probably not work.

hope this helps!

-Rich
DNMEBOY2 years ago
I made this and it seems to be working correctly but my motor does not go backwards. Any ideas?
funny. I'm using a 10 volt battery and using this circuit for a stepper motor from a drill intended for 7.2 V. It won't go backwards either. (I set the PWM power control to half (128) so that it's not too hard on the motor.)

I get the feeling something is fundamentally wrong because no power comes through to the motor when it's supposed to be running in reverse.

any ideas?
nothinglabs (author)  stupidloginthing2 years ago
a very common cause of this problem is a battery that's not producing enough voltage to fully switch the relay. 10v should be enough - is it possible the battery isn't fully charged? (it may also be that the voltage sags to under 7.5v when powering the motor).

this might sound really generic - but I would triple-check all the wiriing.

is it possible anything is backwards / upside down?

I'm assuming you're not really using a stepper motor - but some kind of geared DC motor (stepper motors require very special drivers).

Good luck - let me know how it goes.

-RIch
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!