I am using the Arduino UNO with the L298N to control 2 DC motors in an RC car.
When the RC car is not on the ground (wheels are in the air) it is working without a single problem, but when the car is on the ground, everything is working for the first minute, after that the motors spin normally in one direction, but stutter in the other.
When motor terminals are switched, the same thing happens but in the other direction, so it shouldn't be the motors.
I am using L298N like in the picture:
Everything is powered from a 12V battery, Arduino and L298N are connected to the battery in parallel, the L298N has a onboard 5V regulator but only up to 12V on my version, so the regulator is switched off because last boards voltage regulator was fried from using the same battery (voltage is around 13.2V when fully charged), so Arduino is connected to the 5V input terminal on the board.
Here is the code:
#include<SoftwareSerial.h>
//HC05 soft serial
const int rx = 5;
const int tx = 6;
SoftwareSerial mySerial (rx, tx);
//L298N spoj
const int in1 = 13;
const int in2 = 12;
const int in3 = 8;
const int in4 = 9;
const int en1 = 11;
const int en2 = 10;
//Useful Variables
int i=0;
int j=0;
int state;
int vSpeed=200; // Default speed, from 0 to 255
void setup() {
// Set pins as outputs:
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(en1, OUTPUT);
pinMode(en2, OUTPUT);
// Initialize serial communication at 9600 bits per second:
mySerial.begin(115200);
}
void loop() {
//Save income data to variable 'state'
if(mySerial.available() > 0){
state = mySerial.read();
}
switch(state) {
/***********************Forward****************************/
//If state is equal with letter 'F', car will go forward!
case 'F' :
analogWrite(en1, vSpeed);
analogWrite(en2, vSpeed);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
break;
/***********************Backward****************************/
//If state is equal with letter 'B', car will go backward
case 'B' :
analogWrite(en1, vSpeed);
analogWrite(en2, vSpeed);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
break;
/************************Right****************************/
//If state is equal with letter 'R', car will start turning right
case 'R' :
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
break;
/************************Left*****************************/
case 'L' :
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
break;
/************************Stop*****************************/
//If state is equal with letter 'S', stop the car
case 'S' :
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
break;
}
}
}
Any help would be greatly appreciated!