Motor that is connected to two bottons one to go clockwise and the other to go counterclockwise

I am working on this for a motor to move a ship for a project, but i cant seem to get it to work. I have left the code and tinkerCAD schematic to see what's going wrong

/*

*/

// define constants
const int FirstButton = 13; //The button to first the actuator is wired to pin 13
const int SecondButton = 11; //The button to second the actuator is wired to pin 11
const int ClockwisePin = 4; //The input 3 into H-Bridge is wired to pin 4
const int CounterClockwisePin = 2; //The input 4 into H-Bridge is wired to pin 2
const int enablePin = 7;


// setup
void setup() {
  pinMode(FirstButton, INPUT); //Sets the first button as an input
  pinMode(SecondButton, INPUT); //Sets the second button as an input
  pinMode(ClockwisePin, OUTPUT); //Sets the input to the H-Bridge as an output
  pinMode(CounterClockwisePin, OUTPUT); //Sets the input to the H-Bridge as an output
  pinMode(enablePin, OUTPUT);
}

// loop
void loop() {
  int FirstButtonState = digitalRead(FirstButton); //Read and store the state of the First Button
  int SecondButtonState = digitalRead(SecondButton); //Read and store the state of the Second Button
  if (FirstButtonState == LOW && SecondButtonState == HIGH){ //If the first button is pressed and second button isnt,
  	digitalWrite(enablePin, HIGH); //then turn the motor on
 	digitalWrite(ClockwisePin, HIGH); //clockwise
 	digitalWrite(CounterClockwisePin, LOW);
  }
  
  if (SecondButtonState == LOW && FirstButtonState == HIGH){ //If the second button is pressed and first button isnt
	digitalWrite(enablePin, HIGH); //then turn the motor on
	digitalWrite(ClockwisePin, LOW); //anti clockwise
	digitalWrite(CounterClockwisePin, HIGH);
  }
}
//end of loop

  • The 9V battery wire isn’t connected to the driver motor power pin.

  • Arduino pin D2 is not connected properly.

I honestly don't understand why you're just making your life more complicated. This can easily be done WITHOUT a microcontroller ONLY with the two buttons and the L293D driver. I hope you won't use such a 9 volt battery in your real project.

Why the Arduino? One DPDT switch and a NO push button would do the job.

how do you mean?

I mean that your program reads the state of the buttons and directly controls the input pins of the L293D driver. If you only do that you don't need a microcontroller. See, for example, Figure 10 with the Bidirectional DC motor schematic. You connect the EN pin to Vcc to enable the driver's outputs. Each of the pins 1A and 2A is connected with a button to Vcc and between the button and the pin you place a resistor to GND (pull-down). Then, if only one button is pressed, the motor starts to rotate in the corresponding direction, and when no button is pressed or both buttons are pressed at the same time, the motor stops quickly - see the table below the electrical diagram.

Maybe something like that.

Hello so i decided to switch it up to try a different way because it was not working at all. I removed the resistors, swiched the battery, and changed the code. It was working at first but then it stopped and I'm not sure what happened. Also the H-board sometime heats up randomly.

// declare variables
const int leftPin = 8;
const int rightPin = 12;
const int delayTime = 1000;
const int but1pin = 2;
const int but2pin = 3;
int but1;
int but2;

void setup(){
  pinMode(leftPin,OUTPUT);
  pinMode(rightPin,OUTPUT);
  pinMode(but1pin,INPUT_PULLUP);
  pinMode(but2pin,INPUT_PULLUP);
}

void loop(){
  but1 = digitalRead(but1pin);
  but2 = digitalRead(but2pin);
  
  if(but1 == HIGH){
    digitalWrite(leftPin,LOW);
  }
  else{
    digitalWrite(leftPin,HIGH);
  }
  if(but2 == HIGH){
    digitalWrite(rightPin,LOW);
  }
  else{
    digitalWrite(rightPin,HIGH);
  }
}
  • This implies something has changed.
    Try new wires.
    Connect the motor directly to the batteries to confirm if the motor and battery voltage are okay.

i did that. I also checked the breadboard. still nothing.

  • Do you have access to volt meter ?

  • Please show us good images of the actual wiring.

  • Not at the moment



  • In the future, suggest you use different coloured wires so people can more easily following the connections.

  • Header socket pins are designed for header plug pins.
    Using solid wires often lead to intermittent connections.

  • We will do some software checking of signal in the changes below.
    What do you see printed on the serial monitor when you close/open switches?


// declare variables
const int leftPin = 8;
const int rightPin = 12;
const int delayTime = 1000;
const int but1pin = 2;
const int but2pin = 3;
int but1;
int but2;

void setup(){
  Serial.begin(115200);  // set your serial monitor to match this speed.  <———<<<<

  pinMode(leftPin,OUTPUT);
  pinMode(rightPin,OUTPUT);
  pinMode(but1pin,INPUT_PULLUP);
  pinMode(but2pin,INPUT_PULLUP);
}

void loop(){
  but1 = digitalRead(but1pin);
  Serial.println(but1);

  but2 = digitalRead(but2pin);
  Serial.println(but2);

delay(250); // short delay for debugging 

  if(but1 == HIGH){
    digitalWrite(leftPin,LOW);
  }
  else{
    digitalWrite(leftPin,HIGH);
  }
  if(but2 == HIGH){
    digitalWrite(rightPin,LOW);
  }
  else{
    digitalWrite(rightPin,HIGH);
  }
}
  • I'm sorry these are the only wires i have left. I really need to get some more

  • When i press the serial monitor it just says connecting... and never connects

  • Did you change the serial monitors baud rate to 115200 ?

Your original code works. Check your battery power and your wiring.

You can verify your programming (with the following files) using WOKWI.COM.

click to view sketch.ino
/*

*/

// define constants
const int FirstButton = 13; //The button to first the actuator is wired to pin 13
const int SecondButton = 11; //The button to second the actuator is wired to pin 11
const int ClockwisePin = 4; //The input 3 into H-Bridge is wired to pin 4
const int CounterClockwisePin = 2; //The input 4 into H-Bridge is wired to pin 2
const int enablePin = 7;


// setup
void setup() {
  pinMode(FirstButton, INPUT); //Sets the first button as an input
  pinMode(SecondButton, INPUT); //Sets the second button as an input
  pinMode(ClockwisePin, OUTPUT); //Sets the input to the H-Bridge as an output
  pinMode(CounterClockwisePin, OUTPUT); //Sets the input to the H-Bridge as an output
  pinMode(enablePin, OUTPUT);
}

// loop
void loop() {
  int FirstButtonState = digitalRead(FirstButton); //Read and store the state of the First Button
  int SecondButtonState = digitalRead(SecondButton); //Read and store the state of the Second Button
  if (FirstButtonState == LOW && SecondButtonState == HIGH) { //If the first button is pressed and second button isnt,
    digitalWrite(enablePin, HIGH); //then turn the motor on
    digitalWrite(ClockwisePin, HIGH); //clockwise
    digitalWrite(CounterClockwisePin, LOW);
  }

  if (SecondButtonState == LOW && FirstButtonState == HIGH) { //If the second button is pressed and first button isnt
    digitalWrite(enablePin, HIGH); //then turn the motor on
    digitalWrite(ClockwisePin, LOW); //anti clockwise
    digitalWrite(CounterClockwisePin, HIGH);
  }
}
//end of loop
click to view diagram.json
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 14.4, "left": -0.5, "attrs": {} },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -39.6,
      "left": 131.4,
      "rotate": 90,
      "attrs": { "color": "green" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": -39.6,
      "left": 59,
      "rotate": 270,
      "attrs": { "color": "red", "flip": "1" }
    },
    {
      "type": "wokwi-pushbutton-6mm",
      "id": "btn1",
      "top": -59.8,
      "left": 57.6,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-pushbutton-6mm",
      "id": "btn2",
      "top": -59.8,
      "left": 134.4,
      "attrs": { "color": "green" }
    },
    {
      "type": "wokwi-led",
      "id": "led3",
      "top": -90,
      "left": 100.2,
      "attrs": { "color": "white", "flip": "1" }
    }
  ],
  "connections": [
    [ "nano:4", "led2:A", "green", [ "v0" ] ],
    [ "nano:2", "led1:A", "green", [ "v0" ] ],
    [ "nano:GND.2", "led1:C", "black", [ "v0" ] ],
    [ "nano:GND.2", "led2:C", "black", [ "v0" ] ],
    [ "nano:GND.2", "btn1:2.r", "black", [ "v0" ] ],
    [ "nano:GND.2", "btn2:2.l", "black", [ "v0" ] ],
    [ "nano:11", "btn1:1.l", "green", [ "v0" ] ],
    [ "nano:13", "btn2:1.r", "green", [ "v-105.6", "h153.1", "v-28.8" ] ],
    [ "nano:GND.2", "led3:C", "black", [ "v0" ] ],
    [ "nano:7", "led3:A", "green", [ "v-9.6", "h38.4", "v-57.6" ] ]
  ],
  "dependencies": {}
}

So you decided to use a microcontroller after all. What are the specifications of the electric motor? From the picture it looks quite big to me and with too much power so even connecting it via a prototype board is not a good idea. Also the rest of the elements may not be suitable for him - like driver and battery.

Agreed. What is the motor's rated voltage and current?

I'm not sure i had to change the motor to a smaller one (I'll send a photo when I have the chance) becuase one of the terminals on the previous one broke.

How then did you decide that this L293D driver is suitable for your electric motor? The same applies to powering the electric motor with these batteries.