1

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?

asked Jul 7, 2016 at 16:32
3
  • 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. Commented Jul 7, 2016 at 19:34
  • @DaveX So with the correct sample time would my analog pick up the pseudo analog signal? Commented Jul 7, 2016 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. Commented Jul 8, 2016 at 14:36

1 Answer 1

3

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.

answered Jul 7, 2016 at 18:43
2
  • 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? Commented Jul 7, 2016 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. Commented Jul 8, 2016 at 12:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.