DEV Community

Cover image for Arduino Nano - Light Sensor
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

Arduino Nano - Light Sensor

This tutorial wires a photoresistor, also called an LDR or photocell, to an Arduino Nano so you can read ambient light levels in code. It is a nice first analog sensor project because the hardware is just a resistor whose value changes with light, and the Nano turns that into a number you can log, threshold, or use to trigger something else like an LED.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino Nano - Light Sensor

What you need

Wiring

Arduino Nano - Light Sensor wiring diagram

The photoresistor is a type of resistor with two pins, and since it is symmetrical, it does not matter which pin goes where. It works together with a 10 kΩ resistor on the breadboard to form a voltage divider, and one pin of the photoresistor connects to one of the Nano's analog input pins, A0 in the example code. The Nano's A0 through A7 pins can all read analog voltages, so you are not limited to A0 if your wiring uses a different pin.

How it works

A photoresistor's resistance changes depending on how much light hits its face: more light means less resistance, less light means more resistance. Wired into a voltage divider, that changing resistance changes the voltage at the analog pin. The Nano's analog input converts that voltage, somewhere between 0 volts and VCC, into an integer between 0 and 1023 using analogRead(), giving you a relative measurement of brightness rather than an absolute one.

Code

The sketch starts Serial.begin(9600) so it can talk to the Serial Monitor, then in loop() it calls analogRead(A0) to grab the current light reading. That raw number is printed, and then compared against a handful of thresholds to label the reading as Dark, Dim, Light, Bright, or Very bright, printed with Serial.println(). A delay(500) between readings keeps the output readable instead of flooding the monitor.

arduino-nano-light-sensor.ino

/*
 * This Arduino Nano code was developed by newbiely.com
 *
 * This Arduino Nano code is made available for public use without any restriction
 *
 * For comprehensive instructions and wiring diagrams, please visit:
 * https://newbiely.com/tutorials/arduino-nano/arduino-nano-light-sensor
 */

void setup() {
  // Initialize the Serial to communicate with the Serial Monitor.
  Serial.begin(9600);
}

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

  Serial.print("Analog reading: ");
  Serial.print(analog_value);   // The raw analog reading

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

  delay(500);
}
Enter fullscreen mode Exit fullscreen mode

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-nano/arduino-nano-light-sensor

Top comments (0)