How to test for the ABSENCE of voltage on an analog input pin?

My project requires testing for whether a 12 VDC device is turned on.

My current approach is to check if there is voltage on an analog input pin (after using a voltage divider to reduce the voltage to 5 volts or less).

When I test doing an analogREAD with 4.5 volts power applied I get a correct reading.

However, if the input power is absent (disconnected), all I get is floating readings (as noted in the analogRead documentation).

SO... is there any best way to simply detect the absence of voltage using analogRead?

For example: Should I set up a brief loop to check for large reading fluctuations, and then assume this indicates 0 voltage (i., e., absence of power)?

Note: Using digitalRead would produce similar random fluctuations (high/low).

For what it's worth, below is the test code I am using for the analogREAD.

// Test to see if an analog read can be used to determine if reverse light is on
// 4.5 volt battery used for testing - negative to Arduino GND, positive to pin A2
#define reverseSensor A2
int pinvalue; // hold value of analog read
void setup() {
pinMode (reverseSensor, INPUT);

void loop() {
pinvalue = analogRead (reverseSensor);
Serial.println (pinvalue);
// Should be a value between 0 and 1024; for 4.5 volts should be about 922 (4.5/5 * 1024)
float voltage = (5/1023 * pinvalue);
if (pinvalue > 700) {
Serial.println ("Power is on ");}
if (pinvalue < 700) {
Serial.println ("Power is off");}

One could use an opto isolator on a digital pin with either a pull up or pull down resistor to set a default state.

Did you forget to connect the grounds?

Post a wiring diagram. With a properly grounded voltage divider, you will not experience floating inputs.

How to test for the ABSENCE of voltage on an analog input pin?

Your question is fundamentally flawed; everything in the Universe made of ordinary matter has a voltage on it, there is nothing made of ordinary matter with an absence of a voltage on it.

However, if the input power is absent (disconnected), all I get is floating readings (as noted in the analogRead documentation).

That's because the input is floating, the voltage you read is whatever random thing it picks up from the surroundings, it is acting as an aerial.

You need to wire it so that there is a definite, known voltage on the pin at all times. Probably the simplest for what you are doing is a pull down resistor.

Oh, and please, read 'how to use this forum - please read' (big clue in the title) and follow the instructions, especially item #7.

The "bottom" resistor in the voltage divider should be connected to the Arduino's ground and (if wired properly) it WILL pull-down the analog input when no voltage is applied to the voltage divider's input.

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Particularly how you have the 12V, potential divider and Arduino input connected.

Thanks.. Tom.... :slight_smile:

Well, wait, if you have a resistor divider on it, with no applied voltage, the half of the divider between the pin and ground will act as a pulldown, and it will sit at 0v if no voltage is applied to the other end of the voltage divider.

And if there's no divider, will the voltage ever be Vcc? If not, can the thing you're measuring sink a bit of current? If yes, just turn on the pullup, and measure the voltage - if there's nothing connected it will read ~1023, otherwise it will read ~ the actual value (assuming, again, that it doesn't have difficulty sinking ~0.2mA) - you can then turn off the pullup and measure the voltage without the influence of the pullup.