Picture of 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!

Remove these adsRemove these ads by Signing Up

Step 1: The stuff you need:

Picture of The stuff you need:
- Arduino
- Small DC motor
- Wire for motor

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

Step 2: Connecting the motor

Picture of 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.
1-40 of 46Next »
daaryancode2 months ago

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)?

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?

Thanks in advance =D

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.

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.

Hi sounds like a cool project, what do need help with?
Tischer2 months ago

Worked great.

hello,,i need this cod ..pleas

asarris1 year ago
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.
rikovgy asarris3 months ago

u probly already figured this but just change the tmp to pde works fine

i can't understand the instruction [rotateleft(150,500)] plz any one answer me .... i have project

i can't understand the instruction [rotateleft(150,500)] plz any one answer me .... i have project

danimalik8 months ago

im using arduino with atmega 328p-pu

motor is not runing. wat isue can be ?

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.

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

tonyGV6 months ago

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: Vantec RDFR47 E

Targeted for 12 - 48 VDC SYSTEMS.
Continuous Amps: 75A Con't; 220A Starting Surge
SIZE: 6.25 X 2.3 X 4.5"
1/4" Thick sustantial aluminum U-extrusion

Wt. 43 oz

racataca10 months ago

that I can do to convert 2 PWM inputs (rc servo) to 5Vdc analog?

mfusaro1 year ago
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?
shanymz1 year ago
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
the arduino is 5Volt, yr moor is 12 Volt. I am sure you can do the math on that yourself :-)
gee122 years ago
Good Tutorial,
msurguy2 years ago
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 msurguy2 years ago
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 Lenny242 years ago
Thanks ! I might use TI DRV883x series chip for the H Bridge driver then!
acain32 years ago
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.
vkarpuram12 years ago
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 -
Your link popped up containing Malware.
David972 years ago
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?
Eonir5 years ago
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 Eonir5 years ago
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.
Or just slowly ramp the motor speed up and down
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?
wfelix3 years ago
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
GitarGr84 years ago
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:
(scroll down to protection diode)
OR! you could just buy a pwm motor controller and drive FLIPPEN HUGE MOTORS!
Try a couple transistors as amplifiers.
or relay?
uh yeas.
Chowmix124 years ago
check out STM, they have free samples for an l298HN IC, which is an ic that incorporates 2 h bridges inside the IC.
BrunoG4 years ago
daltore5 years ago
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.
1-40 of 46Next »