Nightlight LED not working

I made an Arduino design that is supposed to make the LED turn on when it gets below a certain light level. This design worked before so it should still work but it doesn't. Instead, the serial monitor that shows me the light level stays at 300-330 no matter what the light level in the room is. This is my design and my Arduino code with the serial monitor:

Is it the sim that does not work or a real circuit with a real Uno?

Pictures of code are suboptimal. Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

We would rather see a schematic. See the how to make a schematic that you can post tutorial.

Change the analogRead(2) to analogRead(A2) or analogRead(16).

For a real Arduino, analogRead(2); will work the same as analogRead(A2); or anaogRead(16);. See the source code in wiring_analog.c.

the real circuit. that image is just a sim of the circuit i made

Are you going to post your code properly so that we can copy the code to our IDE? Can't do much with an image of code.

Your loop is running really fast. I suggest that you put in a delay to run the loop slower. I use the millis() function like in the blink without delay example. Here is 10 samples per second.

const byte ldrPin = A2;
const byte ledPin = 11;

int val = 0;

void setup()
{
   Serial.begin(9600);
   pinMode(ledPin, OUTPUT);   
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 100;
   if (millis() - timer >= interval)
   {
      timer = millis();
      val = analogRead(ldrPin);
      Serial.println(val);
   }   
}

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