Press button 1 = motor clockwise, press button 2 = motor anti-clockwise

Hi,

I’m completely new to Arduino’s. I’ve done a few of the tutorials over on TinkerCAD and thought things were going well…

I have a very basic system whereby i’m using an L293D h-bridge with two push buttons and a motor. The idea is when I press and hold the first button, the motor will turn anti clockwise; release the button and the motor stops spinning. Press the other button and it turns anticlockwise etc…

I’ve attached a screenshot of the circuit (resistors = 10kOhm). Here is the code (i’m using ‘raise’ and ‘lower’ as this will eventually be used to control a vertically mounted linear actuator):

// define constants
const int RaiseButton = 13; //The button to raise the actuator is wired to pin 13
const int LowerButton = 11; //The button to lower the actuator is wired to pin 11
const int ClockwisePin = 4; //Input 1 into H-Bridge is wired to pin 4
const int AntiClockPin = 2; //Input 2 into H-Bridge is wired to pin 2
const int enablePin = 7;

// setup
void setup() {
pinMode(RaiseButton, INPUT); //Sets the raise button as an input
pinMode(LowerButton, INPUT); //Sets the lower button as an input
pinMode(ClockwisePin, OUTPUT); //Sets the input to the H-Bridge as an output
pinMode(AntiClockPin, OUTPUT); //Sets the input to the H-Bridge as an output
pinMode(enablePin, OUTPUT);
}

// loop
void loop() {
if (RaiseButton == HIGH) { //If the raise button is pressed,
digitalWrite(enablePin, HIGH); //then turn the motor on
digitalWrite(ClockwisePin, HIGH); //clockwise
digitalWrite(AntiClockPin, LOW);
}

else if (LowerButton == HIGH) { //if the lower button is pressed
digitalWrite(enablePin, HIGH); //then turn the motor on
digitalWrite(ClockwisePin, LOW); //anticlockwise
digitalWrite(AntiClockPin, HIGH);
}
}
//end of loop

As it stands, running the simulation and pressing either of the buttons does not turn the motor. Cannot figure out where i’m going wrong.

Any guidance would be much appreciated.

Cheers
Carl

May help to know what motor your using.

Also, edit your post and swap out the quote tags with code tags.

The main part left out of you program is failure to actually READ the pin in to the constant you are checking.

Paul

Paul_KD7HB:
The main part left out of you program is failure to actually READ the pin in to the constant you are checking.

Paul

Ok, i amended the code but still no luck. I think I interpreted your advice correctly?

// loop
void loop() {
  digitalRead(RaiseButton);
  digitalRead(LowerButton);
  if (RaiseButton == HIGH) {//If the raise button is pressed,
    digitalWrite(enablePin, HIGH); //then turn the motor on
    digitalWrite(ClockwisePin, HIGH); //clockwise
    digitalWrite(AntiClockPin, LOW);
  }
  
  if (LowerButton == HIGH) { //if the lower button is pressed
    digitalWrite(enablePin, HIGH); //then turn the motor on
    digitalWrite(ClockwisePin, LOW); //anticlockwise
    digitalWrite(AntiClockPin, HIGH);
  }
}
//end of loop

DangerToMyself:
May help to know what motor your using.

Also, edit your post and swap out the quote tags with code tags.

Sorry, at the moment it's just in simulated form. I've attached a print screen of the circuit to my original post. The code will eventually be used to control a linear actuator.

From the tutorial:

void loop()
{
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button's value
}

Paul

Looking at your Fritz, looks like buttons are switching high side but you have pull UP resistors, so the input pins will always read HIGH whether the buttons are pressed or not.

  digitalRead(RaiseButton);
  ....
  if (RaiseButton == HIGH)

Read the pin and chuck away the value.
Then compare the pin number to HIGH.

Don't do it.

Try:

if (digitalRead(RaiseButton) == HIGH) {//If the raise button is pressed,

But change the resistors to pull DOWN(from input pin to GND).

Paul_KD7HB:
From the tutorial:

void loop()
{
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button’s value
}

Paul

Ok, i’ve amended the code as shown below however the motor spins regardless of which button is being pressed. Also, the motor behaviour seems to depend on whether i define button states at the start as a global variable or within the loop as local (i.e. spins approx 16000rpm if defined as global, but approx 8000rpm if local), which I didn’t expect.

// define constants
const int RaiseButton = 13; //The button to raise the actuator is wired to pin 13
const int LowerButton = 11; //The button to lower the actuator is wired to pin 11
const int ClockwisePin = 4; //The input 3 into H-Bridge is wired to pin 4
const int AntiClockPin = 2; //The input 4 into H-Bridge is wired to pin 2
const int enablePin = 7;
int RaiseButtonState = digitalRead(RaiseButton); //Read and store the state of the Raise Button
int LowerButtonState = digitalRead(LowerButton); //Read and store the state of the Lower Button

// setup
void setup() {
  pinMode(RaiseButton, INPUT); //Sets the raise button as an input
  pinMode(LowerButton, INPUT); //Sets the lower button as an input
  pinMode(ClockwisePin, OUTPUT); //Sets the input to the H-Bridge as an output
  pinMode(AntiClockPin, OUTPUT); //Sets the input to the H-Bridge as an output
  pinMode(enablePin, OUTPUT);
}

// loop
void loop() {
  if (RaiseButtonState == HIGH){ //If the raise button is pressed,
  	digitalWrite(enablePin, HIGH); //then turn the motor on
 	digitalWrite(ClockwisePin, HIGH); //clockwise
 	digitalWrite(AntiClockPin, LOW);
  }

  if (LowerButtonState == HIGH){ //If the lower button is pressed,
	digitalWrite(enablePin, HIGH); //then turn the motor on
	digitalWrite(ClockwisePin, LOW); //anti clockwise
	digitalWrite(AntiClockPin, HIGH);
  }
}
//end of loop

edgemoron:
Looking at your Fritz, looks like buttons are switching high side but you have pull UP resistors, so the input pins will always read HIGH whether the buttons are pressed or not.

My understanding of pull up resistors is that when button is not pressed, the input reads HIGH and when pressed the input reads LOW. Based on this i tried changing the if statement within the loop to check for LOW state rather than high, however the motor still continues to spin without either button pressed (see previous post for rest of code):

// loop
void loop() {
  if (RaiseButtonState == LOW && LowerButtonState == HIGH){ //If the raise button is pressed and lower button isnt,
   digitalWrite(enablePin, HIGH); //then turn the motor on
 digitalWrite(ClockwisePin, HIGH); //clockwise
 digitalWrite(AntiClockPin, LOW);
  }
  
  if (LowerButtonState == LOW && RaiseButtonState == HIGH){ //If the lower button is pressed and raise button isnt
 digitalWrite(enablePin, HIGH); //then turn the motor on
 digitalWrite(ClockwisePin, LOW); //anti clockwise
 digitalWrite(AntiClockPin, HIGH);
  }
}
//end of loop

Just realised i had my pull up resistors wired to the 9V line as opposed to ground. This is going to be a long journey isn't it haha. Seems to be working fine now, although motor doesn't stop when buttons are released. I'm sure i can figure that out though. Thanks for the input everyone, still used all tips to make it work.

Final code (feel free to critique further):

// define constants
const int RaiseButton = 13; //The button to raise the actuator is wired to pin 13
const int LowerButton = 11; //The button to lower the actuator is wired to pin 11
const int ClockwisePin = 4; //The input 3 into H-Bridge is wired to pin 4
const int AntiClockPin = 2; //The input 4 into H-Bridge is wired to pin 2
const int enablePin = 7;


// setup
void setup() {
  pinMode(RaiseButton, INPUT); //Sets the raise button as an input
  pinMode(LowerButton, INPUT); //Sets the lower button as an input
  pinMode(ClockwisePin, OUTPUT); //Sets the input to the H-Bridge as an output
  pinMode(AntiClockPin, OUTPUT); //Sets the input to the H-Bridge as an output
  pinMode(enablePin, OUTPUT);
}

// loop
void loop() {
  int RaiseButtonState = digitalRead(RaiseButton); //Read and store the state of the Raise Button
  int LowerButtonState = digitalRead(LowerButton); //Read and store the state of the Lower Button
  if (RaiseButtonState == LOW && LowerButtonState == HIGH){ //If the raise button is pressed and lower button isnt,
  	digitalWrite(enablePin, HIGH); //then turn the motor on
 	digitalWrite(ClockwisePin, HIGH); //clockwise
 	digitalWrite(AntiClockPin, LOW);
  }
  
  if (LowerButtonState == LOW && RaiseButtonState == HIGH){ //If the lower button is pressed and raise button isnt
	digitalWrite(enablePin, HIGH); //then turn the motor on
	digitalWrite(ClockwisePin, LOW); //anti clockwise
	digitalWrite(AntiClockPin, HIGH);
  }
}
//end of loop