A rather odd question...can I connect a digital out to an analog in directly, thus being able to read the PWM signals generated by analogWrite()?
I can't figure it out and I'm afraid to just try it cause I may damage my board...
I'm thinking either:
I shortcircuit my board.
Nothing happens cause there is no circuit.
I think I should connect it to the analog in AND ground, but then I'll need a resistor (I think) on the ground cause otherwise the current will all go into the ground (it finds the path with the least resistance).
can I connect a digital out to an analog in directly, thus being able to read the PWM signals generated by analogWrite()?
Yes, you can, but it won't give the results you probably think it does.
"analogWrite" is a misnomer; a PWM signal is digital.
Trying to use an "analogRead" on such a signal will give (roughly) zero or (roughly) full scale.
About my expectations: I'm not expecting anything in particular. I'm just curious. I recently made a software oscilloscope and I wanna see what happens if I try to display these PWM signals with it. I don't know if the ADC can read fast enough to register the individual pulses thus I'll get a nice square wave (which I hoped), whether I'll get random on/off pulses or whether I'll get the "average" (meaning a 50% duty cycle will register as 2.5V).
About the connection: So the ADC IS allready connected to the ground it seems (since I don't have to ground it to make a circuit)? And the way I understood it the ADCs allready have a large resistor (100K I believe) built in? So I thought I would have to connect it like this:
.-----resistor-----ground
Digital out-|
'-------------------analog in
But you're (Mike) saying I should do it like this:
Digital out---resistor--+--analog in
|
capacitor-----ground
I don't quite understand why? Could you perhaps elaborate a bit?
AH! That's what I suspected. The cap smoothens out the PWM. However that's not what I want. What I would like to do is to see the PWM square wave in the oscilloscope (if possible).
My fault for not making it clear what my intentions were.
So should I try the same setup and lose the cap? or...?
If so then could I try a larger/smaller cap? I happen to have a bunch of 10K and 220 Ohm around....
Yea, just lose the cap if you want to try and sample the PWM signal directly. As already posted I think you will be dissapointed in the results as you most likely won't be synced up with the PWM timing and whatever your A/D sampling rate ends up being.
Yes just use what you have got. You will see a small ripple at the PWM frequency. As you change the values you will see the ripple change. Have a play.
There is no need for the resistor or capacitor if you just want to see the raw PWM.