PWM Timer Question

If I am using a MEGA2560 R3 Arduino Elegoo, Can I get 4 different PWM depending on my input from the app I created? Right now I am using pins 6,9,10,11 for PWM. I think I read that 9 and 10 use the same timer. Does this mess up the input then?

Please advise,
Bull Engineer

What exactly do you want to be different about the "4 different PWM"?

Steve

See:- https://www.arduino.cc/en/Hacking/PinMapping2560

Thanks mike for the good red, I understand pin mapping but dont know much about PWM on the same timer. lets say pins 9/10 run on the same timer. Can I have PWM for motor one on pin 9 and PWM for motor 2 on pin 10? or should I avoid different PWMs on the same timer?

You can use all PWM pins at the same time.

Each timer has two sets of registers that determine the duty cycle of the resulting PWM. Thise can operate independently. However if you want to change the frequency of the PWM you get the same frequency appearing on both pins.

Mike, This frequency on different PWM timers has nothing to do with why some of my motors are moving slower than others?

BullEngineer:
Mike, This frequency on different PWM timers has nothing to do with why some of my motors are moving slower than others?

No nothing.

To change the speed of your motor change the duty cycle.

Expect that two motors need different duty cycle to run at the same speed.

how do I change duty cycle? do you want to look at my code?

The analogWrite function.

#define pin1 2 // motor #1 +
#define pin2 3 // motor #1 –
#define pw1 6 // motor #1 pwm
#define pin3 4 // motor #2 +
#define pin4 9 // motor #2 –
#define pw2 5 // motor #2 pwm
#define pin5 7 // motor #3 +
#define pin6 8 // motor #3 –
#define pw3 10 // motor #3 pwm
#define pin7 12 // motor #4 +
#define pin8 13 // motor #4 –
#define pw4 11 // motor #4 pwm
void setup() {
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pw1, OUTPUT);
pinMode(pw2, OUTPUT);

pinMode(pin5, OUTPUT);
pinMode(pin6, OUTPUT);
pinMode(pin7, OUTPUT);
pinMode(pin8, OUTPUT);
pinMode(pw3, OUTPUT);
pinMode(pw4, OUTPUT);
Serial.begin(9600);
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
digitalWrite(pin5, LOW);
digitalWrite(pin6, LOW);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
analogWrite(pw1, 50);
analogWrite(pw2, 50);
analogWrite(pw3, 50);
analogWrite(pw4, 50);
}

void loop() {

if (Serial.available() >= 2 )
{
unsigned int a = Serial.read();
unsigned int b = Serial.read();
unsigned int val = (b * 256) + a;

if (val == 50) // motor 1 reverse
{
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);

}
else if (val == 100) // motor #1 stop
{
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
}
else if (val == 150) // motor #1 forward
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
}
else if (val == 200) // motor #2 reverse
{
digitalWrite(pin3, LOW);
digitalWrite(pin4, HIGH);
}
else if (val == 250) // motor #2 stop
{
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
}
else if (val == 300) // motor #2 forward
{
digitalWrite(pin3, HIGH);
digitalWrite(pin4, LOW);
}
else if (val == 350) // motor #3 reverse
{
digitalWrite(pin5, LOW);
digitalWrite(pin6, HIGH);
}
else if (val == 400) // motor #3 stop
{
digitalWrite(pin5, LOW);
digitalWrite(pin6, LOW);
}
else if (val == 450) // motor #3 forward
{
digitalWrite(pin5, HIGH);
digitalWrite(pin6, LOW);
}
else if (val == 500) // motor #4 reverse
{
digitalWrite(pin7, LOW);
digitalWrite(pin8, HIGH);
}
else if (val == 550) // motor #4 stop
{
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
}
else if (val == 600) // motor #4 forward
{
digitalWrite(pin7, HIGH);
digitalWrite(pin8, LOW);
}
else if (val >= 1000 && val <= 1255)
{
analogWrite (pw1, val - 1000);
}
else if (val >= 2000 && val <= 2255)
{
analogWrite (pw2, val - 2000);
}
else if (val >= 3000 && val <= 3255)
{
analogWrite (pw3, val - 3000);
}
else if (val >= 4000 && val <= 4255)
{
analogWrite (pw4, val - 4000);
}
}
}

Here is my code.
Some background. I have an made with MIT app inventor that assigned each motor with 1000x(motor 1 = 1000, motor 2 = 2000 etc. then each motor has a slider that gives a value between 0-255 which controls the speed. I have one major problem right now, Motor 1&2 get far more range and faster speed than motors 3 and 4. (it isnt the individual motors because I have switched them around. it seems like the rear two motors dont get power. Almost as if H-bridge 1(that controls motors 1&2) has first priority instead all 4 motors having equal priority.

Thanks

Most likely a wiring/soldering issue.

ok thanks I will investigate. Do you want a link to my app for fun?

ok So I have a 12V power supply that needs to be split in parallel to 2 L298N H-bridges. Could I solder two wires onto the end of the + terminal of the power supply and it will be all good?

Besides the fact, can you run 12V through a standard breadboard. If you can... should you?

my guess is that they will get equal power

Of course you can split wires, just solder them together.

You can run 12V through a breadboard - no problem there - as long as you keep the current below about 500 mA. Very likely your motors draw more current than that.

ok since it is likely my motors draw more than that. I shouldn't connect the + terminal to the breadboard with 2 wires connected in parallel to each L298n because the current would likely exceed 500mA?

Indeed. Have your wires bypass the breadboard. Or, second best, place them right next to one another in the breadboard to shorten the path.

wow good to know, I will give some updates later