#define LDR_PIN A0 #define LED_PIN 9 // You can't know this threshold // by heart. Calibrate it for your room's // light from the values you // read in the Serial Monitor. const int DARK_THRESHOLD = 400; void setup() { pinMode(LED_PIN, OUTPUT); Serial.begin(9600); } void loop() { int ldrValue = analogRead(LDR_PIN); // Watch the sensor // reading while debugging Serial.print("LDR value: "); Serial.println(ldrValue); // In the dark the LDR's resistance // goes up: the voltage on A0 drops, // so the analog reading gets smaller. if (ldrValue < DARK_THRESHOLD) { // A night light: instead of full // on/off with digitalWrite, PWM // (0-255) limits the // brightness to a dim glow. analogWrite(LED_PIN, 100); } else { // the room is bright: off analogWrite(LED_PIN, 0); } // a pause so the ADC isn't hammered // and the Serial Monitor stays readable delay(200); }