In this beginner Arduino light sensor project, you will learn how to use an LDR (Light Dependent Resistor) by creating a voltage divider and reading resistance of the LDR using an analogue input on the Arduino Uno.
Have you ever wondered how devices like motion-activated lights work? They use a light sensor to detect changes in light levels. In this tutorial, we'll learn how to use one of the simplest light sensors, called a light dependent resistor (LDR), together with an Arduino board.
An LDR is a component whose resistance changes depending on the amount of light falling on it. We can use this property to detect light levels.
Projects ideas that you could make using a light sensor are:
The Arduino is an open-source electronics platform that allows anyone
to create interactive objects or environments. It has inputs to collect
sensor readings and outputs to control motors, lights and more. By
connecting the LDR to an analog input on the Arduino, we can measure the
LDR resistance values and write code to respond to different light
levels.
We'll start by learning about LDRs and how to physically connect one to
our Arduino. Then we'll install the Arduino IDE software to program the
board. In our code, we'll read the LDR value, map it to a useful range
and display it on the serial monitor. Finally, we'll test our circuit by
covering and uncovering the LDR to see how the readings change.
By the end of this tutorial, you'll have built your first functioning
light sensing project and learned basic skills for integrating sensors
into your Arduino workflows. This opens up possibilities for more
advanced projects involving light detection. I hope you enjoy exploring
how Arduino and LDRs can come together to sense light! Let's get started
by preparing our materials.
An LDR is a Light Dependent Resistor. 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.