Easy, Reversible Motor Control for Arduino (or any Microcontroller) by nothinglabs
Featured

Step 13: Upload the Code and Test

prog.jpg
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);

 
Remove these adsRemove these ads by Signing Up
paseanlysan says: Aug 2, 2012. 11:40 AM
this is an awesome project!! but i CANNOT get the motor to run in reverse for the life of me...i am 100% sure i have the schematic right and it built right... i am 100% sure my code is correct and im even using a 12 volt battery... it will do forward and forward with pwm but NOT backward... its killing me.. please help!! i spent like 20 bucks getting parts and id hate to go and find another solution to this
nothinglabs (author) says: Aug 2, 2012. 1:40 PM
are you using the exact relay specified?

what kind of 12v battery are you using?
DNMEBOY says: Aug 25, 2011. 2:31 PM
I made this and it seems to be working correctly but my motor does not go backwards. Any ideas?
stupidloginthing says: Dec 17, 2011. 2:09 PM
funny. I'm using a 10 volt battery and using this circuit for a stepper motor from a drill intended for 7.2 V. It won't go backwards either. (I set the PWM power control to half (128) so that it's not too hard on the motor.)

I get the feeling something is fundamentally wrong because no power comes through to the motor when it's supposed to be running in reverse.

any ideas?
nothinglabs (author) says: Dec 19, 2011. 11:46 AM
a very common cause of this problem is a battery that's not producing enough voltage to fully switch the relay. 10v should be enough - is it possible the battery isn't fully charged? (it may also be that the voltage sags to under 7.5v when powering the motor).

this might sound really generic - but I would triple-check all the wiriing.

is it possible anything is backwards / upside down?

I'm assuming you're not really using a stepper motor - but some kind of geared DC motor (stepper motors require very special drivers).

Good luck - let me know how it goes.

-RIch
stupidloginthing says: Dec 20, 2011. 10:28 AM
yeah its not a stepper motor it just behaves strangely. I think that's likely because we had an AC-DC power source before that we set at 12V and it worked fine then. the battery was producing 10.8V, which wasn't quite enough to reverse it, then later it didn't make a sound and wouldn't reverse (as battery drained).

anyways I ordered a motor controller because I'll need to use servos too. this was a good experiment and got us an A on our class project, so thank you.
DNMEBOY says: Aug 25, 2011. 3:02 PM
Nevermind, I was using a 7.4v lipo to power this project, I had the thought that the voltage may not be enough so I connect a 12v Lead Acid battery and it works exactly as it should. So if anyone else runs into the same problem chances are its your input voltage being to low. I wish I still had some 3s 11.1v lipos. The lead acid battery is just BIG.
nothinglabs (author) says: Aug 25, 2011. 3:49 PM
that'll do it! 7.4v is right on the edge (wouldn't surprise me if it worked on a full charge).

if you poke around online - you should be able to find some relays that would work fine at lower voltages.

glad you got it working!

-Rich
DNMEBOY says: Sep 6, 2011. 2:12 PM
I disconnected everything from my arduino after running a motor with this and now windows doesnt even see my arduino uno, nothing in device manager, any ideas? im pretty bummed since i just ordered that motor sheild.
nothinglabs (author) says: Sep 8, 2011. 2:28 AM
sorry you're having arduino probs. I don't think it's this project though - I've played around a fair amount with it without problems.

is it possible you connected your battery to VSS instead of VIN? that might do it.

I've found pushing the reset button just before programming can help it take an upload (shouldn't need to do this - but sometimes helps)

also - trying another USB port might help.

-Rich
DNMEBOY says: Sep 8, 2011. 11:08 AM
It seems that the 8u2 on my board has died. It's the only thing I can think of. It makes sense as the 8u2 is in charge is USB/serial communication. Which isn't working yet the arduino still runs your sketch. Your project isn't to blame I'm sure. Just that I was messing around with it when my arduino stopped working. I had the USB plugged in while running the motor. I could hear the arduino connecting and disconnecting from windows. Not sure what happened. The project was powered with a 12v lead acid battery. Positive to your motor controller and ground to the ground on the arduino as well as the ground from the motor controller to the arduino. My guess is that something was connected wrong but no different than the first time I did this project.

I only asked you because you seem to know more than me about this stuff and I was desperately trying to find the problem. The seller has agreed to ship me a new arduino provided I return the dead one. Going to be a long wait from hong kong.
nothinglabs (author) says: Sep 9, 2011. 1:13 AM
one thing that sometimes helps is re-flashing the arduino boot loader - but you have to have an AVR programmer to do that.

on the other hand - if you're not even seeing it as a serial port - that does sound like a hardware issue.

i will mentiin that while I've generally had good luck with clone arduino (pretty mcuh anything from asia) - I've maybe had a few more issues with them than the ones actually from italy.

good luck!

-Rich
DNMEBOY says: Sep 9, 2011. 5:09 AM
According to what I've learned if the led on pin 13 blinks on reset the bootloader is fine. The arduino still runs your sketch. Maybe if my replacement fails I'll purchase an arduino that's not a clone. I've got 3 kids so money is tight. If I can get a clone for 20 bucks on eBay with free shipping that's a great deal. I've also always been told that the only thing worth buying cheap is beer. So maybe a quality arduino would be worth the extra money.
DNMEBOY says: Sep 6, 2011. 11:44 AM
I found a 5v relay at radioshack but honestly I would have no clue how to wire it up and make it work like the one you made.

I went ahead and ordered a motor controller based on the L293D from ebay. Just waiting for it to arrive from hong kong...

Im planning on building an autonomous vehicle that uses skidstearing. So basically like a little tank. Ive got alot to learn. Right now im pretty much just finding tutorials like this and copy/pasting code. Ive yet to come up with anything on my own.
nothinglabs (author) says: Sep 8, 2011. 2:25 AM
I can't say for sure - but I suspect that 5v relay won't work for this project.

good luck with your robot!

copy/pasting is a pretty good way to learn stuff. I suspect a large amount of commercial software is developed like that...

-Rich
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!