WARNING: This is not a safe way to control a motor. Each I/O pin can only handle 40 mA of current. I would recommend using a H-Bridge or something along those lines.

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.

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

Step 1: The stuff you need:

- Arduino
- Small DC motor
- Wire for motor

- Computer with Arduino IDE installed
- A-B USB cord
<p>pls send us the code</p>
<p>WTF!!!!!!</p><p>Inductive loads like relays or motors NEVER should be connected directly to arduino. For inductive loads you need a transistor and a flywheel diode.</p>
<p>BEGINNERS READ: Do NOT try this out, there is no transistor, no protecting diode in this circuit. Your chance to ruin your MCU is very high with this. Author: Please delete this pages.</p>
<p>Is there any way to do this without ruining the MCU?</p>
<p>The warning you have put should be more bold and bright. This shall surely fry the MCU in long run. Better would be to remove the article or post a better one with safety precautions to the MCU.</p>
<p><strong>ATTENTION! Before frying something...read this:</strong></p><p>http://arduino.stackexchange.com/questions/11657/how-does-this-motor-circuit-not-ruin-the-microcontroller</p>
<p>This is not a stepper motor, right? Can Darligton pair be used instead of H-bridge?</p>
<p>Okay, first off, I'm really not trying to be mean or anything. That said, could you please take this instructable down for the sake of very beginners? I believe it would be really frustrating and for some people even spirit crushing to fry their first arduino. This is a really bad idea in so many ways.The ultimate hardware killer.</p><p>It would be very kind if you would either take it down or change it a little bit and make it better :) (This will help: http://embedded-lab.com/blog/wp-content/uploads/20...</p>
<p>Saying that this is not the safest way to control a motor is putting it mildly. I would recommend NOT to do this. The 40 mA is a story in itself (in short: it's less!), but even smaller motors will ultimately damage the I/O pins. Even if it seems to work perfectly. If you don't want to damage your Arduino, use an H-bridge.</p>
<p>im using arduino with atmega 328p-pu</p><p>motor is not runing. wat isue can be ?</p>
<p>The motor is not running because the motor is using to much current from the arduino. You can damage your arduino because the pins can short out. I don't recomend this method for you.</p>
<p>the out current is about 50mA, it is not enough to load the motor. I have a tips that you can use 2 pin mode as an output, and those connect to one pole of the motor, so it can double the current :D</p>
<p>@kpnd.dang - </p><p>NEVER do this when you're using digitalWrite and esp. analogWrite to <br>control the pins! Between two Writes the connected pins are at a different level <br>and the pins are short-circuited.</p>
<p>yes maybe it works but I would recommend to drive it using transistors and protection diodes, because even if you double the current for the motor, when the motor stop it will generate too much current in reverse on your IC(whatever is) pin, frying it =D</p><p>check this schem =D </p><p>http://www.thebox.myzen.co.uk/Workshop/Motors_1_files/shapeimage_2.png</p>
<p>Hi computergeek,</p><p>could you explain what the rotateright/rotateleft values indicate or used for (are those timers or voltages ?)</p>
<p>the code saves as a tmp file on my computer for some reason how do i fix it or can i just get a picture of the code</p>
<p>Just go to the property of the file and make one necessary change................................i.e., change the extension from .tmp to .pde and open it in IDE........................it worked for me in the same way!!!!!!!!!!</p>
<p>I tried and it worked for me............................I used Arduino UNO and a DC Motor...........................</p>
<p>Hi,</p><p>Is there an one way version of this code?</p><p>Btw this two way one didnt work. for some reason my motor wouldnt run eventhough it is a 3V 0,13A motor and I know it works fine when I attach it to my ground and 5v pins.</p>
<p>I tried this method on AD mega 2560, I also use Transistor and diode but its not working as its mentioned here. Motors only work CW. on LOW one motor stops and other still running. but I need two way control. any idea ? so I can move my stuff farward and backword </p>
<p>Yass bum nipple</p>
<p>Yass bum nipple</p>
<p>Yass bum nipple</p>
<p>Yass bum nipple</p>
<p>Yass bum nipple</p>
<p>Yass bum nipple</p>
<p>What you are doing is wrong. The motor will obviously drain more than 40mA from the Arduino board, damaging it badly. Why don't you try using traditional combination of transistor and drain (diode 1N4007)?</p>
<p>How? =D I'm working on a nanoquadrotor drone, and I have to use 4 dc motors, at first I think I could use 4 555 timer's, what do you suggest?</p><p>Thanks in advance =D</p>
<p>Maybe you need to use rc motors with speed controller.I want to make this project too but only tricopter with leds when turn right and left and more cool things.</p>
<p>I've tried with an npn and a protecting diode and it works well(if you'r using 3.7v micromotors, if you want to drive larger motors I think a mosfet will be better, or you can directly buy a cheap esc, but I think it's more fun to project and assemble from parts rather than buying it =D)</p>
<p>Yes it's more fun to project and assamble from parts rather than buying it </p><p>But i need to buy the ESC because the motors dont get enoung energy from arduino,and i put only the signal.And the controls....well i use visual studio to make one cool control system, now i'm working on xbox360 control.</p><p>If you want the visual studio projectfiles i give it:D. I like to help people :D</p><p>If i can help you with coding or something tell me!</p>
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?
<p>no just use bigger battery</p>
<p>hello, im working on a project where we control a miniature house via PS3 cotroller (preferably bluetooth connection). the house has 2 servo motors, for a door and a window. one Dc motor (fan) and LED lights. its my first time to use arduino and this type of circuits. help would be great. </p>
Hi sounds like a cool project, what do need help with?
<p>Worked great.</p>
<p>hello,,i need this cod ..pleas </p>
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.
<p>u probly already figured this but just change the tmp to pde works fine</p>
<p>i can't understand the instruction [rotateleft(150,500)] plz any one answer me .... i have project </p>
<p>i can't understand the instruction [rotateleft(150,500)] plz any one answer me .... i have project </p>
<p>hello, i'd want to know if it's possible to control two 48 lbs trolling motors with the apm 2.6, i was looking for an ESC from VANTEC but the one that would work for this proyect is targeted for 12-48 vdc systems, i'm using LiPo batteries and the ESC is the following: <a rel="nofollow"><strong>Vantec RDFR47 E</strong></a></p><p>Targeted for 12 - 48 VDC SYSTEMS.<br>VOLTAGE RANGE: 9 - 55<br>Continuous Amps: 75A Con't; 220A Starting Surge<br>SIZE: 6.25 X 2.3 X 4.5&quot;<br>1/4&quot; Thick sustantial aluminum U-extrusion</p><p>Wt. 43 oz </p>
<p>that I can do to convert 2 PWM inputs (rc servo) to 5Vdc analog?</p>
my motor runs when hooked up to a 1.5v battery, yet when I run this code hooked up to my laptop, I get nothing. Why?
Hi, <br> I tried your code to run a 12 V dc motor, and it doesnt work. I just hear beeping from motor, <br>it is connected directly to computer using arduino uno. <br>I am a newbie! so I really appreciate your help
the arduino is 5Volt, yr moor is 12 Volt. I am sure you can do the math on that yourself :-)
Good Tutorial,
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?
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 &gt; 1000 Ohms could easily kill your Arduino. <br>The Code should work just fine, but you might consider using an H-Bridge for controlling current.

About This Instructable




More by computergeek:DIY Apple Remote (Slideshow for the instructable) DIY Apple Remote Shield for the Arduino Simple 2-way motor control for the arduino 
Add instructable to: