You are on page 1of 6

Home Sign Up!

Browse Community Submit


All Art Craft Food Games Green Home Kids Life Music Offbeat Outdoors Pets Photo Ride Science Tech

Simple 2-way motor control for the arduino


by computergeek on October 14, 2009

Table of Contents

Simple 2-way motor control for the arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Intro: Simple 2-way motor control for the arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Step 1: The stuff you need: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Step 2: Connecting the motor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Step 3: Coding the Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Step 4: How this works & more Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

http://www.instructables.com/id/Simple-2-way-motor-control-for-the-arduino/
Intro: Simple 2-way motor control for the arduino
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!

Step 1: The stuff you need:


Parts:
- Arduino
- Small DC motor
- Wire for motor

Tools:
- Computer with Arduino IDE installed
- A-B USB cord

http://www.instructables.com/id/Simple-2-way-motor-control-for-the-arduino/
Step 2: Connecting the motor
Connect your DC motor to your arduino.

- Connect one wire from the motor to pin 5 on you arduino


- Connect the other wire from your motor to pin 6 on your arduino

The hardware setup for this is pretty simple.

Step 3: Coding the Arduino


Here's some basic steps to program your arduino.

1) Download the source code from below


2) Open the file in the Arduino IDE
3) Press the "Upload to I/O Board" button
4) Once the program is uploaded it will start running

I tried to add a good amount of comments to the code, but if you have any questions, please ask them.

http://www.instructables.com/id/Simple-2-way-motor-control-for-the-arduino/
Image Notes
1. Upload to I/O Board Button

File Downloads

_2Way_Motor_Control.pde (1 KB)
[NOTE: When saving, if you see .tmp as the file ext, rename it to '_2Way_Motor_Control.pde']
Step 4: How this works & more Ideas
How This Works:
When you make one pin HIGH & another LOW, the pin that is HIGH has a positive voltage & the pin that is LOW works like a ground(-).

Note : The rotation direction maybe different than below.

Pin 5: Pin 6: Rotates: HIGHLOWCCWLOWHIGHCWPWM: 127LOWCCW 1/2 Speed


CCW = Counter Clockwise
CW = Clockwise

Ideas:
Add a second motor. Sorry I don't have any code for this, But there is a schematic posted below.

Related Instructables

Brain- Simple Robotics


Arduino Tutorial
Controlled Controlling an Bundle Breadboard by
Wheelchair by Arduino with .:Arduino doctek
Cocoa (Mac OS CRANE GAME Serial
jerkey Experimentation Controlled
X) or C# by marc.cryan Kit:. (ARDX) by Variable Speed
(Windows) by
oomlout Motor by westfw
http://www.instructables.com/id/Simple-2-way-motor-control-for-the-arduino/
computergeek

Comments
18 comments Add Comment

shortcircuitaz says: Feb 16, 2011. 12:41 PM REPLY


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 REPLY


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 REPLY


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 REPLY


OR! you could just buy a pwm motor controller and drive FLIPPEN HUGE MOTORS!

the_burrito_master says: Sep 21, 2010. 10:28 PM REPLY


Try a couple transistors as amplifiers.

beehard44 says: Oct 24, 2010. 7:53 AM REPLY


or relay?

the_burrito_master says: Oct 24, 2010. 8:12 AM REPLY


uh yeas.

Chowmix12 says: Jun 29, 2010. 8:07 PM REPLY


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 REPLY


cool.

daltore says: Nov 27, 2009. 5:20 PM REPLY


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.

Eonir says: Nov 2, 2009. 4:12 AM REPLY


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 says: Nov 2, 2009. 4:14 AM REPLY


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.

http://www.instructables.com/id/Simple-2-way-motor-control-for-the-arduino/
Bongmaster says: Oct 31, 2009. 5:56 PM REPLY
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 says: Oct 31, 2009. 6:10 PM REPLY


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 says: Oct 31, 2009. 7:41 PM REPLY


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 says: Nov 1, 2009. 5:14 AM REPLY


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 says: Nov 1, 2009. 7:35 AM REPLY


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 says: Nov 1, 2009. 7:30 PM REPLY


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.

http://www.instructables.com/id/Simple-2-way-motor-control-for-the-arduino/

You might also like