Learn to code

Arduino Home

Lessons 1
Arduino - Getting Started
Lessons 2
Arduino - Basics
Lessons 3
Arduino - Serial Communication
Lessons 4
Arduino - Digital / Analog
Lessons 5
Arduino - Visual Output
Lessons 6
Arduino - Motor Control
Lessons 7
Arduino -LCD Displays
Lessons 8
Arduino -LCD Displays

Detecting light with an LDR Sensor

in Arduino Sensors

Would like to measure the darkness of a room? In this sensor tutorial, we will show you how by using an LDR sensor. It is a very simple tutorial that shows how to measure the amount of light present in a room. 

In order to detect the intensity of light or darkness, we will use a sensor called LDR (Light dependent resistor). The LDR is a special type of resistor that passes higher voltages whenever there is light and passes low voltages in the case of darkness. I will explain the concept below before making showing the code.

How does the LDR sensor work?

This system works by sensing the intensity of light in its environment. The sensor that can be used to detect light is an LDR. It’s inexpensive, and you can buy it from any local electronics store or online.

The LDR will send an analog voltage signal which varies in proportion to the light intensity on it. So, the greater the intensity of the light on it the greater responding analog voltage will be sent to the Arduino Board. 

With its built-in analog-to-digital converter, the voltage from (0-5) will be converted into a digital value in that ranges from 0 – 1023. If you are unfamiliar with reading analog values, I suggest you read the tutorial: Reading analog values.

Hardware Required

  • Arduino Uno
  • Breadboard
  • LDR Sensor
  • hook-up wires
  • 20K-ohm resistor

Circuit Diagram

LDR Sensor Diagram

First, you need to connect the LDR to the analog input pin 0 on the Arduino. You have to use a voltage divider configuration to do this. The connection diagram for the Arduino is as given below.

One leg of the LDR is connected to VCC (5V) on the Arduino, and the other to the analog pin 0 on the Arduino. A 20K resistor is also connected to the leg that is connected with the VCC (5V). The way we use this resistor is interesting since we are using it as a voltage divider

If you look closely at the diagram you will see the 20K resistor and the LDR sensor. The LDR changes its resistance with light and we will measure that using an analog pin of the Arduino Board. But to measure this we need a fixed resistor that we can use for comparison. This is the voltage divider, 10K resistor, that divides the 5v between the LDR and the resistor. Now we can measure the amount of voltage on the LDR by using the analog read function, and we have our reading. The amount of that 5V that each part gets is proportional to its resistance.

With the Arduino analogRead, at 5V (its max) it would read 1023, and at 0v it read 0.

So if the LDR and the resistor have the same resistance, the 5V is split evenly (2.5V), to each part. (analogRead of 512)

But if the LDR is hit with a lot of light and is reading only 1K of resistance, the 20K resistor is going to soak up 20 times as much of that 5V. So the LDR read a lower number.

And if it is in a dark room, the LDR may be 40K or resistance, so the LDR will soak up 2 times as much of that 5V as the 20K resistor. So the LDR a higher number.

Code Example LDR Sensor

After connecting the LDR to your Arduino, you can check for the values coming from the LDR via the Arduino. To do this, connect the Arduino via USB to your PC and open up the Arduino IDE or software. Next, paste this code and upload it to your Arduino:

int sensorPin = A0; // select the input pin for LDR

int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
  Serial.begin(9600); //sets serial port for communication
void loop() {
  sensorValue = analogRead(sensorPin); // read the value from the sensor
  Serial.println(sensorValue); //prints the values coming from the sensor on the screen



After connecting the LDR to your Arduino, you can check for the results in the serial monitor. Remember, the below picture is an example. The results depend on the amount of light that is captured by the LDR. In conclusion, you can receive different results than below because of the light that is captured or you are using a different voltage divider.

LDR Sensor output
Previous Post
Detecting distance with HC-SR04 sensor
Next Post
Detecting touch with Capacitive Touch Sensor (TTP223B)
You must be logged in to post a comment.