I have a wire going from PWM4 to A0 but I don't dare turn on my Arduino Mega in case this is incorrect.

PWM4 will eventually go to an analog device but I want to make sure the read value is what I expect it to be. Am I okay to proceed with my current setup or do I need to throw in a resistor / conversion?

  • A resistor/capacitor conversion? Yes. An RC filter on PWM4 could smooth out the square wave. If you don't, you'll get the 0 or the +5 values into A0, depending on the sample time.
    – Dave X
    Jul 7 '16 at 19:34
  • @DaveX So with the correct sample time would my analog pick up the pseudo analog signal?
    – Jacksonkr
    Jul 7 '16 at 22:44
  • 1
    No-The default analog sample time (<0.0001s) is short compared to the PWM frequency 490Hz, 980Hz , so the value you read at any particular point in time will be probabilistic. If you average enough samples of the binary +5 and 0V PWM output, it would approach the analog value statistically. Or you could use an RC filter to make an exponentially-weighted moving average in electrical-analong component space, rather than programming space.
    – Dave X
    Jul 8 '16 at 14:36

You are fine by doing that, but you won't get what you expect.

A PWM signal isn't an analog value. It's a series of pulses, the duration of those pulses is set by the (not accurately called) analogWrite. So sometimes the value is 5V and sometimes it's 0V. You need to get the average value by adding e.g. an RC-filter.

Another option is to add a proper DAC (digital to analog converter) chip to your circuit.

  • If I understand you correctly, I need to set the correct on/off interval for my pwm out before I can get the desired value for my analog read but this is otherwise doable and safe?
    – Jacksonkr
    Jul 7 '16 at 19:13
  • 1
    No, you set the on/off interval ratio using analogWrite. The average voltage will be proportional to the ratio. However to get the average voltage you need to create a filter average the 0V and 5V signals going out of the PWM pin.
    – Gerben
    Jul 8 '16 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.