Motors PWM problem

hello...

I have an arduino mega and 2 dc motors and my problem is that when I set PWM_right in value=200 and PWM_left in value=255 ,both motors rotate in value=200(I'm sure about it ,beacuse when I set both of them in value=255 ,motors rotate faster).For example this is my code:

I tested the circuit and the code many times,but I do not know what is happenning.

const int IN_1_left = 30;
const int IN_2_left = 31;
const int PWM_left = 8;
const int IN_1_right = 32;
const int IN_2_right = 33;
const int PWM_right = 7;

void setup()
{
  pinMode(PWM_left, OUTPUT);
  pinMode(PWM_right, OUTPUT);

  pinMode(IN_1_left, OUTPUT);
  pinMode(IN_2_left, OUTPUT);
  pinMode(IN_1_right, OUTPUT);
  pinMode(IN_2_right, OUTPUT);

  analogWrite(PWM_left, 0);
  analogWrite(PWM_right, 0);
}


void FORWARD()
{
  analogWrite(PWM_left, 255);
  analogWrite(PWM_right, 200);

  digitalWrite(IN_1_left , HIGH);        //forward
  digitalWrite(IN_2_left , LOW);
  digitalWrite(IN_1_right , HIGH);
  digitalWrite(IN_2_right , LOW);
}


void loop()
{
FORWARD();
}

CIrcuit I followed:

https://www.google.com/search?biw=1600&bih=757&tbm=isch&sxsrf=ACYBGNQX1wYYwhRYwXcEI62Tb5j9bqCLcQ%3A1568915039903&sa=1&ei=X76DXfLcNs_JwQL976iwBg&q=arduino+2+motors+l293d&oq=arduino+2+motors+l293d&gs_l=img.3...35006.35202..35856...0.0..0.167.316.0j2......0....1..gws-wiz-img.tSZTV8zEWgE&ved=0ahUKEwiyiIv9t93kAhXPZFAKHf03CmYQ4dUDCAc&uact=5#imgrc=oS3ZTZmjfIecRM:

It would be a good idea if you make a simple pencil drawing derived from your own system and post a photo of the drawing. Don't worry if you are not an artist. See this Simple Image Guide

Often when making a drawing things get shown up that might help with the problem.

Details of exactly what components you are using and what you are using to power the motors will also be important.

...R

Circuit photo:

When I set value=255 in PWM_left and value=0 in PWM_right and call FORWARD in void loop() ,then left motor rotate in max speed and right motor stop ,but when when I set value=0in PWM_left and value=255 in PWM_right and call FORWARD in void loop() ,then both motors rotate in max speed (value=255).

I use a 9 volt battery for motors.

Code:

const int IN_1_left = 32;
const int IN_2_left = 33;
const int PWM_left = 13;
const int IN_1_right = 30;
const int IN_2_right = 31;
const int PWM_right = 8;

void setup()
{
  pinMode(PWM_left, OUTPUT);
  pinMode(PWM_right, OUTPUT);

  pinMode(IN_1_left, OUTPUT);
  pinMode(IN_2_left, OUTPUT);
  pinMode(IN_1_right, OUTPUT);
  pinMode(IN_2_right, OUTPUT);

  analogWrite(PWM_left, 0);
  analogWrite(PWM_right, 0);
}

void FORWARD()
{
  analogWrite(PWM_left, 0);
  analogWrite(PWM_right, 255);
  digitalWrite(IN_1_left , HIGH);        //forward
  digitalWrite(IN_2_left , LOW);
  digitalWrite(IN_1_right , HIGH);
  digitalWrite(IN_2_right , LOW);
}

void loop()
{
FORWARD();
}

The l293d driver works fine(I have 2 of them and I tested them).

I do not know where the problem is. :frowning:

image
ihttps://ibb.co/hLqy4f0

Image from Reply #3. See this Simple Image Guide

...R

What sort of 9v battery are you using? If it is one of the small PP3 style smoke alarm batteries it is quite useless - they cannot provide enough current. Try a pack of 6 x AA alkaline cells.

Exactly what motors are you using? How much current do the motors need?

...R

This is the battery I use: [ My battery is ok ,motors rotate really fast ]

https://www.google.com/search?q=okcell+9v&sxsrf=ACYBGNT94yIv3TqC656KpZfyZhEu82qL9Q:1568966835070&source=lnms&tbm=isch&sa=X&ved=0ahUKEwiKg_n2-N7kAhXQ66QKHXnrDNIQ_AUIEigB&biw=1600&bih=757#imgrc=zANrKAswxoVrnM:

Motors I use: [ Voltage range:6-9 Volt, Current (Stall): 650mA ,speed:130 RPM ]

That battery will certainly be better than the non-rechargeable smoke-alarm batteries but it won't power your motors for very long.

...R

Battery is not the problem .

When I set value=255 in PWM_left and value=0 in PWM_right and call FORWARD in void loop() ,then left motor rotate in max speed and right motor stop(works good) ,but when when I set value=0in PWM_left and value=255 in PWM_right and call FORWARD in void loop() ,then both motors rotate in max speed (value=255).It's irrational...

Does anyone know why this is happening?

You're calling FORWARD() over and over again very rapidly in loop() which may not be letting the PWM get started properly. Try either putting the call at the end of setup() or at least putting a 2 or 3 second delay() after it in loop() and see if that changes anything.

Steve

I found the wrong.It was the IN_1 & IN_2 values.