Why isn't this PWM code giving me 5V?

When I measure the voltage of Pin 9 with a DMM, I get 2.5V on my Arduino Uno. On my Arduino Nano, I get 2.2V. The frequency of 30.64 Hz is correct, however.

I'm pretty positive that neither board is broken or damaged, because digitalWrite() can give me 5V.

int PWM_Pin_1 = 9;
int PWM_Pin_2 = 10;

void setup() {
  // put your setup code here, to run once:
  pinMode(PWM_Pin_1, OUTPUT);
  pinMode(PWM_Pin_2, OUTPUT);
  TCCR1B = TCCR1B & B11111000 | B00000101; //Change frequency of D9 and D10 to be 30.64 Hz.

  
}

void loop() {
  // put your main code here, to run repeatedly:
  analogWrite(PWM_Pin_1, 125);
  analogWrite(PWM_Pin_2, 125);
}

Are you aware PWM is a square wave, not a DC voltage ?

Yes, I'm measuring with the AC setting on my DMM.

What voltage is measured on the 5v DC pin on the two Arduinos ?

5.08V for the Uno, 4.81 for the nano

You can't reliably measure a PWM signal with a DMM. A scope must be used for that.

PWM is a digital signal (not a varying voltage) that switches between the supply and ground of the processor. A DMM tries to average that to a voltage, and how it averages depends on the DMM.

An Uno uses a (backflow protection) mosfet to switch between USB supply and external supply (if used). Therefore VCC (5volt) of an Uno is closer to the USB supply voltage than a (classic) Nano, which uses a basic Schottky diode with a ~0.4volt drop for the same function.
Leo..

5.08V for the Uno, 4.81 for the nano

There is a difference then, 0.27v difference.

Ok, I'll try measuring with a scope and get back to you.

Try the code below, you should measure the same dc voltage on the PWM pin as you measure on the modules +5 volt pin.

PS: it is your original code with just the PWM value changed.

For any PWM output value, it should measure PWM / 255 * VCC
where VCC = supply voltage

int PWM_Pin_1 = 9;
int PWM_Pin_2 = 10;
void setup() {
  // put your setup code here, to run once:
  pinMode(PWM_Pin_1, OUTPUT);
  pinMode(PWM_Pin_2, OUTPUT);
  TCCR1B = TCCR1B & B11111000 | B00000101; //Change frequency of D9 and D10 to be 30.64 Hz.
}
void loop() {
  // put your main code here, to run repeatedly:
  analogWrite(PWM_Pin_1, 255);
  analogWrite(PWM_Pin_2, 255);
}

@Wawa @LarryD

So I was having trouble getting my scope to work properly so I switched my DMM to DC as @WattsThat recommended and it worked fine? Thank you!

Sorry if this is a dumb follow up, but my intention was to drive a device that requires an AC square wave to drive it. The device will get damaged if it gets a DC voltage for a long period of time. Will I be safe driving the device with this PWM method? I'm only wondering since the AC setting on the DMM didn't measure the voltage but the DC setting did.

No, because this is not an AC voltage.

However, if you connect the output through a (not electrolytic) capacitor, it will be AC. :+1:

Thanks for your response. I will look into how to convert the PWM into AC using capacitors.

Here was the product I wanted to drive. It requires an AC voltage across the two pins:

PWM Is a unipolar signal with its output range between zero and VCC volts. True AC signals are bipolar with the range being equal positive and and negative voltage levels.

What is the device youā€™re driving? Does it need a true AC bipolar signal?

Here is the device: Large Liquid Crystal Light Valve - Controllable Shutter Glass : ID 3330 : $7.50 : Adafruit Industries, Unique & fun DIY electronics and kits

It doesn't need to be bipolar.

Before trying this PWM method, I tried just oscillating two digital Arduino pins. I got the same result, like around 2.2V which i could measure with the AC setting of my DMM, and that did not damage the device. However, it did not darken the device to it's max opacity.

So that's why I wanted to try this PWM method for more reliable results. So it's possible that it won't get damaged if I just PWM this device directly since just oscillating the pins didn't damage it, but I'm not sure.

As long as the input has no reference to ground, otherwise itā€™s still ā€˜pulsed DCā€™.

Thereā€™s a real reason theyā€™re called AC and DC.

That shutter is fine with PWM, just donā€™t allow the output to go any higher than required to make it fully dark, that should be around 200 counts on the analog output, according to Adafruit and never allow 255 as that is steady dc with no pulsing.

Still need the series capacitor.

What F cap would you recommend? Also, wouldn't I be fine going 255 if I used the cap?

Why are you lowering the PWM frequency to 30 Hz? Thatā€™s just slightly above where humans perceive flicker. Iā€™m also not sure why two pins of PWM, you only need one, connecting the device between PWM and GND. If

Have you tried it at the default PWM frequency without a capacitor? If that does not work, Iā€™d add a simple low pass r/c filter of 10k and 0.1uf.