arduinoarduino-unoled

Arduino UNO: Everything is working except light censor


This is a simple Arduino UNO project that can turn on and off the LED light with push button or light censor. The push button is perfectly working it turns on and off the LED light without any problem. The light censor is working too, I debugged it and it can detect light. My problem is even though the light level (>400) is more than the threshold (400), it still does not turn the LED on or off like no effect. (with light 500-700). Here is my Arduino diagram. (The green line is light censor) Arduino Diagram

And Here is my code.

int ledPin = 13;        // LED connected to digital pin 13
int lightSensorPin = A0;  // Photoresistor connected to analog pin A0
int btnPin = 2;         // Button connected to digital pin 2
int threshold = 400;    // Light level threshold (adjust based on your sensor and environment)

int state = LOW;        // Current state of the LED

void setup() {
  pinMode(ledPin, OUTPUT);       // Set the LED pin as an output
  pinMode(btnPin, INPUT_PULLUP); // Set the button pin as input with an internal pull-up resistor
  Serial.begin(9600);            // Initialize serial communication for debugging
}

void loop() {
  int lightLevel = analogRead(lightSensorPin);  // Read the light sensor value
  int buttonState = digitalRead(btnPin);        // Read the button state

  // Debugging: Print the light sensor value and button state
  Serial.print("Light Level: ");
  Serial.print(lightLevel);
  Serial.print(" | Button State: ");
  Serial.println(buttonState);

  // Toggle LED state when button is pressed
  if (buttonState == LOW) {  // Button pressed (active LOW)
    delay(50);  // Debounce delay
    if (digitalRead(btnPin) == LOW) {  // Confirm button is still pressed
      state = !state;  // Toggle the state
      digitalWrite(ledPin, state);  // Update the LED
      while (digitalRead(btnPin) == LOW) {
        // Wait for button release to avoid multiple toggles
      }
    }
  }
  else {
    // Control LED based on light sensor only if the button is not pressed
    if (lightLevel < threshold) {
      digitalWrite(ledPin, LOW);   // Turn on the LED if light level is below the threshold
    } else {
      digitalWrite(ledPin, HIGH);  // Turn off the LED if light level is above the threshold
    }
  }

  delay(100);  // Small delay to smooth the sensor readings
}


Solution

  • I have run your code on an Arduino Uno with the button connected as you have, and with a potentiometer in place of the LDR. As I turn the potentiometer taking the analogue reading past the 400 value, the LED switches accordingly. So your code works.

    In order to get a better idea of what is going on, you could edit your code so that you get an analogue reading to the serial port closer to where you use the value. So try this:

    int ledPin = 13;        // LED connected to digital pin 13
    int lightSensorPin = A0;  // Photoresistor connected to analog pin A0
    int btnPin = 2;         // Button connected to digital pin 2
    int threshold = 400;    // Light level threshold (adjust based on your sensor and environment)
    
    int state = LOW;        // Current state of the LED
    
    void setup() {
      pinMode(ledPin, OUTPUT);       // Set the LED pin as an output
      pinMode(btnPin, INPUT_PULLUP); // Set the button pin as input with an internal pull-up resistor
      Serial.begin(9600);            // Initialize serial communication for debugging
    }
    
    void loop() {
      int lightLevel = analogRead(lightSensorPin);  // Read the light sensor value
      int buttonState = digitalRead(btnPin);        // Read the button state
    /*
      // Debugging: Print the light sensor value and button state
      Serial.print("Light Level: ");
      Serial.print(lightLevel);
      Serial.print(" | Button State: ");
      Serial.println(buttonState);
    */
      // Toggle LED state when button is pressed
      if (buttonState == LOW) {  // Button pressed (active LOW)
        delay(50);  // Debounce delay
        if (digitalRead(btnPin) == LOW) {  // Confirm button is still pressed
          state = !state;  // Toggle the state
          digitalWrite(ledPin, state);  // Update the LED
          while (digitalRead(btnPin) == LOW) {
            // Wait for button release to avoid multiple toggles
          }
        }
      }
      else {
        lightLevel = analogRead(lightSensorPin);
        Serial.print("Light Level: ");
        Serial.println(lightLevel);  //note I am using println here
        
        // Control LED based on light sensor only if the button is not pressed
        if (lightLevel < threshold) {
          digitalWrite(ledPin, LOW);   // Turn on the LED if light level is below the threshold
        } else {
          digitalWrite(ledPin, HIGH);  // Turn off the LED if light level is above the threshold
        }
      }
    
      delay(100);  // Small delay to smooth the sensor readings
    }
    

    I have also commented out the other code sending to the serial port to clarify what is going on.

    As you change the light level you should see the value transition the 400 level, as reported via the serial monitor. At that point you should see the LED change state.

    If you see the serial readout then there is no way that the LED can fail to change state. If you don't see the serial readount, it must be that the (buttonState == LOW) condition is preventing the code from reaching the LDR part.