As you say, a digital pin can only be in one of 2 states, HIGH or LOW so such a pin cannot be used to measure a voltage. The best that could be achieved is to detect when the state of the pin changes from HIGH to LOW and vice versa which will happen at 2 different voltages
I would be interested to see how a digital pin could be measure a voltage even with external hardware
Prior to having analog inputs we would use a resistor and capacitor attached to the input to make analog measurements. Write a low to the digital pin then switch it to input and measure how long it took to get to a one state. This was a way to read joystick positions without analog inputs.
@cedarlakeinstruments
I found this confusing - until I realised you were using a digital pin.
#define ANALOG_PIN 2
Its an easy way to measure a voltage with a digital pin, the only down side being that for a low input voltage the conversion takes a VERY long time; however thats easily fixed by adding a resistor to +Vcc.
I had actually planned on extending the series with other techniques such as successive-approximation using the comparator input, but never got around to it. It's hard to maintain the motivation when high-resolution ADCs cost so little
Its a great proof of concept - and of course while the accuracy may not be great, the precision is virtually unlimited (depending on the conversion time).
A dual slope conversion would require a negative supply for a constant current - which would be ridiculous; as you say its easier and likely cheaper to add an external ADC..
However - I wonder if a similar approach could be used to make a delta-sigma ADC without too many additional components? I may try this, if only to help me understand how that conversion works!
This never occurred to me until you wrote that but I built a voltage inverter with a couple of caps and diodes (trying to simulate the old ICL7660) hanging off an Arduino pin and I was able to get about -3.4V at low current using standard silicon diodes.
I doubt that I'll ever get around to building a dual-conversion ADC with resistors, caps and diodes, but it is tempting should I ever run out of other projects. Maybe in about 20 years or so