Arduino - Light Sensor
This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://arduinogetstarted.com/tutorials/arduino-light-sensor
This build uses a photoresistor (LDR) to measure ambient light with an Arduino Uno. The sensor's resistance changes with light level, and the Arduino reads that as an analog value, prints it to the Serial Monitor, and classifies the room as Dark, Dim, Light, Bright, or Very bright.
Supplies
Everything below is what the build uses. The links go to the parts we make and test.
- Official Arduino Uno
- DIYables STEM V3, Fully Compatible with Arduino Uno R3
- USB 2.0 cable type A/B (for USB-A PC)
- USB 2.0 cable type C/B (for USB-C PC)
- Light Sensor
- 10 kΩ Resistor
- Breadboard
- Jumper Wires
- Screw Terminal Block Shield for Arduino Uno
- Sensors/Servo Expansion Shield for Arduino Uno
- Breadboard Shield for Arduino Uno
- Enclosure for Arduino Uno
- Prototyping Base Plate & Breadboard Kit for Arduino UNO
- DIYables STEM V3 Starter Kit (Arduino included)
- DIYables Sensor Kit (18 sensors/displays)
Wiring
The light sensor used here is a photoresistor (LDR), a type of resistor, so its two pins are symmetric and can be connected either way. Since the LDR module alone needs a resistor to work in the circuit, use the 10 kΩ resistor along with it on the breadboard. Wire one side of the photoresistor/resistor pair to the Arduino's A0 analog input pin so the voltage at that junction can be read by the board.
How It Works
A photoresistor's resistance drops as more light hits its face, so measuring that resistance tells you how bright the surroundings are. Arduino Uno's A0-A5 pins work as analog inputs, converting a voltage between 0V and VCC into an integer between 0 and 1023 (the ADC value). By connecting the photoresistor to an analog pin, analogRead() returns this value, letting you track relative light levels.
Code
The sketch starts serial communication at 9600 bps, then in the loop reads the analog value on A0 with analogRead() and prints it to the Serial Monitor. It then checks the value against a set of thresholds and prints a qualitative label: Dark below 10, Dim below 200, Light below 500, Bright below 800, and Very bright above that. A 500 ms delay runs between readings.
arduino-light-sensor.ino - the sketch from the tutorial, unchanged.
Full Tutorial
The complete tutorial, wiring diagram and code explanation: https://arduinogetstarted.com/tutorials/arduino-light-sensor