I'm trying to control 2 DC motors by using Arduino and L298N and something strange happens.
I'm writing same PWM values in the Arduino PWM pins (3 and 6), but the voltages that I see in the output pins (which is the pins that connect to the motors) of L298N are different (the motors moving in different speed).
I am not understand why. Same PWM voltage from the Arduino leads to different output of the L298N.
Best regards.
// Two Motors DC With L298
#include <Servo.h>
//FLAME SENSORS
#define Left 8 // left sensor
#define Right 9 // right sensor
#define Forward 7 //front sensor ###
//MOTORS
#define LM 11 // left motor - FRONT
#define LM_CONST 10
#define RM 13 // right motor - FRONT
#define RM_CONST 12
#define EN_RM 3 // right motor - FRONT
#define EN_LM 6 // left motor - REAR
//WATER PUMP
#define pump 4 // ###
//Servo
#define servoPin 5
Servo myservo;
int motor_speed = 60; // PWM
int pos = 0; // ANGLE
//General
int i;
char state = 0; //Variable for storing received data
boolean fire = false;
void setup() {
pinMode(Left, INPUT);
pinMode(Right, INPUT);
pinMode(Forward, INPUT);
pinMode(LM, OUTPUT);
pinMode(LM_CONST, OUTPUT);
digitalWrite(LM_CONST, LOW);
pinMode(RM, OUTPUT);
pinMode(RM_CONST, OUTPUT);
digitalWrite(RM_CONST, LOW);
pinMode(pump, OUTPUT);
analogWrite(EN_LM, motor_speed); // analogWrite(pin, value) // Enable
analogWrite(EN_RM, motor_speed); // analogWrite(pin, value) // Enable
myservo.attach(servoPin); // servo.attach(pin)
// servo.attach(pin, min, max)
myservo.write(0); // set servo to mid-point
}
void loop() {
if (digitalRead(Left) == 1 & digitalRead(Right) == 1) {
digitalWrite(LM, HIGH);
digitalWrite(RM, HIGH);
myservo.write(90);
}
else if (digitalRead(Left) == 0) {
digitalWrite(LM, LOW);
digitalWrite(RM, HIGH);
myservo.write(180);
delay(5000);
}
else if (digitalRead(Right) == 0) {
digitalWrite(LM, HIGH);
digitalWrite(RM, LOW);
myservo.write(270);
delay(5000);
}
}
Try using same frequency PWM (DPin-3, 11: 490 Hz).
Figure-1:
If the above does not solve the problem, then measure the speeds of both motors using IR sensor (Fig-2) and then modulate the PWM signals to keep the same speed for both motors.
Figure-2:
You want to control the speed of two motors using PWM with a L298 motor controller. Why am I seeing a bunch of code aimed at a Servo motor? What am I missing and where did this code come from?
Motors have reactance and if the frequency is too low the motor may not be able to support the voltage which will cause your different dvm readings if the periods of the two channels are different.
I have seen this using a scope, crappy wave forms and whinning from the motor.