Code for changing the direction of dc motor with one button

Hello, I am beginner and I am making a dc motor project.

What I prepared and set out:

  • Arduino Nano
  • L298N Motor Driver
  • A dc motor
  • 6V External battery for motor driver.

Till now, when I turn on the external battery, the motor rotates in one direction well.

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop() {
  digitalWrite(5, HIGH);
  digitalWrite(6, LOW);
  analogWrite(3, 100);
}

But I want to change the direction of motor with one button in opposite way. Every time I press the button, the dc motor rotates in opposite way. For this, I guess that every time I press the button, the each value of pin 5&6 should be exchanged because those are whether 0 or 1. Therefore I thought to add like below code inside void loop. The button in connected in pin2.

void loop() {
  digitalWrite(5, HIGH);
  digitalWrite(6, LOW);
  analogWrite(3, 100);

  int pin2 = digitalRead(2);
  if ( pin2 == 0) {
   
    }

I couldn't find the right code for exchanging the value of pin 5&6 when pin 2 is 0. How should I write the code to change the direction of motor in opposite way each time I press the button?

Please share with your wisdom...

  • Also after changing the direction, I hope to control the speed of dc motor with the potentiometer node. If you know the useful resource for this, also let me know...

Thanks!
Lee

key aspect is, the direction has to be remembered

think about that for a while

also look at the state change ide example

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop() {
  digitalWrite(5, HIGH);
  digitalWrite(6, LOW);
  analogWrite(3, 100);
}

In general, you should not use magic numbers. The I/O pins love to have a functional name.

something like this maybe...
(Compiles, NOT tested!)

uint8_t last_read;

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);

  last_read = digitalRead(2);

  analogWrite(3, 0); //ensure motor is OFF

  //Inital motor direction selected
  digitalWrite(5, HIGH);
  digitalWrite(6, LOW);
}

void loop() {
  int speed = map(analogRead(A0), 0, 1023, 0, 255); //potentionmeter connected analog input A0 and map ADC value for AnalogWrite

  uint8_t new_read = digitalRead(2);
  
  if (new_read != last_read) {
    if (new_read == 0) {
      analogWrite(3, 0); //Stop Motor
      delay(10); //brief delay

      //flip motor directior
      digitalWrite(5, !digitalRead(5));
      digitalWrite(6, !digitalRead(6));

      analogWrite(3, speed); //start motors again base of Pot reading
    }
    last_read = new_read;
  }

}

hope that helps...

Welcome: Hint: The L298 is a poor choice for a motor. With that bridge you are driving the motor with about 3V, not 6V. The reason is the design of the L298 itself, it uses bipolar transistors in a darlington configuration in each motor lead. Each darlington pair drops about 1.4V maybe a bit more so that * 2 (one in each motor leg) kills performance. Also in your case about 1/2 of your battery energy is burnt up as heat in the L298. Consider looking for a different bridge that has MOSFET outputs.

The Arduino does not have an exchange instruction, you have to turn on or off each pin as you desire. Swapping pin values can be done at the register level but that is not for new programmers. Best way is pin A on, then turn pin B off or the other way around which works best for you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.