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

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


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);
    }
  }

}

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

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

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

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

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 .

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

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

led1sw1
Figure-1:

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

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

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

Im stupid I forgot the analogRead :sweat_smile:
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)

  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?

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?

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

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

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

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

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

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