Light detector not working

Hello,

I just built a light detector, that basically turns on an LED when it's dark.
It worked completely fine, until I changed the light parameter, and now LED won't turn on, when it should (even after changing it back to the original).

When looking at the serial monitor, the program is doing exactly what it's supposed to, but the LED just doesn't work.
image
I've tried troubleshooting most, if not all the parts, yet nothing works.

Here is the code:

const int lightSensorPin = A0;
const int lightPin = 8;

void setup() {
  Serial.begin(9600);
}

void loop() {
  
  int lightSensorReading = 0;
  lightSensorReading = analogRead(lightSensorPin);
  Serial.println(lightSensorReading);
  delay(100);
  
  if(lightSensorReading < 20){            
    Serial.print("It is dark.");
    digitalWrite(lightPin,HIGH);
  }

  else {
  Serial.print("It is light.");
  digitalWrite(lightPin,LOW);
  }
}

and the schematic:
image

Does anyone know what's wrong?

Thank you,
Mads

lightPin needs to be made an OUTPUT.

void setup() 
{
  Serial.begin(9600);

  pinMode(lightPin, OUTPUT);
}
1 Like

Thank you so much.
It works now!

If your question has been answered to your satisfaction, please mark the thread as solved so that other members that wish to help will not waste their time opening the thread only to find that you no longer require assistance.

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