0
0
AutocadHow-ToBeginner · 4 min read

Arduino Project for Automatic Night Lamp: Simple Guide & Code

An automatic night lamp with Arduino uses a photoresistor to detect darkness and turns on an LED or lamp automatically. The Arduino reads the light level and switches the lamp on when it gets dark, and off when it is bright.
📐

Syntax

This project uses these main parts in the code:

  • pinMode(pin, mode): Sets a pin as input or output.
  • analogRead(pin): Reads the value from the light sensor (photoresistor).
  • digitalWrite(pin, value): Turns the LED or lamp on or off.
  • if-else statement: Checks if it is dark or light to control the lamp.
arduino
const int sensorPin = A0;  // Light sensor connected to analog pin A0
const int lampPin = 13;    // LED or lamp connected to digital pin 13

void setup() {
  pinMode(lampPin, OUTPUT);  // Set lamp pin as output
  Serial.begin(9600);        // Start serial communication for debugging
}

void loop() {
  int lightLevel = analogRead(sensorPin);  // Read light sensor value
  Serial.println(lightLevel);               // Print light level

  if (lightLevel < 300) {  // If dark (value less than threshold)
    digitalWrite(lampPin, HIGH);  // Turn lamp ON
  } else {
    digitalWrite(lampPin, LOW);   // Turn lamp OFF
  }
  delay(500);  // Wait half a second before next reading
}
💻

Example

This example shows a complete Arduino program that reads a photoresistor and turns on an LED when it is dark. The LED turns off when it is bright.

Connect the photoresistor between 5V and analog pin A0 with a 10k resistor to ground. Connect an LED with a resistor to digital pin 13.

arduino
const int sensorPin = A0;  // Light sensor connected to analog pin A0
const int lampPin = 13;    // LED connected to digital pin 13

void setup() {
  pinMode(lampPin, OUTPUT);  // Set lamp pin as output
  Serial.begin(9600);        // Start serial communication
}

void loop() {
  int lightLevel = analogRead(sensorPin);  // Read light sensor
  Serial.print("Light Level: ");
  Serial.println(lightLevel);               // Print value

  if (lightLevel < 300) {  // Dark condition
    digitalWrite(lampPin, HIGH);  // Turn LED ON
  } else {
    digitalWrite(lampPin, LOW);   // Turn LED OFF
  }
  delay(500);  // Wait 500ms
}
Output
Light Level: 250 Light Level: 245 Light Level: 260 ... (repeats every 500ms)
⚠️

Common Pitfalls

Common mistakes when making an automatic night lamp include:

  • Using the wrong pin numbers or forgetting to set pinMode.
  • Not calibrating the light sensor threshold; different environments need different values.
  • Incorrect wiring of the photoresistor and resistor causing wrong readings.
  • Forgetting to add a delay in the loop, which can flood the serial monitor.

Always test sensor values with Serial.println to find the right threshold for your room.

arduino
/* Wrong way: No pinMode and no delay */
const int sensorPin = A0;
const int lampPin = 13;

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

void loop() {
  int lightLevel = analogRead(sensorPin);
  Serial.println(lightLevel);
  if (lightLevel < 300) {
    digitalWrite(lampPin, HIGH);  // May not work properly without pinMode
  } else {
    digitalWrite(lampPin, LOW);
  }
  // Missing delay causes too fast serial output
}

/* Right way: */

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

void loop() {
  int lightLevel = analogRead(sensorPin);
  Serial.println(lightLevel);
  if (lightLevel < 300) {
    digitalWrite(lampPin, HIGH);
  } else {
    digitalWrite(lampPin, LOW);
  }
  delay(500);  // Proper delay
}
📊

Quick Reference

Tips for building your automatic night lamp:

  • Use a photoresistor with a 10k resistor in a voltage divider circuit.
  • Connect the sensor to an analog pin and the lamp (LED) to a digital pin.
  • Set the lamp pin as OUTPUT in setup().
  • Read sensor values with analogRead() and decide threshold for darkness.
  • Use digitalWrite() to turn lamp on/off based on sensor reading.
  • Test sensor values with Serial.println() to adjust threshold.

Key Takeaways

Use a photoresistor and Arduino analog input to detect light levels.
Set a threshold value to decide when it is dark enough to turn the lamp on.
Always configure pins correctly with pinMode before use.
Test sensor readings with Serial Monitor to find the right threshold.
Add delays in the loop to avoid flooding outputs and improve stability.