Arduino and L298N Forward and Reverse motor controller

Hi guys,

I have recently configured this setup from howtomechatronics.com for
a forward and reverse motor controller using L298N.

We have got it working however, when the speed is set and switching from forward to reverse, the speed changes. I understand that it’s due to reading the position of the potentiometer, when it is set at say 80% power forward, clicking the button will send it to go 20% in reverse.

How can you code such that in forward it reads the analog input as 80% but as soon as the switch is clicked to go in reverse it can now read the analog input as 80% in reverse.

The schematic and code are attached.

Many thanks in advance!!!

/* Arduino DC Motor Control - PWM | H-Bridge | L298N - Example 01
by Dejan Nedelkovski, www.HowToMechatronics.com
*/
#define enA 9
#define in1 6
#define in2 7
#define button 4
int rotDirection = 0;
int pressed = false;
void setup() {
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(button, INPUT);
// Set initial rotation direction
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
}
void loop() {
int potValue = analogRead(A0); // Read potentiometer value
int pwmOutput = map(potValue, 0, 1023, 0 , 255); // Map the potentiometer value from 0 to 255
analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin
// Read button - Debounce
if (digitalRead(button) == true) {
pressed = !pressed;
}
while (digitalRead(button) == true);
delay(20);
// If button is pressed - change rotation direction
if (pressed == true & rotDirection == 0) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
rotDirection = 1;
delay(20);
}
// If button is pressed - change rotation direction
if (pressed == false & rotDirection == 1) {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
rotDirection = 0;
delay(20);
}
}

Arduino-and-L298N-Circuit-Diagram-DC-Motor-Control.pdf (50.1 KB)

Some additional information. I understand that the potentiometer will send a pulse of 0 to 5v to control the speed.

How can you write the output which is read by the potentiometer as a variable and then feed that variable back into the next function (which reverses the direction) as:

analogWrite(enA, (255-pwmOutput));

where the 255 - the variable / output gives the other spectrum thus providing the same speed in both forward or back if button is pressed?

I have tried the above, am not receiving any error messages so am unsure how to go about it.

Thank you!

tpratt95:
I understand that it's due to reading the position of the potentiometer, when it is set at say 80% power forward, clicking the button will send it to go 20% in reverse.

I don't know where that "understanding" came from but I'm afraid it's 100% wrong. With an L298N once the speed is set with the analogWrite() it applies to both directions.

There's nothing in your code or the way potentiometers work (they don't send "a pulse" but a simple DC voltage) that will cause the motor speed to change when you reverse direction.

If the speed does change it is perhaps because the motor you're using is more efficient in one direction than the other or maybe whatever mechanical setup you have causes drag on the motor when it's in reverse.

More details about your complete project may help to sort out what is going on, but it's not what you are imagining.

Steve