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)