I am trying to reverse a motor- but not able to do it

i attached circuit diagram and code what i am using… motor is rotating only in one direction, it is not reversing

bi_direction_mmotor.ino (523 Bytes)

Please post the code here to avoid the need to download it. When you do, please select the code in the forum editor and click on the icon far left above the editor window (</>) to insert code tags.

Just post the code - don't make people download it.

int enablePin = 11; 
int in1Pin = 9; 
int in2Pin = 10; 
int switchPin = 7; 
int potPin = 0;
void setup()
  pinMode(in1Pin, OUTPUT);  
  pinMode(in2Pin, OUTPUT);  
  pinMode(enablePin, OUTPUT);  
  pinMode(switchPin, INPUT_PULLUP); 
void loop()
{  //int speed = analogRead(potPin) / 4; 
   boolean reverse = digitalRead(switchPin);  
   setMotor(255, reverse); 
void setMotor(int speed, boolean reverse) 
  analogWrite(enablePin, speed);  
  digitalWrite(in1Pin,! reverse);  
  digitalWrite(in2Pin, reverse); 
  pinMode(in1Pin, OUTPUT);  
  pinMode(in2Pin, OUTPUT);

Does it really make sense to use "in" in the name of an out pin?

Why is the potentiometer read commented out?

First - test to see if the chip is ok:

  1. Disconnect all power.

  2. Remove from the Arduino the wires going to the Arduino pins 9, 10, and 11.

  3. Hook the wire from the L293 pin 1 to ground.

  4. Hook the wire from the L293 pin 2 to ground.

  5. Hook the wire from the L293 pin 7 to +V (5 volts).

  6. Re-connect the power.

  7. The motor should spin one way.

  8. Disconnect the power.

  9. Move the wire on pin 1 of the L293 from ground to +V (5 volts).

  10. The motor should spin the other way.

If the motor doesn't change direction (or doesn't turn at all until you get to steps 9 and 10) then your L293 is bad; you might try the other motor driver to see if it works - or get a new L293.

If that test works, then it may be your Arduino that is the problem; try hooking up LEDs (with a series resistor) to the output pins, and see how the LEDs light up - pin 11 of the Arduino should be a solid HIGH (LED on), while pins 9 and 10 should alternate, depending on if the button is pressed. If they don't alternate, then either there is something wrong with pins 9 or 10, or there is something wrong with the pins the button is connected to.

Another thing: If you are using one of those push-buttons like is shown in your diagram, sometimes the "legs" of the button won't touch the connectors in the breadboard (or will be intermittent) - verify that the button actually works (isn't broken) - and that it makes contact with the pins in the breadboard.

Lastly - try increasing the voltage going to pin 8 of the chip (pin 8 is the power supply for the motor outputs, pin 16 is the power supply for the logic of the L293); make sure it is a couple of volts higher than the voltage needed to run the motor (because the L293 is a bipolar transistor logic chip - it will drop some voltage across the outputs) - but DON'T increase the voltage going to pin 16 (it should always be 5 volts).

Also remember that this chip works best with motors rated for 6 volts or above; so your voltage at pin 8 should be around 8 volts. Attach a separate battery (or other power source) of that voltage to pin 8 and ground, and tie your grounds together so that the Arduino shares the ground.

Then re-run your tests.

I'm not seeing anything major with the code (as AWOL posted) - so I don't think it is a software issue...

No decoupling on the L293D, sharing motor power with Arduino 5V rail - both of these are
problems needing fixing.

Is this an L293 or an L293D ?