Arduino - Light Sensor

by DIYables in Circuits > Arduino

14 Views, 0 Favorites, 0 Comments

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.

  1. Official Arduino Uno
  2. DIYables STEM V3, Fully Compatible with Arduino Uno R3
  3. USB 2.0 cable type A/B (for USB-A PC)
  4. USB 2.0 cable type C/B (for USB-C PC)
  5. Light Sensor
  6. 10 kΩ Resistor
  7. Breadboard
  8. Jumper Wires
  9. Screw Terminal Block Shield for Arduino Uno
  10. Sensors/Servo Expansion Shield for Arduino Uno
  11. Breadboard Shield for Arduino Uno
  12. Enclosure for Arduino Uno
  13. Prototyping Base Plate & Breadboard Kit for Arduino UNO
  14. DIYables STEM V3 Starter Kit (Arduino included)
  15. 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.

/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-light-sensor
*/

void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

void loop() {
// reads the input on analog pin A0 (value between 0 and 1023)
int analogValue = analogRead(A0);

Serial.print("Analog reading: ");
Serial.print(analogValue); // the raw analog reading

// We'll have a few threshholds, qualitatively determined
if (analogValue < 10) {
Serial.println(" - Dark");
} else if (analogValue < 200) {
Serial.println(" - Dim");
} else if (analogValue < 500) {
Serial.println(" - Light");
} else if (analogValue < 800) {
Serial.println(" - Bright");
} else {
Serial.println(" - Very bright");
}

delay(500);
}

Full Tutorial

The complete tutorial, wiring diagram and code explanation: https://arduinogetstarted.com/tutorials/arduino-light-sensor