Hi,
I was planning to make a simple cart using two wheels, an L298n motor driver, and Arduino UNO. I encounter some weird problem as follow-
Whenever I give power to the motor for a few seconds using the pins of the bridge, the wheels jerk or PULSES on and off, sometimes it runs fluently while other times it jerks randomly. I power the Arduino using 5V adapter and the bridge using 12V adapter. I tried doing it with a new bridge but had to go through the same problem. This is my first time with a bridge so please give adequate suggestions.
// Motor A connections
int enA = 9;//White
int in1 = 7;//Yellow
int in2 = 6;//Purple
// Motor B connections
int enB = 10;//Orange
int in3 = 5;//Brown
int in4 = 4;//Black
// Ultrasonic connections
int trig = 3;//White
int echo =2;//Grey
//Servo Motor
#include <Servo.h>
int pos=120;
Servo myservo;
char serial;
//Variables
long duration, inches, cm;//Ultrasonic
void setup() {
Serial.begin(9600);
// Set the pins to OUTPUT/INPUT
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
myservo.attach(8);
// Turn off motors - Initial state
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void loop(){
Serial.available();
{
serial=Serial.read();
if(serial=='2')
{
analogWrite(enA,255);
analogWrite(enB,255);
digitalWrite(in1, HIGH);
digitalWrite(in3 ,HIGH);
delay(1000);
digitalWrite(in1, LOW);
digitalWrite(in3 ,LOW);
}
}
//Ultrasonic Sensor
/*digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
pinMode(echo, INPUT);
duration = pulseIn(echo, HIGH);
cm = duration / 29 / 2;
delay(100);*/
//Servo Motor
/*myservo.write(pos);
for (pos = 120; pos >= 60; pos -= 1)
{
myservo.write(pos);
delay(3);
}
while (pos <= 120)
{
myservo.write(pos);
delay(3);
pos += 1 ;
}*/}```
My first suggestion is get a different driver, you are losing at least 3 volts to the motor radically reducing power and control. The L298 is an obsolete item.
Are you using PWM thrpugh pins 9 & 10 to control your motors? This may be of interest:
From the Servo library reference,
On boards other than the Mega, use of the (Servo) library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins.
12V is the just right volt to apply to the Motor Driver as the PWM signal will chop this voltage and will effectively bring low voltage 1.3V to 5V across the Motor terminals. This is what I am saying from experience.
Hi,
I would need the servo library for the rest of my project and its no use if it works after removing it. So that gets ruled out.
Also, one can barely notice the motors moving when 9V is applied.
As previously pointed out, you can't use pins 9 and 10 for PWM if you use the servo library.
How much current can your 9V adapter provide?
You should allow 1A for each motor.
I would suggest you to follow these steps; where, minimum jumpers will be used in order to rule out the problems you are facing with your present hardware setup. For these purposes, you can use those jumpers that I see in your picture of post #10.
1. Take out one Motor from the wheel of your Robot Car.
2. Solder two wires (approx. 7") at the terminals of the Motor. If you have no soldering iron, then twist bare ends of the jumpers with Motor terminals tightly.
3. Connect other two male headers of the Motor wires with OUT1 and OUT2 terminals of the following Motor Driver. 4. Do not remove the ENA, ENB, and 5V Jumpers from the Driver. Leave them as they are.
5. Connect GND-pin of Driver with GND-pin of UNO. Connect IN1-pin of Driver with 5V-pin of Driver. Connect IN2-pin of Driver with GND-pin of Driver.
6. Connect 12V-pin of Driver with 5V-pin of UNO. Check that Green LED of Driver is ON. Check that Motor is turning in the Forward Direction. If not, swap the OUT1 and OUT2 terminals. Run the Motor for 10 sec and then remove connection from from 5V-pin of UNO.
7. Remove IN1's connection from 5V-pin of Driver. Connect IN1 with DPin-3 of UNO to receive PWM signal to control the speed of the Motor.
9. Connect %V of UNO with 12V-pin of Driver. Press RESET Button of UNO. Check that the Motor is turning in the Forward direction at varying speed smoothly. After 10 sec of running, disconnect 5V from UNO.
10. Next task will be to drive the Motor using 12V supply and then to control the movement of the Motor using Ultrasonic sensor.
It is still not working.
I have replaced the 9 and 10 pins, removed the servo library, changed the Arduino, changed the bridge, changed the power supply(5v,9v,12v) to both the Arduino and bridge, changed the motors, changed the code, and even used the self PWM method prescribed by @GolamMostafa. It still jerks and it is very frustrating.