Arduino Lesson 8 – Using LDR Sensor and Serial Monitor Readings
Using LDR Sensor and reading values on Serial Monitor
Welcome to Lesson 8 – Basic Arduino Course
In today’s lesson, we will learn how to use an LDR sensor and read values on a Serial Monitor with Arduino.
To measure light intensity, we will use the famous and widely used low-cost LDR (Light Dependent Resistor) sensor, to detect the intensity of light or darkness easily and cheaply.
LDR (Light Dependent Resistor) Sensor
LDR is a special type of resistor that passes higher voltage (low resistance) when light intensity is high and low voltage (high resistance) when darkness is low.
With this method, we can use it for example in projects such as:
- Lack of Light Alarm – Triggers an alarm when power is cut.
- Brightness Control – Used to control the brightness of a recording or filming environment for example.
- Contrast Control Screen – Often used on mobile phones to automatically lower or increase the brightness of the Display.
- Emergency Light – Automatically turns on when Light is cut.
- Ticket Counter – Also used to count people at the entrance of an establishment.
- Invasion Alarm – Used to trigger an alarm when there is an invasion of a monitored establishment.
In today’s example, we will use this sensor to measure numerical resistance and read these values from the serial monitor.
To do this, we will use Arduino analog port A0. We can use any analog port, in the case of Arduino Uno there are 6 ports; A0 to A5.
Hardware Required
- Arduino Board
- LDR – Sensor Dependent Light
- 100K ohms resistor – (Brown, black, yellow, gold)
- Jumper Wires
- Protoboard (optional)
The Circuit Connections
The circuit is very simple, we connect one leg of the LDR to positive 5V, and another leg to a 100K resistor in series with negative GND, and the same leg that takes the resistor and LDR, we connect to port A0, as shown in Figure 2 below.
Fig. 2 – Using LDR Sensor and reading values on Serial Monitor – tinkercad.com
We use a protoboard to facilitate the connections, but you can also connect the wires directly to the Arduino.
The Code
The analogRead() function reads the value from the specified analog pin. Arduino boards contain a multichannel, 10-bit analog to digital converter. This means that it will map input voltages between 0 and the operating voltage (5V or 3.3V) into integer values between 0 and 1023.
On an Arduino UNO, for example, this yields a resolution between readings of: 5 volts / 1024 units or, 0.0049 volts (4.9 mV) per unit.
After building the circuit, connect your Arduino board to your computer, launch Arduino Software (IDE), copy the code below and paste it into your Arduino IDE. But first let us understand the code line by line.
- In Line 3, we declared sensorPin to select the analog input Pin A0 to connect the LDR.
- In Line 4, we declared sensorValue as a variable to store the value coming from the LDR sensor.
- In Line 6, we enter the void setup() function. This function is read only once when the Arduino is started.
- In Line 7, we set the serial port for communication, we will read the numeric value from the LDR sensor.
// Arduino Lesson 8 - Using LDR Sensor and reading values on Serial Monitor with Arduino
int sensorPin = A0; // Select the Analog input pin for LDR
int sensorValue = 0; // Variable to store the value coming from the sensor
void setup() { // This function is called once when the program starts
Serial.begin(9600); // Sets Serial Port for communication with bounce rate in 9600
}
void loop() { // The loop function runs over and over again as long as the Arduino has power.
sensorValue = analogRead(sensorPin); // Read the value from the sensor
Serial.println(sensorValue); // Prints the values coming from the sensor on the Serial Monitor
}
//------------------------------------- www.elcircuits.com --------------------------------------------
- In Line 10, we enter in the loop() function does precisely what its name suggests, and loops consecutively.
- In Line 11, the sensorValue variable receives the value read from the sensorPin, which is the analog input Pin, and stores the read values.
- In Line 12, the Serial.println() function prints the values from the variable sensorValue on the serial monitor screen.
Below you can see the full code, which we can be copying and pasting into your Arduino IDE, and uploading to the Arduino.
// Arduino Lesson 8 - Using LDR Sensor and reading values on Serial Monitor with Arduino
int sensorPin = A0; // Select the Analog input pin for LDR
int sensorValue = 0; // Variable to store the value coming from the sensor
void setup() { // This function is called once when the program starts
Serial.begin(9600); // Sets Serial Port for communication with bounce rate in 9600
}
void loop() { // The loop function runs over and over again as long as the Arduino has power.
sensorValue = analogRead(sensorPin); // Read the value from the sensor
Serial.println(sensorValue); // Prints the values coming from the sensor on the Serial Monitor
}
//------------------------------------- www.elcircuits.com --------------------------------------------
All ready! After you have assembled the entire circuit, and uploaded this code, open the Serial Monitor and what you will see is the numerical value of the LDR.
When you approach your hands to the LDR, inhibiting the light, the number will drop to the minimum possible, it will depend on the LDR, and when you shine a light on the sensor, the number will go to the maximum of the sensor.
With that, the possibilities are immense to work with this sensor.
Next Lesson
Previous Lesson
- Arduino: Lesson 5 – Reading Potentiometer and Showing Values On Serial Monitor
- Arduino: Lesson 6 – How to use Analog Output to fade an LED
- Arduino: Lesson 7 – Controlling LED Through Serial Monitor with Arduino
✨ Our Gratitude and Next Steps
We sincerely hope this guide has been useful and enriching for your projects! Thank you for dedicating your time to this content.
Your Feedback is Invaluable:
Have any questions, suggestions, or corrections? Feel free to share them in the comments below! Your contribution helps us refine this content for the entire ElCircuits community.
If you found this guide helpful, spread the knowledge!
🔗 Share This Guide
Best regards,
The ElCircuits Team ⚡

Português
Español


