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


Introduction

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.

How read the output from a LDR light sensor with an Arduino Uno

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:

* Automatic nightlight lights that turn on in the dark.
* Light intensity meter to measure brightness.
* Light-activated alarm for security systems.
* Basic solar trackers that follow the sun.

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.

What is an LDR

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.

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