# Arduino - Pulses get unstable when updating PWM duty-cycle

I'm trying to obtain stable pulse trains when adjusting the duty cycles wth an Arduino Uno board. In my actual project I need to use 4 adjustable PWM output pins at a fixed 25kHz frequency to control four pwm controlled DC motors.

But to point the issue easier I want to ask my question with an easy setup and with an easy short code.

Lets say the goal is to vary PWM duty-cycle of pulses with a fixed frequency. A potentiometer can be used to vary the input voltage at A3 pin which than is scaled to duty cycle of PWM pin 9. The simple setup is illustrated below:

To explain the issue in a clear way, I would like to show you couple of simple codes and short video records from the scope screen.

Here is the code which adjusts the duty-cycles from potentimeter readings at A3:

void setup()
{
pinMode(9, OUTPUT);
}

void loop()
{
float volt =(5.0 * val) / 1023; //convert the value to voltage
val = 255 * (volt / 5); //scale the voltage for pwm duty-cycle
analogWrite(9, val);

}


And with this code uploaded to the uC, below is the video record for the output:

As you see on the scope, the pulses are flickering. They are not stable. By the way the issue is not related to the poti output since I also tried fixed 3.3V pin out with an int cast. It seems to me it happens each time analogWrite updates.

And here is another code where analogRead and analogWrite functions are still continuously active in the loop. The only difference this time the analogWrite has a fixed value here as 24:

void setup()
{
pinMode(9, OUTPUT);
}

void loop()
{
float volt =(5.0 * val) / 1023; //convert the value to voltage
val = 255 * (volt / 5); //scale the voltage for pwm duty-cycle
analogWrite(9, 24);

}


And now with this one uploaded, below is the video record for the new output:

As you see on the scope, the pulses are extremely stable.

I need to control the DC motors with a high precision. Before this uC way, I made a simple analog circuit with 555 and a comparator but for some extra features I needed to use a uC.

But now I noticed this issue and spent too many hours to see if other libraries would have a better solution. I couldn't find any. I found the following video from a guy who wrote a library: https://www.youtube.com/watch?v=9JXGIeM3BSI but the same issue occurs with his library as well.

Is this a known issue and is there a workaround? And I would be very glad if I can find a solution or any suggestions.

Edit:

The problem was the Arduino being supplied by laptop's USB output. I checked the potentiometer and 3.3V pins with AC coupling and here is what I got:

Arduino ADC is 10 bit which is 5000/1024 = 4.8mV. Which means it is sensitive for each 4.5mV. In my case power ripple was causing sometimes around 15mV difference.

I tried with a non-USB 9V power supply from Arduino's power barrel jack, the issue solved. Thanks to Anklon for his intuition.

• can you give any photo or link to show what kind of potentiometer you are using ? Jun 23 '16 at 22:18
• yes I can but do you really need that? I wrote in the question I also hooked up A3 to Arduino's 3.3V pin and same issue exists. I can upload if you still need it. Jun 23 '16 at 22:20
• if you use a low quality potentiometer , then it is likely your output voltage from the potentiometer is flickering (very small range so that you may not notice that in voltmeter ).When arduino reads that, it becomes a issue. Jun 23 '16 at 22:22
• it would be a good idea to see the output voltage of the potentiometer in oscilloscope and see , is there any flickering occuring ? Jun 23 '16 at 22:23
• Some thoughts: The ADC result might be noisy for a variety of reasons; find out if this is the case and if so, why. Output the ADC values somewhere (UART?) so you can inspect them. What is your arduino connected to? The noise might be coming from any of those things. Your power supply could be noisy, for example. If the noise is unfixable, you could use some sort of moving averaging in your code to reduce it. Final remark: avoid using floats on microcontrollers like these; they have to be emulated which is horribly, horribly slow. And you only really need val /= 4; or val >>= 2;. Jun 23 '16 at 22:59

I think the problem is related on how you manage float and integer numbers in your app. val = 255 * (volt / 5) here you divide volt (float) with integer 5, then it becomes integer val??? float volt =(5.0 * val) / 1023; Here you multiply and divide integers and you expect to get float??