In this beginner-friendly Arduino light sensor project, you will learn how to use a Light Dependent Resistor (LDR). By creating a voltage divider and and connecting the LDR to an analogue input on the Arduino Uno, you'll measure light levels and see results in real-time.
Have you ever wondered how devices like motion-activated lights work? These devices rely on light sensors to detect changes in brightness. In this tutorial, you'll learn how to use an LDR, a basic yet powerful component, to create your own light sensing projects.
An LDR is a special resistor that changes its resistance based on the amount of light falling on it. By connecting it to the Arduino’s analog input pin, you can measure these changes and program the Arduino to respond to varying light levels. .
Here are just a few of the exciting projects you can create using a light sensor:
In this setp-by-step guide you will starting with understanding how an LDR works and then connecting it to your Arduino. Next, you’ll write a simple program using the Arduino IDE to monitor light levels. By the end, you’ll have built a functioning light sensor and gained valuable skills to expand into more advanced Arduino projects.
An LDR is a Light Dependent Resistor - the more light that shines on it the lower its resistance becomes. There are
some materials that were discovered to be reactive to light - the one
used in most Arduino light sensor for an LRDs is Cadmium Sulphide
because it is cheap and reacts to normal visible light. Other materials
can react better to Infrared or other light wavelengths.
The interesting property of an LDR is that the resistance of the
material falls with increased light intensity. You can think of it as
adding energy to the electrons making them easier to move in the
material; R=V/I so as I increases R falls.
The layout diagram shows how to place components on the breadboard.
Ground (0V) and supply voltage (5V) are connected from pins in the lower
pin header of the Arduino Uno, while analogue input pin A0 connects to
the LDR and the 10k resistor.
The schematic shows the pin connections and components.
Diagram using fritzing
You can copy and paste the code below into the Arduino IDE (in a new
sketch) replacing everything that is in the new sketch window (See "Uploading the code" below).
sketch ldr.ino
This is a more detailed explanation of the code operation, explaining all the code used in the Arduino light sensor tutorial.
The code reads the resistance value of a Light Dependent Resistor (LDR) over time and prints it to the serial monitor periodically.
It first defines a constant:
In setup(), it initializes serial communication at 9600 baud to print values later:
The key function is LDR_Resistance() which calculates the actual resistance value from the raw reading. It does this using the voltage divider formula:
In loop():
By calculating and printing the actual resistance over time, you can monitor how the LDR resistance changes in response to light level variations.
There are a few steps to uploading the code using the Arduino IDE:
You can find a more detailed tutorial on the Arduino IDE page.
Connect up as shown and shine a light or torch on the LDR to see different values of resistance of the LDR.
In this beginner Arduino light sensor project, you learned how to use a light
dependent resistor to sense light levels and interface it with an
Arduino board.
By connecting the LDR in a voltage divider circuit and reading the
analog values, you are able to monitor how its resistance changes in
response to light. The code calculates the actual resistance over time
and prints it to the serial monitor.
This tutorial covered the basics of setting up an LDR light sensor,
writing the code to read values, and testing the live output.
Building this simple circuit and program provides a foundation for
developing more complex projects involving light detection. With further
learning, examples like automatic lighting, photometers and solar
trackers can be realized.
Written by John Main who has a degree in Electronic Engineering.
Note: Parts of this page were written using claude-instant as a research assistant.
Comments
Have your say about what you just read! Leave me a comment in the box below.
Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.