Simple 2-way motor control for the arduino

 by computergeek
100_4205.JPG
MotorSchematic.png
When I say simple, I don't mean use a speed control. But control the motor directly without any external circuitry. Here's how:

How this came about:

I recently was working on an instructable about charlieplexing with an arduino. And I was wondering if the same principle would work with motors to some extent. So I came up with the idea that if you used a motor instead of an led you could have 2-way control of it & if you used 2 PWM (Pulse Width Modulation) ports you could have 2-way variable speed control for a motor with no external hardware!! So I decided to post my findings. Have fun! If you have any questions Please ask them.


Note: This is not the safest way to control a motor. Each I/O pin can only handle 40 mA of current. I would recommend using a H-Bridge as pointed out by: Bongmaster & Frollard.

I am not responsible for any thing bad that happens to you or to your arduino!


 
Remove these adsRemove these ads by Signing Up

Step 1: The stuff you need:

100_4208.JPG
Parts:
- Arduino
- Small DC motor
- Wire for motor

Tools:
- Computer with Arduino IDE installed
- A-B USB cord
asarris says: May 8, 2013. 2:45 PM
I am not sure if this is my fault or instructable's fault, but when I try to download this file I get a temporary file.
shanymz says: Mar 3, 2013. 3:28 PM
Hi,
I tried your code to run a 12 V dc motor, and it doesnt work. I just hear beeping from motor,
it is connected directly to computer using arduino uno.
I am a newbie! so I really appreciate your help
gee12 says: Dec 18, 2012. 4:01 PM
Good Tutorial,
msurguy says: Dec 12, 2012. 11:31 AM
So I am planning to drive a small Electromagnetic coil with this code, it is 120 Ohms so the power consumption will only be 20-40mah max, this code should work, right?
Lenny24 in reply to msurguyDec 14, 2012. 12:45 PM
Hey, since motors, coils and/or speakers are complex loads, meaning they do not only consist of a resistance that might be defined by Ohm's Law, even a small electromagnet with a static resistance thats > 1000 Ohms could easily kill your Arduino.
The Code should work just fine, but you might consider using an H-Bridge for controlling current.
msurguy in reply to Lenny24Dec 14, 2012. 3:40 PM
Thanks ! I might use TI DRV883x series chip for the H Bridge driver then!
acain3 says: Jun 25, 2012. 3:01 PM
I'm working on a schematic that uses four transistors per engine, a universal ground, a universal power, and an Arduino Uno. It will allow you to use two wires to control both motor directions. It switches two transistors on at a time.
vkarpuram1 says: Jun 4, 2012. 4:01 AM
You must not use this method to control a motor with the arduino as it could blow up the whole board, instead you could use a motor driver like the l298n. Here's a tutorial - http://vkwarehouse.blogspot.in/2012/05/bidirectional-motor-control.html
acain3 in reply to vkarpuram1Jun 25, 2012. 12:46 PM
Your link popped up containing Malware.
David97 says: Feb 19, 2012. 12:33 AM
I wold not trythhis with big motors anyone looking at doing it for several reasons.
#The current draw would be too big for the arduino
#the back EMF could produce a huge voltag spike and blowing up your arduino.
#Do you think its a good idea when most instructables and projects have motorcontrollers to prevent blowing up the arduino?
Eonir says: Nov 2, 2009. 4:12 AM
The power limits of the microprocessor are not the only thing you should be worried about.

A motor is not an entirely resistive object. It has also some inductance. What does that mean? It means that when you reverse the voltage on the pins, the current doesn't change its direction immediately. So, for a short moment, the motor acts like a current source.

And that could effectively blow up your microprocessor.
Eonir in reply to EonirNov 2, 2009. 4:14 AM
I didn't have time to check your code, but there's one thing you can do to prevent a hot situation. Whenever you want to switch your motor, a dead time would be applied, in order to allow for the current to fall to a negligible level.
uberdum05 in reply to EonirAug 6, 2011. 8:19 AM
Or just slowly ramp the motor speed up and down
shortcircuitaz says: Feb 16, 2011. 12:41 PM
I tried this and my motor must be too big or something.. do I need to send any information through the interface to the controller?
wfelix says: Feb 2, 2011. 11:59 AM
very good for small motors... '

but, for big motors, use analog io or gnd+digital with an external driver is better (obviously)''

but, yet, is a great idea
GitarGr8 says: Dec 15, 2010. 9:04 AM
Just so you know, there is an extremely large voltage that is created when the magnetic field of a motor breaks down, ie. when you switch off the circuit. You can visually see this if you hook up a 9V batter to the motor and slowly remove a lead. The spark that is created means a LOT of volts (someone smarter than I could tell you an exact number) which WILL damage / destroy the transistors in your chip. You can get around this by using a protection diode, which will dissipate that voltage back through the coil:

http://www.kpsec.freeuk.com/trancirc.htm
(scroll down to protection diode)
kyle brinkerhoff says: Sep 3, 2010. 4:47 PM
OR! you could just buy a pwm motor controller and drive FLIPPEN HUGE MOTORS!
the_burrito_master in reply to kyle brinkerhoffSep 21, 2010. 10:28 PM
Try a couple transistors as amplifiers.
beehard44 in reply to the_burrito_masterOct 24, 2010. 7:53 AM
or relay?
the_burrito_master in reply to beehard44Oct 24, 2010. 8:12 AM
uh yeas.
Chowmix12 says: Jun 29, 2010. 8:07 PM
check out STM, they have free samples for an l298HN IC, which is an ic that incorporates 2 h bridges inside the IC.
BrunoG says: Feb 15, 2010. 11:37 AM
cool.
daltore says: Nov 27, 2009. 5:20 PM
It seems to me like this would only be able to drive the motors at a maximum of 1/2 power each when they're reverse of each other.  You could get both to move full-power when they're going the same direction (they share either a common ground or common source), but when they're opposite, you're spending 1/2 of each PWM train on each motor (which at full power, is a 1/2 duty-cycle PWM wave).  Interesting idea though, it's crazy what you can do with microcontrollers these days.
Bongmaster says: Oct 31, 2009. 5:56 PM
kool but its best not to drive a motor directly from an arduino ;) could blow ure pins..

u can make a simple h bridge from transistors like i did here
http://bongmasters-things-and-stuff.blogspot.com/2009/05/robot-h-bridge-pair-motor-driver.html

using the schematic on this page
http://www.beam-online.com/Robots/Circuits/circuits.html

there was a 4 transistor version somewhere too. cant remember where tho XD

computergeek (author) in reply to BongmasterOct 31, 2009. 6:10 PM
 I'm glad you like it! It is definitely not the best way, but probably the simplest. You do have to be careful using this approach.
Bongmaster in reply to computergeekOct 31, 2009. 7:41 PM
hell yea XD i did it this way 1st XD but i didnt want to blow my chip by drawing the current thu it to drive a motor :)

hence why i built the h-bridge pair i linked ;) better safe XD
frollard in reply to BongmasterNov 1, 2009. 5:14 AM
Drawing current and especially drawing a changing current (like pwm) thru an inductive load like a motor can have disastrous results to those little pinnies :P

Totally agreed in using an h-bridge, driven by these pins.
computergeek (author) in reply to frollardNov 1, 2009. 7:35 AM
 I looked at the Arduino Specs, and found each I/O pin can only handle 40 mA of current. I measured the current draw of my motor & found it was at the max 35 mA's. So I decided to put a warning in my instructable. Personally I prefer continuous rotation servos over motors.
frollard in reply to computergeekNov 1, 2009. 7:30 PM
Remembering when a motor turns on or off it has a pulse of very high inrush current to get it started.  A multimeter will probably not even detect it because its so fast - but its enough to destroy a pin.

using pwm just means you're turning it on and off MANY times...more risk of burning out that expensive pin....my 2 cents.
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!