Automatic Night Light using Arduino

Materials:

  • Arduino board (e.g. Uno)
  • Light sensor module (e.g. Photoresistor)
  • LED or a series of LEDs
  • Resistor (220 ohm)
  • Jumper wires
  • Breadboard (optional)

Steps:

  1. Connect the light sensor module to the Arduino board. The light sensor module usually has three pins: VCC, GND, and Signal. Connect the VCC to the 5V pin on the Arduino, and connect the GND to the GND pin on the Arduino. Then, connect the Signal pin to an analog input pin on the Arduino, such as A0.
  2. Connect the LED or series of LEDs to the Arduino board. Place the LED’s anode (positive leg) in one of the digital output pins, such as pin 13. Then, connect the LED’s cathode (negative leg) to a resistor (220 ohm) and connect the other end of the resistor to the GND pin on the Arduino.
  3. Open the Arduino IDE and create a new sketch.
  4. Define the pins used for the light sensor module and the LED. Add the following code to the top of your sketch:
const int lightSensorPin = A0; // Pin for light sensor module
const int ledPin = 13; // Pin for LED
  1. In the setup() function, initialize the LED pin as an output. Add the following code to the setup() function:
pinMode(ledPin, OUTPUT);
  1. In the loop() function, read the analog value from the light sensor module using the analogRead() function. The analog value ranges from 0 to 1023, with 0 being completely dark and 1023 being very bright. Store the analog value in a variable. Add the following code to the loop() function:
int lightValue = analogRead(lightSensorPin);
  1. Use an if statement to check if the light level is below a certain threshold. If it is, turn on the LED. If it is not, turn off the LED. Add the following code to the loop() function:
if (lightValue < 500) {
  digitalWrite(ledPin, HIGH);
} else {
  digitalWrite(ledPin, LOW);
}
  1. Upload the sketch to the Arduino board.
  2. Test the night light by covering the light sensor module with your hand. The LED should turn on automatically.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *