Quick question on analogWrite

Hello Team,

Quick question on analogWrite code.

analogWrite(9, 255);

When we measure pin 9, we get 4.89 to 4.91 VDC.

Now change the code to....

analogWrite(9, 128); and we get 2.45 VDC

We understand that's PWM and is not true voltage coming out of the PIN. Just wondering this is the reason? Any way to setup a closed loop monitoring to give exact output?

Thanks, BTFDev

PWM sets Pulse duty cycle. It has nothing to do with voltage. Output voltage would vary:

  1. depends on stability PSU, USB isn't exception, as it esily goes down to 4.8 and less under load;
  2. current flow to the load, voltage drops to 4.2V at 20 mA.
  3. DMM not measure correctly PWM, as it design for DC or pure sine-wave. To get PWM you have to pass signal via filter (RC etc).

Digital multimeters use slow integrating analog-to-digital converters - these sum the input voltage over perhaps 1/4 of a second and then display the average. With PWM at 1kHz a DMM will fairly accurately show the average level as you have observed. This slowness is useful for situations like this (a fast ADC would cause the displayed value to jump all over the place - hard to interpret)