How to test if a led is on without looking at it?

Hi all, is there a way to test if 5V are passing through a certain pin? Can se post me a basic sketch that show me a possible way?
C remeinessence lead me to use flags, but this is not the proper solution I am looking for.
Thanks in advance!

You can do the digitalRead of the same pin that you have the digitalWrite (I've not sure if this method works, but you can try it). If the last method don't work, you can connect together an input and an output and then read with the input if the output is HIGH or not.

I think is what you're looking for, right?

Do you mean you want to see if the pin is set high, or if there is actually voltage there?

The only way a pin can be set high is when you do it with a write, so you already know that it's high: your suggestion if using a flag seems to me to be the right way of remembering. It's very common in the forum to see states set like that. Just after you set the pin high, set a boolean variable high as well. Seems to me to be strange to read the pin to see if it's high, since you're the one who set it in the first place and all you have to do is remember that fact.

If you mean you want to see if the pin is actually high, ie there's actually 5V there, then I guess you could connect it to another pin and read that one.

luisilva:
You can do the digitalRead of the same pin that you have the digitalWrite (I’ve not sure if this method works, but you can try it). If the last method don’t work, you can connect together an input and an output and then read with the input if the output is HIGH or not.

I think is what you’re looking for, right?

Yeah, that seems to be the easiest way, I hope it will work thank you man.
Now, I am using the SoftwareSerial library, to send the value of the DigitalRead(pin) through bluetooth on another device I would have to use the function << BluetoothData=Lukebt.read(pin);>> ?

Note: SoftwareSerial Lukebt(4, 3); Lukebt is how I initialized the softwareserial

JimboZA:
Do you mean you want to see if the pin is set high, or if there is actually voltage there?

The only way a pin can be set high is when you do it with a write, so you already know that it’s high: your suggestion if using a flag seems to me to be the right way of remembering. It’s very common in the forum to see states set like that. Just after you set the pin high, set a boolean variable high as well. Seems to me to be strange to read the pin to see if it’s high, since you’re the one who set it in the first place and all you have to do is remember that fact.

If you mean you want to see if the pin is actually high, ie there’s actually 5V there, then I guess you could connect it to another pin and read that one.

You’re right too, but the main problem it is that the pin of arduino i am referring to, can be slaved by different devices through bluetooth, so let’s make an example : Device 1 (call it A) set the pin on state 1, switching ON the led associated with it . Now device 2 (call it B) can set the pin on state 1 or 2, but if we don’t have a real time “answer” from arduino about the state of this pin, B doesn’t know if he got to set the pin on state 1 or 2, in order to effectively change the state of the led.
I hope you got better what I mean now, and thank you guys for your answers, I appreciate your big help!

void setup() 
{
  pinMode(13, OUTPUT);
  Serial.begin(115200);
  Serial.println("Start ");
}

void loop() 
{
  digitalWrite(13, random(2)); // 50% chance HIGH/LOW
  
  if (digitalRead(13) == HIGH) Serial.println("ON");
  else Serial.println("OFF");
  delay(1000);
}

we don’t have a real time “answer” from arduino about the state of this pin

You will if you use a global variable or one bit of a global variable to flag the state of the pin.

I don't think I agree, at least as I understand it.

I think that whatever device sets the led on, should set the ledIsOn flag too. Then there is a real-time knowledge of its state. Nothing happens simultaneously remember, there will always be a sequential run through the commands. So as long as the command which sets the flag is immediately after the command which turns the led on, the state is up to date. When any other command needs to do something to the led, it checks the flag which is surely up to date in real time, and can act accordingly.

You could think of the whole thing as a finite state machine whose state is always known, and whose possible transitions are known, so the possibilities of Current state + Possible transition => New state are finite and known.

At least that's my take on it.....

Agreed 100%

one could check the low level ports .. These hold the state of the output bits....

The “digital read” and the “flag” solution seems to work both in order to check always the state of a pin.
Maybe I’m asking too much and i’m going OT, but I would need a little more help to solve once for all my issue…
I am interfacing Arduino with my smartphone via Bluetooth and I created a specific app with MIT app inventor in order to set the state of that (damn ]:slight_smile: )pin. While i managed sending text from my smartphone to the serial of arduino in order to set the pin’s state on or off, I really can’t find a way to read what Arduino writes on its serial (in this case i just need to write with arduino the value of the “flag” and to make my smartphone reading it). Do you guys know if is there any function on MIT app inventor that allow me to receive some values from arduino’s serial (maybe with bluetooth as I do to send them)?

P.s.I attach the function I use to send data from my smartphone to Arduino.

Following this with great interest, is possible to measure the "load" on a pin? i.e. see how much current is being used? Surely, if you have 5V and use a 1K resistor, you know the LED should use 0.005A. But, how could one, only with code on the Arduino MCU, see how much current is being used?

robtillaart:
one could check the low level ports .. These hold the state of the output bits....

Ah yes I keep forgetting about those. Never used ports, must do that sometime.

Luke_tx:
Now, I am using the SoftwareSerial library, to send the value of the DigitalRead(pin) through bluetooth on another device I would have to use the function << BluetoothData=Lukebt.read(pin);>> ?

Just use

BluetoothData=Lukebt.read();

…R

Luke_tx:
You're right too, but the main problem it is that the pin of arduino i am referring to, can be slaved by different devices through bluetooth, so let's make an example : Device 1 (call it A) set the pin on state 1, switching ON the led associated with it . Now device 2 (call it B) can set the pin on state 1 or 2, but if we don't have a real time "answer" from arduino about the state of this pin, B doesn't know if he got to set the pin on state 1 or 2, in order to effectively change the state of the led.
I hope you got better what I mean now, and thank you guys for your answers, I appreciate your big help!

I don't understand the difficulty. When some device tells the Arduino to set the pin HIGH (or LOW), do so by setting a variable to the pin state required and writ that to the pin. It doesn't matter how many devices can set it to whatever state. The question you really need to ask is "How can I guarantee that a pin state remains the same until the inquiring device tells it to change?", which is a different problem altogether.