PWM with arduino and L298N

Hey

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);   
  }     
}

How did you measure this voltage? With a DVM?

The Arduino has different PWM frequencies.

One, Nano, Mini pins 3, 9, 10, 11= 490 Hz,
pins 5 and 6 = 980 Hz

If you use a common DVM, you will find different voltage values for the same PWM value.

And step motors are going to have different speeds.

Please don't duplicate Topics it is strictly banned.... Your other topic has been flagged.

Please refer to this for more info.

Thank You.

can you post a link to the specified shield you're using and which pins on the arduino and shield were connected together?

As we dont all know EVERY part that can be connected ato an Arduino its always best to post a link to the ACTUAL components you are using. Like this

I'd not expect that, I guess it depends on the dvm? I'll have to test it.
But the problem is easily solved with a CR low pass filter.

Probably these are the drivers:
L298DriverPic
L298Schematic-2

Try using same frequency PWM (DPin-3, 11: 490 Hz).
pwm328
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.
image
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?

Ron

maybe.why guess?

but is it one that has a dir and step pin or one with in1, in2 and en

and which arduino pins are connect to which pin on the driver board

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.

would you expect the voltage to be different if the two motors are the same?

The voltage can be different because of the two different frequencies.

Currently, I have been preparing to drive a 4-wheel Robot Car using L298 driver. If you want I can answer to your questions of post #10.

i don't understand why there are 3 pins allocated for each motor?

the h-bridge chips typically have IN1, IN2 and EN. but driver boards may just have Dir and EN(?).

which board is the OP using?

From his post it is clear that OP is using L298N Motor Driver Board (post #7).

Referring to Fig-1, I am making this tested Functional Table using ENA, IN1, and IN2 pins for 1st Motor.

ENA     IN1     IN2       Function               +VS (Vcc)        V(OUT1-OUT2)
5V      5V      0V        Forward Rotation       6V               +4.3V
5V      0V      5V        Reverse Rotation       6V               -4.3V
5V      PWM  0V           Forward variable speed 6V               changing


Figure-1:

PWM Test Sketch:

byte dutyCycle = 255;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  analogWrite(3, dutyCycle);
  dutyCycle -= 20;
  Serial.println(dutyCycle);
  delay(1000);
}

then why does he control the direction of each motor by just changing one pin?

could it be this driver card?

DRI0009

@jasontit

Please, post the picture of your L298N Motor driver board.