I have an Arduino-like board (HiFive1) that, like Raspberry Pis but unlike most Arduinos, doesn't have any analogue inputs. They are very handy for simple demos such as making a LED blink faster or slower, positioning an RC servo etc.
Would the following simple-minded scheme work? I don't need high accuracy or linearity or anything like that. I have a very limited selection of parts lying around at present.
Proposed parts:
- one (1) digital IO pin, switched as an output most of the time, then input when making a reading
- a 220 ohm resistor attached to the IO pin
- a 10 nF ceramic capacitor from the 220 ohm resistor to ground.
- a 10 k ohm resistor from the junction of the 220 ohm and 10 nF, to ...
- a 10 k ohm potentiometer from the fixed 10 k to +3.3 V.
The idea is for the IO pin to be driven low most of the time, keeping the capacitor discharged to within 1% - 2% of ground (or at least the IO pin output voltage). I figure the maximum current from a charged capacitor would be restricted to about 15 mA (and 0.15 mA - 0.3 mA via the pot), and the capacitor discharged with a time constant of about 2 us.
When a reading is desired, the IO pin is switched to a (high impedance) input and sampled until digitalRead() returns 1 instead of 0. The capacitor will charge via between 10k and 20k ohms, depending on the pot setting, with a time constant of around 0.1 - 0.2 ms.
If I understand it correctly, one time constant will get the input voltage to about 63%, two time constants to 86% etc. So it seems likely that the digital input will change from 0 to 1 sometime roughly around 1 time constant (certainly sometime between 0.25 (22%) and 2).
I don't think I'd break anything (ok, maybe put 440 ohm on the IO pin, not 220), but the point on which I'm not clear is how consistent and reliable the switching point would be on something which after all is intended to have clear hi or lo inputs and fast switching between them.
I certainly would not be surprised if different pins switched at different points, let alone different chips. But would one pin be reasonably consistent?
The CPU itself is running at 256 MHz (at 1 cycle per instruction, except loads/stores. multiply (4 cycles), divide and mispredicted branches). There is a register incrementing at 32 MHz, so no problem to get accurate timings, if the switching point is consistent.
The CPU doesn't connect directly to the IO pins, but via a TI TXS0108ERGYR level shifter
http://www.ti.com/lit/ds/symlink/txs0108e.pdf Schematics are at
https://dev.sifive.com/hifive1/hifive1-schematics/Am I crazy?