Arduino Automatic Night Light: Wiring, Code and Simulator

To build this Arduino Automatic Night Light project you need an Arduino Uno and an LDR light sensor module, a 220 Ω resistor and a yellow LED. A light sensor (LDR) on A0, converted to lux. The LED on pin 9 comes on in the dark. Make the 6 connections in the wiring table, upload the Arduino C++ code, and drag the light slider below 50 lux: the LED switches on.

Open this circuit in the free Arduino Uno simulator

Parts list

QtyPartWhat it does
1Arduino Unothe microcontroller board
1LDR light sensor moduleAO to an analog pin: brighter light reads lower (lux = (50000·10^0.7 / R)^(1/0.7), R from the 10 kΩ divider). DO goes HIGH in the dark.
1220 Ω resistorLimits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down.
1yellow LEDLights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor.

Wiring

#FromTo
1Arduino Uno 5VLDR light sensor module VCC
2Arduino Uno GNDLDR light sensor module GND
3Arduino Uno A0LDR light sensor module AO
4Arduino Uno pin 9220 Ω resistor leg 1
5220 Ω resistor leg 2yellow LED anode (A, long leg)
6Arduino Uno GNDyellow LED cathode (C, short leg)

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add an LDR light sensor module, a 220 Ω resistor and a yellow LED from the parts palette.
  3. Wire 1. Connect Arduino Uno 5V to LDR light sensor module VCC.
  4. Wire 2. Connect Arduino Uno GND to LDR light sensor module GND.
  5. Wire 3. Connect Arduino Uno A0 to LDR light sensor module AO.
  6. Wire 4. Connect Arduino Uno pin 9 to 220 Ω resistor leg 1.
  7. Wire 5. Connect 220 Ω resistor leg 2 to yellow LED anode (A, long leg).
  8. Wire 6. Connect Arduino Uno GND to yellow LED cathode (C, short leg).
  9. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  10. Run it. Press Run. Drag the light slider below 50 lux: the LED switches on.

Arduino C++ code

// Night light: a light sensor on A0 turns the LED on when it gets dark.
const float GAMMA = 0.7;
const float RL10 = 50;
const int LED = 9;

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

void loop() {
  int reading = analogRead(A0);
  // Convert the reading back to lux (see the sensor's hint).
  float volts = reading / 1024.0 * 5;
  float resistance = 2000 * volts / (1 - volts / 5);
  float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, 1 / GAMMA);
  Serial.print("Light: ");
  Serial.print(lux, 0);
  Serial.println(" lux");
  digitalWrite(LED, lux < 50 ? HIGH : LOW);
  delay(250);
}

What to try

Drag the light slider below 50 lux: the LED switches on.

Frequently asked questions

How do I wire the Automatic Night Light project on an Arduino Uno?

Make 6 connections: Arduino Uno 5V to LDR light sensor module VCC; Arduino Uno GND to LDR light sensor module GND; Arduino Uno A0 to LDR light sensor module AO; Arduino Uno pin 9 to 220 Ω resistor leg 1; 220 Ω resistor leg 2 to yellow LED anode (A, long leg); Arduino Uno GND to yellow LED cathode (C, short leg).

What parts do I need for this Arduino project?

An Arduino Uno and an LDR light sensor module, a 220 Ω resistor and a yellow LED.

Can I simulate this Arduino project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.

What does the code do?

A light sensor (LDR) on A0, converted to lux. The LED on pin 9 comes on in the dark.

More Arduino Uno projects