Hi everyone, I'm attempting to interface a motor with the Arduino using the L293D driver and have it work where when I push button 1 it turns in one direction and if button 2 is pressed the motor turns in the opposite direction. The driver is connected correctly but when I run my code the motor turns great in one direction, but not at all in the other direction. I can feel the motor "wanting" to turn (the motor is tested good in both directions).
Furthermore when I model the circuit at 123D Circuits the motor turns very fast in one direction, and very slowly in the other. Is there something in my code that would cause this behavior? Thanks for any insight (I'm still very much developing as a programmer).
int input1=1; //connected to the input pin 1 on the motor driver
int input2=2; //connected to the input pin 2 on the motor driver
int button=5;
int buttonState=0;
int button2=7;
int buttonState2=0;;
void setup() {
pinMode(input1,OUTPUT);
pinMode(input2,OUTPUT);
pinMode(button,INPUT);
pinMode(button2,INPUT);
}
void loop() {
buttonState=digitalRead(button);
buttonState2=digitalRead(button2);
if (buttonState == HIGH && buttonState2==LOW)
{
digitalWrite(input1, HIGH);
digitalWrite(input2,LOW);
}
if (buttonState == LOW && buttonState2==HIGH)
{
digitalWrite(input1, LOW);
digitalWrite(input2,HIGH);
}
else
{
digitalWrite(input1, HIGH);
digitalWrite(input2,HIGH);
}
}
For an informed response, please post a wiring diagram (hand drawn, not Fritzing) and links to the product pages for the motor and the motor power supply.
Thanks for the suggestion remington. Attached is a pictorial diagram of how the circuit is connected. I'm using 130 Size Hobby Motors along with these l293 Drivers.
if (buttonState == HIGH && buttonState2 == LOW)
{
// do Thing A
}
else if (buttonState == LOW && buttonState2 == HIGH)
{
// do Thing B
}
else
{
// do Thing C
}
instead.. This way you could tie all if sentences as group (I don't know correct term).. In your code only these are tied together
if (buttonState == LOW && buttonState2 == HIGH)
{
// do Thing B
}
else
{
// do Thing C
}
I haven't tried does it solve the problem but by writing as I did, you can be sure that only one if sentence will run per cycle..
Hint: You could use Uno's pull-up resistors by using
but then you have LOW-active buttons but you could get rid of external resistors..
The code will be like this
int input1 = 1; //connected to the input pin 1 on the motor driver
int input2 = 2; //connected to the input pin 2 on the motor driver
int button = 5; // you could use const int instead of int
int buttonState = 0; // you could use boolean instead of int
int button2 = 7; // you could use const int instead of int
int buttonState2 = 0; // you could use boolean instead of int
void setup()
{
Serial.begin(9600); // for debugging
pinMode(input1, OUTPUT);
pinMode(input2, OUTPUT);
pinMode(button, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
}
void loop()
{
buttonState = digitalRead(button);
buttonState2 = digitalRead(button2);
if (buttonState == HIGH && buttonState2 == LOW)
{
Serial.println("if1"); // for debugging like this for example
digitalWrite(input1, HIGH);
digitalWrite(input2, LOW);
}
else if (buttonState == LOW && buttonState2 == HIGH)
{
digitalWrite(input1, LOW);
digitalWrite(input2, HIGH);
}
else if (buttonState == HIGH && buttonState2 == HIGH) // or just else
{
digitalWrite(input1, HIGH);
digitalWrite(input2, HIGH);
}
}
You could also debug your code while it's running by using Serial.print() inside your if-sentences. It's easy way to find out if your code will run more than one if per cycle..
Thanks! The "else if" structure solves the problem in my simulation, so hopefully it will also do the trick in my real circuit. Thank you for taking a look and pointing me towards the else if resource. I will also try and use the pull up resistors to simplify my circuit.
greisinb:
Thanks! The "else if" structure solves the problem in my simulation, so hopefully it will also do the trick in my real circuit. Thank you for taking a look and pointing me towards the else if resource. I will also try and use the pull up resistors to simplify my circuit.
No worries It seems that it tried to turn motor both ways by pressing button 2..
Btw hint no 2: Purchase DC power supply as soon as possible if you'll continue with this hobby. In the future you'll try little bit bigger dc motors and might fry your Arduino by trying to power those motor thru Arduino.. That one sucks 450mA but by adding another one and driving it simultaneously the limit is quite near or even over..