Electronic – arduino – Simple ADC alternative by using a capacitor and digital Arduino pin

arduinocapacitancecapacitorpotentiometer

I'd like to measure a dozen potentiometers with an Arduino UNO. Unfortunately, the UNO only has 6 analog pins, but it does have about a dozen digital pins.

Could I effectively measure an analog value with a digital pin by following this procedure?

  1. Wire the potentiometer in parallel with a capacitor.
  2. Connect one junction to an Arduino's digital pin.
  3. Set this pin to write HIGH until the capacitor fully charges.
  4. Set pin to read LOW and then use pulseIn() to measure the time it takes for the capacitor to discharge across the potentiometer, causing the voltage at the pin to go from 5V to 0. This time should be proportional to the resistance of the pot. e.g. a pot with a low resistance will cause the cap to discharge very fast, whereas a high resistance will cause the cap to discharge more slowly.

Best Answer

I would not rely on it. While it may work there could also be some stray capacitance or other factors that also affect the reading of time and voltage value. Remember that there is a no mans land between high and low (where the analog signal would be), that theoretically could change with temperature, time or any variety of other reasons and invalidate your setup. It also relies on knowing the precise time of discharge. Keeping time like this is something that is very hard to do accurately on any microcontoller.

Take for instance you get this to work and calibrate it so that you know that it reads low when the input gets to 1.7v. But come back in a day (or even minute) later and that 1.7v threshold is now 1.6v. The calibration you did would be invalid and you would be getting bogus results.

What I would do instead is use the digital pins to read an external ADC over an SPI or other digital line. This has the benefit of being more reliable and (in most cases) more accurate as well.

So to answer your question, it is theoretically possible. It would be a tough circuit to design, you would need to disconnect the line being monitored while you charge the capacitor, but it could be done. However it most likely would not be very reliable.