Code for basic L293d H-bridge motor control

Is there any reason this code wouldn't make a DC motor go one direction when a button was pressed and then the other direction if not?

const int controlPin1 = 2;
const int controlPin2 = 3;
const int enablePin = 9;
const int onSwitch = 5;
void setup()
{
pinMode(onSwitch, INPUT);
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, HIGH);
}

void loop()
{
if(digitalRead(onSwitch == HIGH))
{
digitalWrite(controlPin1, HIGH);
digitalWrite(controlPin2, LOW);
}
else
{
digitalWrite(controlPin1, LOW);
digitalWrite(controlPin2, HIGH);

}
}

Looks ok assuming those control pins of yours go to the right pins on the 293 so let's see the circuit: do you have grounds?, the 293's EN pin high?, and have you got VCC2?

edit.... also you should have a pullup or pulldown resistor on the switch to make sure its state is well known. (INPUT_PULLUP is easiest way of doing that.)

Hopefully these pictures will be good enough for you to see what's going on.

Ok well first I'm putting them inline so others can see without downloading...

Huntechr1:
Hopefully these pictures will be good enough for you to see what's going on.

And the answer is?.... no.

Circuit diagram please.

This should be the diagram. Sorry it has to be downloaded but, honestly I'm not sure how to get it to just post the image.

Ok let's have a look...

To post as image you need to "cheat". Attach as you did, post, right click the attachment and save the address. Then edit the post, or do a new one, and use the image icon (next to the chain link) and then use the saved address in there....

Hmmm that looks ok, so maybe there's a discrepancy between the diagram and real life, but I can't tell from those photos.

Have you measured the voltages at the 293? You have 5V on the logic supply and EN, 9V on motor supply, 5V or 0V on the motor controls as expected? You getting 9V and 0V out as expected? Checked the gnd continuity?

Does motor run in either direction btw?

Does motor run when wired straight to battery?

I don't currently have instant access to a multimeter so I can't test those things right now. The motor will run very briefly when attached directly to the 9 volt battery. When I connect the Arduino to the computer and 9 volt battery the motor isn't spinning at all.

Well it would appear that my battery must have been almost dead and I'm now very sad that that was my problem. However, I have encountered another problem because the motor won't switch directions when I press the button.

Hum, 9 volt battery.

How is the breadboard 10k connected?
.

It's attached to ground coming off of the switch. You can see it some in the middle picture.

Easiest way to use a switch is to use the internal pullups. Ditch the external resistor, and wire the switch one side to the digital pin, other side to ground. Use INPUT_PULLUP in the pinMode. The logic is reversed since a push is now low whereas before it was high, but that's no biggy.

Huntechr1:
I don't currently have instant access to a multimeter

You should have one in each room of the house and one in the car and one in your lunch box.... never know when you'll need one :slight_smile:

I think that will be my next purchase! So removing the external resistor helped because pushing the button will now stop the motor but, it doesn't change the direction should something be added to the code?

You should have one in each room of the house and one in the car and one in your lunch box.... never know when you'll need one :slight_smile:

Maybe you can get one for your birthday.

You can make a, go no go, tester with a LED and resistor until you get a DVM.

.

DONT remove the resistor unless you enable INPUT_PULLUP since pushing the switch will now short 5V to Gnd in the Arduino

I did change the code to "pinMode(onSwitch, INPUT_PULLUP)" but it didn't allow the motor to change directions. The wire to ground should be placed in the same place that the resistor was correct?

Also, pressing the button now makes the arduino lights turn off which seems like it is probably shorting out like you said so I'm going to assume I did something wrong.