Should this code to test if an LED is on or off work

I am basically trying to test if there is a voltage going to a LED and if there is the planner says on or off
do you think this will work
if not why

hardware.pin5.configure(ANALOG_IN);

local voltage = hardware.voltage();
server.log(format(“Running at %.2f V”, voltage));
function checkPot() {
if (voltage == 0) {
server.show(“Off!”);
}
else {
server.show(“On!”);
}
imp.wakeup(0.01, checkPot)
}

server.log(“Testing voltage April”);

imp.configure(“voltagereader”, [], []);
checkPot(); to test if there is a voltage
hardware.pin5.configure(ANALOG_IN);

local voltage = hardware.voltage();
server.log(format(“Running at %.2f V”, voltage));
function checkPot() {
if (voltage == 0) {
server.show(“Off!”);
}
else {
server.show(“On!”);
}
imp.wakeup(0.01, checkPot)
}

server.log(“Testing voltage April”);

imp.configure(“voltagereader”, [], []);
checkPot();

it posted twice sorry

A few things:

  • You’re reading the pin before you configure it as analog in.
  • You’re assuming that off is exactly zero. This is very unlikely.
  • You’re actually not reading the pin, you’re reading the voltage the imp is running at

You probably want this:

`imp.configure(“voltage reader”, [], []);

hardware.pin5.configure(ANALOG_IN);

function checkpin5() {
local v = (hardware.pin5.read() / 65535) * hardware.voltage();
server.log(format(“pin5 is at %.2f volts”, v));
if (v > 1.0) server.show(“on”); else server.show(“off”);
imp.wakeup(0.5, checkpin5);
}

checkpin5();
`

…note also that LEDs can be turned on and off two ways. One is to turn the power pin (anode) on and off, the other is to connect or disconnect the ground pin (cathode: this is more common). You need to determine, likely with a voltmeter, which method is in use.

If the system is grounding the anode to turn the LED on, then the voltage you read will be near zero… but this also depends on where the current limiting resistor is in the circuit…

your code works thanks