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

193K240125

Intro: 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.

STEP 1: 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

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)

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)

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

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

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

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

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

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

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

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!

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

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

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!

122 Comments

Since this project is around 10 years old, I wonder if you burned down your house with that. I'm sure it worked but holy moly, that looks like a safety hazard. I don't think that should be a how-to for other people. All the contacts are unprotected, close together, and without any housing. You only have to move a cable in the wrong direction and you create a short, that, depending on your power source, can burn down your house.
Newbie. Many sites insist on need for a diode across relay coil to protect upstream from back current when current to relay coil is cut. I note you don't have this, (and I am not sure where one would put it in this circuit). Does the lack cause problems?

excellent instruction. Question - how does the circuit work if I don't need to turn motor on? In my scenario i am connecting to a stall motor (tortoise point motor). Can I do without the second TIP 120?

Hi, I made the connections all same as this but, my motor does not turn. Also, when I tried checking the voltage at all pins of relay it is around 12V. Please help!

Maybe check your connections again? Did you ever plug anything in backwards? Does your power supply provide enough current to turn the motor?

I have checked my connections and also motor works fine when connected directly to power supply.

What do you mean by plugging anything backwards?

i used this same motor 2 pin method with just an arduino micro and a dc motor

in the d8 and d9 pin slots it works going orward but going backwards it just goes really slow forward

Sorry for slow response...

Not sure why it would be doing that... wondering if maybe the power source is experiencing some voltage sag when the relay is engaged?

also - some motors are "timed" so they spin faster one direction than the other...

-Rich

a rookie here so im sorry if the following question is stupid but if i replace the darlingtons with MOSFETS will it still do the trick?

Hey Rich, is there any way to add some code and a switch button to brake the motor? I like the design a lot but I definitely don't want the motor to be continuously running

i guess you could modify the code a bit

like when the button is pressed the enable pin is switched off and there is no delay in it

something like:

val=digitalRead(pushbutton);

if(val==1){

digitalWrite(9,LOW);

}

Existing setup already allows for on/off control via the "enable" pin.

If you're referring to active braking by shorting the motor.leads together - don't think there's easy way to do that.

Good luck!

very helpful!

i made it with MOSFETs

i made that circuit for running 12VDC car seat motor but one transistor (i.e. Enable one just keep burning. What should be possible solution to avoid that? Moreover, i did trying using heat sink and thermal paste but did not work the whole appratus just run for hardly 2 or 3 minutes and then transistor just go bad. Help needed

It sounds like you're just drawing too much current for the tip120 datlington.

You could try swapping the tip120 with a MJ11032GG (about $10 online). Way more current handling.

That said - if you're going down that path may make more sense to look at other options (pololu is a good website for motor drivers).

Good luck!
Great post.indeed simple and useful..can utilize those relays who are depressed from unemployment...thumbs up to you.

I'm considering using this circuit for a project at my internship. The caveat is I'm using a 12V 10A power supply as my motor draws a fair bit of current. I've already sourced a 12V 10A relay but as far as transistors go, I believe I need to go with 2 MOSFETs in order to handle the larger current. Would these work in place of the TIP120s? I'm also assuming the MOSFETs do not have built in flywheel diodes, and so I'm wanting to know where those would fit into the circuit.

thanks for any help and great instructable btw!

What motor do you use? I like the one in the video, would like to use the same model! Thanks!

actually have no idea of model (was many years ago I got it).

some places to look for gear motors:
http://banebots.com/

http://www.robotmarketplace.com/products/motors_geared.html

i finally managed to get the device to work.

More Comments