AnalogWrite weird behaviour when using more than 3 PWM pins

Hi all,

I have a weird issue with my Arduino mega2560
I'm trying to control A4988 modules with the analogWrite function. (Because I just want to rotate the motor, I don't care about steps or whatsoever and the frequency of the analogWrite is good enough for me, so instead of sending High and then Low to the step pins I just use analogWrite)

When I send a signal to a single pin, there are no issues, and the motors rotate nicely.

When I send it up to 3, there is also no issue, the moment I try to send it to more than 3, something weird happens, the motors won't rotate as smoothly, they will become so noisy, and in fact, few of them won't even rotate.

When I measure the voltage at the Step pin, I notice that I don't get as much voltage on each pin as compared to when controlling less than 3 motors only, it is usually less (like a drop from around 3.4v to 1.8v

Have a look at this simple code:

const byte stepPin[7] = {8, 7, 6, 5, 9, 3, 2}; 
const byte dirPin[7] = {22, 24, 26, 28, 30, 32, 34};


void setup() {
    for (int i = 0; i < 7; i++) {
    pinMode(dirPin[i], OUTPUT);
    digitalWrite(dirPin[i], HIGH);
    pinMode(stepPin[i], OUTPUT);
    analogWrite(stepPin[i], 125);
    delay(3000);
}
}

void loop() {
  // put your main code here, to run repeatedly:

}

when I change the for loop to for (int i = 0; i < 3; i++) or say for (int i =3; i < 6; i++), there are no problems there.
I only get the issues when i'm running more than 3.

Any ideas on what's wrong?

I'm powering the full system with a 20A 12V power supply that goes through a step down to 5.5v that i feed to the arduino through the Vin pin.

You cannot power an Arduino at the Vin pin with 5.5V. At the Vin pin it needs 7V minimum.

This is now the third attempt with different sketches to operate more than 3 steppers. It should be obvious that your sketches are NOT the problem. Your problem is your hardware. Post a schematic how you wired all this and provide links to the used hardware elements.
However, maybe just supplying the Arduino with the correct voltage is enough. 5.5V at the Vin pin is definitely not correct.

1 Like

I guess that you power the motors from a too weak PSU. Measure all supply voltages.

1 Like

Hi,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Please do not use a Fritzy picture.

1 Like

The Vin pin of an Uno requires more than 5.5V. Use at least 7V.

1 Like

Are the steppers running directly off the 12V supply?

Note: If you use Pin 4 or Pin 13 for a stepper, expect them to run at 980 steps per second instead of 490 like on the other PWM pins.

1 Like

If the A4988 module is set to no microstepping, then going from 0 to 490 or 980 steps per seconds is mechanically too much. If it not too much, then the current limiting might be set for a low current and that prevents a good rotation.

1 Like

It was too hard to draw the schematic, however, the issue was resolved, thanks to all the inputs.

So what i did is these:

You are right, i changed the input to a high voltage and noticed the iprovements.

It was an issue of powering the mega, i shouldn't power it with 5v only, accourding to the arduino datasheet, the recommended input is 7-12 volts

I revised the wiring of my circuit, and in addition to the Vin issue i had many GND connections, i changed that to a common ground and it solved

Yes i did that, it helped, thanks

Yes that too was an issue, one of the motors was connected to pin 4 and it was being weird, i connected it to a different PWM pin and it was solved, also from what i googled i figured that pins 4 and 13 run on timer 0 wich has different frequency which actually caused some problems

I made sure to keep the current limit at 80% of the maximum required for the motor and i got a smooth running for all of them

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.