Let's Make Robots!

Arduino Motor Control Code Help

I am currently using a arduino duemilanove, and it is awesome. I have been playing around with it for a couple of days now, and I really like it. Anyways I figured it was time to start to building a robot. I decided to start with the motors. For the motor control I decided to use guibot's tutorial. It worked fine, but the motors only traveled in one direction. Because I wanted the motors to also travel in reverse too I decided to experiment with my own code (on only one motor) that looks something like this:

int motorPin1 = 2;
int motorPin2 = 3;
void setup(){
pinMode (motorPin1, OUTPUT);
pinMode (motorPin2, OUTPUT);
void loop(){
  digitalWrite (motorPin1, HIGH);
  digitalWrite (motorPin2, HIGH);
 delay ;1000;
digitalWrite (motorPin1, LOW);
digitalWrite (motorPin2, HIGH);
delay ;1000;
digitalWrite (motorPin1, LOW);
digitalWrite (motorPin2, LOW);
delay ;1000;

Not suprisingly, it didn't work. I feel like there is something obvious that I'm missing. The motors go in one direction perfectly, but not in reverse. Can somebody help me to get these motors to also go in reverse?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.


NOTE: the "9V" pin on the board, under POWER is actually your VIN pin. For testing, you don't need the VIN or the regulator if you have the Arduino plugged into USB. (and make sure if you have the USB plugged in, to unplug the battery/supply, and vice versa.)

The lightning sign is your external supply.. be it batteries or a wallwart.

And I think I missed a capacitor on the regulator, but you'll be fine.

You can change the pins to any pins you would like, but you can use analogWrite(pinNumber, 0-255) to control the speeds on any pins marked PWM.


I hope this helps.. sorry if it's a little messy, have only played with Fritzing a few times. Best of luck, and let us know your results!

For anyone who doesn;t recognize the drawing, it was done on Fritzing.

Fritzing is a free breadboard drawing program for the Arduino. 

Thanks, that helps alot. I will go get a 5v regulator today . I will make that circuit today and tell you how it turns out. If I have problems with it I will post a picture of my circuit. Quick question though, when using the external supply do I also use power from usb/regular battery with it (for powering the board)? I think I do, but I'm not sure.

I'm a bit confused with your question, might be late.. but

If you're keeping the Arduino plugged into your computer, for testing code and such, the external supply should only be powering the motor driver. You won't need the 5v regulator.(but the ground of the motor driver circuit still connects to the ground of the Arduino)

 But if you've uploaded code, and you've unplugged the Arduino from your computer, then you'll want the Regulator, and you can either use a Usb connector and just power through USB (MUST be 5v, any higher and you'll fry your FTDI chip), or through VIN( I believe can be up to 9v, but best to stay around 5v as well).


If you have a spare wall-wart with the power-pin that's the same size of the arduinos, you could use that and hook up the supply to that. Use 1 supply for both without needing the regulator off the board. But you would have to "ruin" a wall-wart for the jack, but helpful if you use different supplies often and want it mobile.

Hope I didn't repeat myself too much, it's very late, so excuse anything that doesn't make sense!:D

Yes, you did answer my pathetically worded question :D. I made the above circuit except not using the pwm pins (still using digitalWrite). Now the motors run fine, but not in reverse. With the above code the motors will run then pause and go the same direction, instead of the opposite direction. It will run forward fine, but not in reverse. Any ideas?

Here's a quick test you can try:
• Make note of the direction that the motor does rotate correctly in
• Disconnect the motor from the motor driver, and then plug it back in with the two leads switched (change nothing else!)
• Fire up the circuit again, compare motor behaviour

If the motor now only turns in the opposite direction to the first case, then there is a problem around the motor driver and/or associated circuitry.
If the motor still only goes in the same direction as before, then the issue is with the motor itself, and there aren't that many things that can go wrong with a DC gearmotor so it's easy to narrow it down from there.

Yes, when the leads are switched it only turns in the opposite direction to the first case. I think I will try to post a picture of the circuit.
Good idea, maybe the picture will provide us with some clues. So... it's not the code, Cap'n certified that, the motor is fine, the circuit has proper power now, the enables are set... it'll be hard to test the parts further without a multimeter, but I wonder if the SN754410 has partly died?

Looking at the diagram on the drawing, it appears that both Enables and Vss and Vs are all getting 9 volts.

The enables and Vss should be 5 volts, while Vs, the motor supply, can be 9 volts.

With Vss, the logic supply, being at a higher voltage, it may be questionable if the chip can actually read a 5 volt TTL signal.

It probably has survived, just correct voltages should be applied. 

In the TI datasheet, pin 8 is actually called Vcc2 for motor supply while pin 16 is Vcc1 for logic supply.

Hey guys, sorry I've been gone so long.  After getting home I was about to take the picture when I saw the post from robologist. I tried it out and it worked like a charm. I now have a motor that runs forward and reverse. Thanks to everybody who helped. My robot is now on the way.