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– Dave X2016年07月07日 19:34:33 +00:00Commented Jul 7, 2016 at 19:34
-
@DaveX So with the correct sample time would my analog pick up the pseudo analog signal?Jacksonkr– Jacksonkr2016年07月07日 22:44:29 +00:00Commented Jul 7, 2016 at 22:44
-
1No-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– Dave X2016年07月08日 14:36:59 +00:00Commented Jul 8, 2016 at 14:36
1 Answer 1
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– Jacksonkr2016年07月07日 19:13:14 +00:00Commented Jul 7, 2016 at 19:13
-
1No, 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– Gerben2016年07月08日 12:42:01 +00:00Commented Jul 8, 2016 at 12:42
Explore related questions
See similar questions with these tags.