L298n arduino code

I have a L298n motor control and i’m trying to control it with a Nano. what’s doing my nut in is i can’t find what’s wrong with my code. As i understand it, the In1 and In2 pin should be set to HIGH, LOW respectively to move forward and LOW, HIGH to move backwards. (this is relative to my code) when I do this however the motor acts as though both pins are HIGH and vibrates back and forth.

I have tested the 12v DC motor and it spins correctly in both directions when 12v is supplied by a bench supply. the L298n seems to also be working because if i disconnect the In1 and In2 pins and introduce them separately i can make the motor run forwards and backwards but when they are connected at the same time, it’s as though both pins from the Nano are set to HIGH. i have tried the code on an UNO as well to try and eliminate hardware issues. Any help would be greatly appreciated.

I have the ENA jumped to have the motor run at top speed and the arduino is being controlled by HM-10 and android app. communication looks good in serial tests.

#include <SoftwareSerial.h>
SoftwareSerial HM10(2, 3); //RX of BTLE = 3, TX of BTLE = 2

char appData;
String inData;

int in1 = 9;
int in2 = 10;

void _moveForward(){
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);

}

void _moveBackward(){
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
}

void _mStop(){
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}

void setup() {
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
HM10.begin(9600);
Serial.begin(9600);
_mStop();
}

void loop() {
HM10.listen();
while(HM10.available() > 0){
appData = HM10.read();
inData = String(appData);
}
if(inData == “F”){
_moveForward();
}
if(inData == “B”){
_moveBackward();
}

if(inData == “X”){
_mStop();
}

}

motor_test.ino (805 Bytes)

Sorry, I don’t fully understand how you have things connected, but I can tell you that you’ll need to be careful not to apply voltages higher than 5v to ANY of Arduino pins. You’ll need some kind of an isolation circuit between the NANO and the 2v motor, opto or a relay that doesn’t put the 12v on your Arduino.

From what I can tell, the code looks reasonable.

Sir Michael

Sorry for the confusion. Pin 9 goes to the In1 and pin 10 goes to In2. as far as i’m aware the signal pins (In1, In2) on the l298n go to an H-bridge so there shouldn’t be any issue with the arduino getting stray 12v.

I don't see a signal return wire from L298 ground to Arduino ground. You do have pins 9 & 10 set as OUTPUTs, don't you?

Hi JCA34F, this is going to sound like a dumb question but where does the signal return wire come/go on the l298n? Since last night i have tried putting some 4k7 resistors to ground on the breadboard in line with the signal wires, but this hasn't helped. i have both pins set to OUTPUT in the setup() part of the code i posted above.

where does the signal return wire come/go on the l298n?

On the GND screw terminal.

JCA34F! Thank you! It Works! if you have time can you explain why this is necessary? Being new to this stuff, it helps my understanding and development.

SunTzuShin3:
JCA34F! Thank you! It Works! if you have time can you explain why this is necessary? Being new to this stuff, it helps my understanding and development.

Current flows in a circuit.
Current from the UNO has to flow in a circuit through the motor controller and back to the UNO.
The gnd connections provide the return circuit.

Without the gnd connection, there is no current flow and so no control.

Tom.. :slight_smile: