Measure voltage using digital pin

Hi,

I know we could measure voltage using Analog pin to read the input. However, i just ask if we could use digital pin to measure voltage.

Because for digital pin high voltage is 1 and low voltage is zero. Thus , i believe it could not measure voltage. Could you guy verify my thinking.

Without any other hardware attached to the digital pin then no you can't.

I moved your post here because you posted in the section about getting the IDE to work.

The clue is in the name...digital.

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

Thank you very much

Could you recommend some hardware i can use ?

Why bother when you could use an analogue pin?

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.

To measure what? What is providing the voltage? How much accuracy do you need?

Just for fun, I wrote a blog post about this sometime earlier this year.

Far simpler these days to use an actual hardware ADC.

2 Likes

Yes, you can using no additional hardware. But it will only yield a digital result that indicates that the voltage is within certain ranges.

@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.

image

(diagram produced with EasyEDA)

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 :frowning:

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!

Yeahbut...

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 :slight_smile:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.