# How do you detect if there is a connection from the Output Pin to the LED

**URL:** <https://forum.arduino.cc/t/how-do-you-detect-if-there-is-a-connection-from-the-output-pin-to-the-led/1045716>\
**Category:** General Guidance\
**Created:** [October 25, 2022, 6:19am UTC](https://forum.arduino.cc/t/how-do-you-detect-if-there-is-a-connection-from-the-output-pin-to-the-led/1045716 "2022-10-25T06:19:53Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![emilymusic](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@emilymusic](https://forum.arduino.cc/u/emilymusic)\
**Post date:** [October 25, 2022, 6:19am UTC](https://forum.arduino.cc/t/how-do-you-detect-if-there-is-a-connection-from-the-output-pin-to-the-led/1045716/1 "2022-10-25T06:19:53Z")

</div>

I'm wondering how to make it so that if I pull out the connection of the Output pin to the LED that I want to use (the Green LED in this one), then the Arduino returns an Error in the Serial Monitor and does the same thing when I pull the GND connection from either my Breadboard or either LED. Is there a way to consistently detect this?  
(code attached below)  
Thanks in advance,

Emily

```cpp

int Green = 2; // Green LED Pin 2
int Red = 4; // Red LED Pin 4
int Time = 2000; // Time for Fuction LED Blinking = 2s (2000ms)
int Time2 = 500; // Time for Error LED Blinking = 0.5s (500ms)
int GNDcheckGreen = 3; // GND Check Green on Pin 3
int GNDcheckRed = 5; // GND Check Red on Pin 5
//int GNDcheckBzz = 10; // GND Check Buzzer on Pin 10
int VoltcheckGreen = 7; // 5V Check Green on Pin 7
int VoltcheckRed = 8; // 5V Check Red on Pin 8
int Bzz = 9; // Buzzer on Pin 9
int ledSate= LOW; // LED State
bool SUGcheckG; // S(tart)U(p)G(round)checkG(reen)
bool SUGcheckR; // S(tart)U(p)G(round)checkR(ed)

unsigned long prevMillis = 0; // Save previousMillis in a Variable

void setup() {

  Serial.begin(9600); // Serial Monitor on
  pinMode(Green, OUTPUT); // GreenLED = Output
  pinMode(Red, OUTPUT); // RedLED = Output
  pinMode(LED_BUILTIN, OUTPUT); // Builtin LED on Board = Output
  pinMode(GNDcheckGreen, INPUT_PULLUP); // Check for GND as Input_Pullup
  pinMode(GNDcheckRed, INPUT_PULLUP);
  pinMode(GNDcheckBzz, INPUT_PULLUP);
  pinMode(VoltcheckGreen, INPUT); // Check if there's a connection from the Output to the LED 
  pinMode(VoltcheckRed, INPUT);
  pinMode(Bzz, OUTPUT); // Buzzer as Output
  Serial.println("--------------------");
  Serial.println("Test Green Voltage connection:");
  if (digitalRead(VoltcheckGreen == LOW)) {
    Serial.println("Green LED receives Voltage");
  }
  else {
    Serial.println("Green LED does not receive Voltage");
  }
  Serial.println("--------------------"); //
  Serial.println("Test Green GND connection:");
  if (SUGcheckG == 0) {
    Serial.println("GND is not connected to the Green LED");
  }
  else {
    Serial.println("GND is connected to the Green LED");
  }
  Serial.println("--------------------");
  Serial.println("Test Red Voltage connection:");
  if (digitalRead(VoltcheckRed) == LOW) {
    Serial.println("Red LED receives Voltage");
  }
  else {
    Serial.println("Green LED does not receive Voltage");
  }
  Serial.println("--------------------"); //
  Serial.println("Test Red GND connection:");
  if (SUGcheckR == 0) {
    Serial.println("GND is not connected to the Red LED");
  }
  else {
    Serial.println("GND is connected to the Red LED");
  }
  Serial.println("--------------------");
  Serial.println("");
  Serial.println("--------------------"); // Feedback in Serial Monitor when the Setup is done
  Serial.println("Setup Done");
  Serial.println("--------------------");
  Serial.println("");
}

void loop() {

  unsigned long currMillis = millis();
  // Current LED State in Serial Monitor
  if (digitalRead(GNDcheckGreen) == LOW) {

    if (currMillis - prevMillis >= Time) {

      // save the last time you blinked the LED
      prevMillis = currMillis;

      if (ledState == LOW) {
        ledState = HIGH;
        Serial.println("LED on");
        Serial.println("");
      }
      else {
        ledState = LOW;
        Serial.println("LED off");
        Serial.println("");
      }
      digitalWrite(Green, ledState);
    }
  }

  if (digitalRead(GNDcheckGreen) == HIGH || digitalRead(GNDcheckRed) == HIGH) { // If either input detects a HIGH signal show errors
    Serial.println("GND is not connected to the Breadboard or LEDs or the cable is severely damaged");
    Serial.println("");
    digitalWrite(Green, LOW);
    while (digitalRead(GNDcheckGreen) == HIGH || digitalRead(GNDcheckRed) == HIGH) {
      digitalWrite(Red, HIGH);
      digitalWrite(LED_BUILTIN, HIGH);
      tone(Bzz, 220);
      digitalWrite(Green, HIGH);
      delay(Time2);
      digitalWrite(Red, LOW);
      digitalWrite(LED_BUILTIN, LOW);
      noTone(Bzz);
      digitalWrite(Green, LOW);
      delay(Time2);
    }
  }

}
```

---

<div class="post-metadata">

**Author:** ![lastchancename](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lastchancename/32/215426_2.png) [@lastchancename](https://forum.arduino.cc/u/lastchancename)\
**Post date:** [October 25, 2022, 6:34am UTC](https://forum.arduino.cc/t/how-do-you-detect-if-there-is-a-connection-from-the-output-pin-to-the-led/1045716/2 "2022-10-25T06:34:10Z")

</div>

How’s the LED wired?  
Resistor, Vcc or ground ?

---

<div class="post-metadata">

**Author:** ![emilymusic](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@emilymusic](https://forum.arduino.cc/u/emilymusic)\
**Post date:** [October 25, 2022, 6:35am UTC](https://forum.arduino.cc/t/how-do-you-detect-if-there-is-a-connection-from-the-output-pin-to-the-led/1045716/3 "2022-10-25T06:35:10Z")

</div>

There is a 150 Ohm resistor from the output to the led and then another one behind it then going to ground

---

<div class="post-metadata">

**Author:** ![hammy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/hammy/32/238813_2.png) [@hammy](https://forum.arduino.cc/u/hammy)\
**Post date:** [October 25, 2022, 7:22am UTC](https://forum.arduino.cc/t/how-do-you-detect-if-there-is-a-connection-from-the-output-pin-to-the-led/1045716/4 "2022-10-25T07:22:29Z")

</div>

Take the connection to the led and connect to an analog input ?

---

<div class="post-metadata">

**Author:** ![emilymusic](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@emilymusic](https://forum.arduino.cc/u/emilymusic)\
**Post date:** [October 25, 2022, 7:31am UTC](https://forum.arduino.cc/t/how-do-you-detect-if-there-is-a-connection-from-the-output-pin-to-the-led/1045716/5 "2022-10-25T07:31:26Z")

</div>

Just now did that and made the Serial Monitor read out the value of the Analog pin but it now only gives back a value of 19 even if it is connected or not (also tried to completely disconnect the cable from the analog pin just to test but same thing)

Edit: Also tried a different pin (from A5 to A0) and now it just shows 14 for some reason

---

<div class="post-metadata">

**Author:** ![hammy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/hammy/32/238813_2.png) [@hammy](https://forum.arduino.cc/u/hammy)\
**Post date:** [October 25, 2022, 8:18am UTC](https://forum.arduino.cc/t/how-do-you-detect-if-there-is-a-connection-from-the-output-pin-to-the-led/1045716/6 "2022-10-25T08:18:52Z")

</div>

You should have output pin for the led . Connect this  
Pin then to 330ohm resistor, then led then 0v .  
Take the analog input from between the resistor and led .

( analogRead) Should read around 250-300 .

---

<div class="post-metadata">

**Author:** ![emilymusic](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@emilymusic](https://forum.arduino.cc/u/emilymusic)\
**Post date:** [October 25, 2022, 8:26am UTC](https://forum.arduino.cc/t/how-do-you-detect-if-there-is-a-connection-from-the-output-pin-to-the-led/1045716/7 "2022-10-25T08:26:06Z")

</div>

So after the Cable from the Output pin I now got a 330 Ohm resistor then analog pin and led after and then to 0V yet it still reads it as a value of 14. Any idea why it still says that. It's as if the Analog pin was not connected because it does read the same value when connected and disconnected

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [October 25, 2022, 8:39am UTC](https://forum.arduino.cc/t/how-do-you-detect-if-there-is-a-connection-from-the-output-pin-to-the-led/1045716/8 "2022-10-25T08:39:15Z")

</div>

**1.** This is a typical setup (FIg-1).

![led1sw1](https://europe1.discourse-cdn.com/arduino/original/4X/b/b/5/bb5cac32df4b15392ab39a538afd15295185b38c.png)  
Figure-1:

**2.** Analysis  
**(1)** Without load at DPin-4

```cpp
Voltage at DPin-4 ~= 5.0V
Voltage at A0-pin ~= 5.0V
Serial.print(ananlogRead(A0)) will show ~= 1023.

```

**(2)** With load connected at DPin-4

```cpp
Volatge at DPin-4 ~= 5-2.5 = 4.75 V.
Voltage at A0-pin ~= 4.75 V
Serial.print(ananlogRead(A0)) will show ~= (1023/5*4.75) = 972

```

---

<div class="post-metadata">

**Author:** ![emilymusic](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@emilymusic](https://forum.arduino.cc/u/emilymusic)\
**Post date:** [October 25, 2022, 8:49am UTC](https://forum.arduino.cc/t/how-do-you-detect-if-there-is-a-connection-from-the-output-pin-to-the-led/1045716/9 "2022-10-25T08:49:34Z")

</div>

Im stupid I forgot the analogRead 😅  
Now since the led toggles on and off, which it should do here, it reads 0 and then a value of 416-419, I now added code so it triggers when the value is not that OR when it is at 1023 which is when the LED itself is disconnected yet when the LED is disconnected it still toggles on the pin so it only does the error code for ~2 seconds (when LED itself is disconnected). How can I make it detect the missing LED so it is stuck in the ERROR loop (code below)  
(Red, Bzz, Time2 and Green are definied in the main code above this is just when there is an error)

```cpp
  if ((analogRead(A0) > 0 && analogRead(A0) < 415) || analogRead(A0) > 419) {
    Serial.println("Connection to the Green LED broken or disconnected");
    Serial.println("");
    while ((analogRead(A0) > 0 && analogRead(A0) < 415) || analogRead(A0) > 419) {
      digitalWrite(Red, HIGH);
      digitalWrite(LED_BUILTIN, HIGH);
      tone(Bzz, 220);
      digitalWrite(Green, HIGH);
      delay(Time2);
      digitalWrite(Red, LOW);
      digitalWrite(LED_BUILTIN, LOW);
      noTone(Bzz);
      digitalWrite(Green, LOW);
      delay(Time2);
    }
  }

```

Edit: I also noticed that while the LED is off it sends "Connection to the Green LED broken or disconnected" in the Serial monitor whenever possible, is there a way to prevent that too?

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [October 25, 2022, 9:22am UTC](https://forum.arduino.cc/t/how-do-you-detect-if-there-is-a-connection-from-the-output-pin-to-the-led/1045716/10 "2022-10-25T09:22:02Z")

</div>

well you could say that if the LED detection thingy is changing every X millis() then it's all right but if it does not change every x millis() then its not all right. Right? Seeing as how the normal operations is change when its all good, not changing is????

You could say ok 3 not changes then it is all bad, no need to check if its still changing now that it is fault and only print message when it is a fault?

---

<div class="post-metadata">

**Author:** ![bluejets](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bluejets/32/1013044_2.png) [@bluejets](https://forum.arduino.cc/u/bluejets)\
**Post date:** [October 25, 2022, 10:46am UTC](https://forum.arduino.cc/t/how-do-you-detect-if-there-is-a-connection-from-the-output-pin-to-the-led/1045716/11 "2022-10-25T10:46:09Z")

</div>

> [@GolamMostafa](#):
>
> **(2)** With load connected at DPin-4
> 
> ```arduino
> Volatge at DPin-4 ~= 5-2.5 = 4.75 V.
> 
> ```

Are you sure..??  
And I would not expect to see any change....

---

<div class="post-metadata">

**Author:** ![emilymusic](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@emilymusic](https://forum.arduino.cc/u/emilymusic)\
**Post date:** [October 25, 2022, 11:33am UTC](https://forum.arduino.cc/t/how-do-you-detect-if-there-is-a-connection-from-the-output-pin-to-the-led/1045716/12 "2022-10-25T11:33:44Z")

</div>

After tweaking some value for tolerance I got it working now, thanks alot!

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [October 25, 2022, 2:04pm UTC](https://forum.arduino.cc/t/how-do-you-detect-if-there-is-a-connection-from-the-output-pin-to-the-led/1045716/13 "2022-10-25T14:04:16Z")

</div>

> [@bluejets](#):
>
> And I would not expect to see any change....

My previous calculation of Section-2(2) post #8 is flawed. It should be:

With load connected, voltage at DPin-4 is approx:

```arduino
= 5.0 - ((5.0-4.2)/.020)x(5-2.5)/2200
~= 4.95 V.

```

Yes! There is not much change as you have anticipated.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 23, 2023, 2:04pm UTC](https://forum.arduino.cc/t/how-do-you-detect-if-there-is-a-connection-from-the-output-pin-to-the-led/1045716/14 "2023-04-23T14:04:58Z")

</div>

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