Arduino light sensor. A beginners project on using a light sensor with an Arduino Uno


Introduction

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.

How read the output from a LDR light sensor with an 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:

* Automatic Nightlight.
* Light Intensity Meter.
* Light-Activated Alarm.
* Basic Solar Tracker.

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.

What is an LDR

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.

Required Components

  • Arduino Uno board.
  • Light dependent resistor (LDR) - 10k version.
  • Resistor (e.g. 10k ohm).
  • Jumper wires.

Arduino light sensor: Circuit Diagram

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.

arduino light sensor using LDR
Diagram using fritzing

The schematic shows the pin connections and components.
arduino light sensor ldr schematic

Diagram using fritzing

Arduino light sensor: Example Sketch

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).

#define ldrPin A0

void setup() {
  Serial.begin(9600);  
}

float LDR_Resistance(int reading) {
  float ratio = (float)reading/1024.0;
  float resistance = 10000 * ratio/ (1-ratio);
  return resistance;
}

void loop() {

  int reading = analogRead(ldrPin);
  float res = LDR_Resistance(reading);  

  // Send value to serial port
  Serial.print("LDR resistance ");
  Serial.println(res);

  delay(1000);

}

sketch ldr.ino

Short Code Explanation

  • The LDR is connected to analog pin A0.
  • A function calculates resistance using a voltage divider formula.
  • It takes readings and calculates resistance in loop().
  • Resistances are printed to serial monitor.
  • The process repeats with a delay between iterations.
  • This allows monitoring changing resistance over time in response to light changes.

Detailed Code Explanation


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:

  • #define ldrPin A0 - LDR is connected to analog pin A0

In setup(), it initializes serial communication at 9600 baud to print values later:

  • Serial.begin(9600)

The key function is LDR_Resistance() which calculates the actual resistance value from the raw reading. It does this using the voltage divider formula:

  • Takes the raw reading and calculates the voltage ratio
  • Applies the resistance formula using the ratio and fixed resistor value
  • Returns the calculated resistance

In loop():

  • It takes a reading from the LDR pin.
  • Passes the reading to the function to calculate resistance.
  • Prints the resistance value to the serial monitor.
  • Adds a delay to take periodic readings.

By calculating and printing the actual resistance over time, you can monitor how the LDR resistance changes in response to light level variations.

Arduino light sensor: Uploading the Code

There are a few steps to uploading the code using the Arduino IDE:

  • Connect the Arduino Uno to the PC with a USB cable.
  • Select the Arduino Uno hardware.
  • Open a new sketch.
  • Paste the code above into the new page (overwrite everything).
  • Press the upload button (right arrow at top).

You can find a more detailed tutorial on the Arduino IDE page.

Testing the Circuit

Connect up as shown and shine a light or torch on the LDR to see different values of resistance of the LDR.

Arduino light sensor: Conclusion

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.

B3



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.



Privacy Policy | Contact | About Me

Site Map | Terms of Use