If you've ever wondered how to efficiently measure the light level in an environment with Arduino, you've come to the right place. In this article, we'll explain step by step how to do it using a LDR photoresistor, also known as a photoresistor. These little technological marvels are electronic components capable of changing their resistance depending on the amount of light they receive, opening up endless possibilities for electronic and automation projects.
The applications of the light sensor with Arduino are many: from automatic lighting systems to robots that orient themselves based on light. The best of all is that it is an affordable and easy-to-use component. Here we will give you all the necessary information so that you can build your own light measurement system with Arduino and take advantage of its full potential.
What is an LDR and how does it work?
A Light Dependent Resistor (LDR) is a resistor whose resistance varies depending on the amount of light falling on it. In darkness, the resistance is very high, reaching values ​​of up to 1 MOhm. Conversely, when the LDR receives abundant light, the resistance decreases considerably, reaching values ​​between 50 and 100 ohms under bright light.
Its operation is based on the principle of conductivity of semiconductor materials. When receiving light, the photons energize the electrons of the material, which facilitates the flow of current and therefore decreases the resistance. This type of sensor is very useful for applications where a relative measurement of light in the environment is required.
Features of LDR
No products found.
This component is very popular due to its low cost and ease of use. Typical resistance values ​​range from 1 MOhm in complete darkness to 50-100 ohms in bright light . However, it's worth noting that these sensors aren't the most accurate if you're looking to precisely measure illuminance (light in lux), as they can be affected by factors such as temperature.
The resistance variation is quite slow , taking between 20 and 100 milliseconds depending on the model. This means it's not suitable for detecting rapid changes in light, such as those produced under AC-powered lights, but it offers excellent stability in more constant light conditions.
While LDRs are better suited for measuring light trends than providing precise data, their low cost and ease of integration with Arduino boards make them an ideal sensor for DIY projects.
Circuit and Connection Diagram
In order for the Arduino to measure the resistance variation of the LDR, it is necessary to mount the sensor on what is known as a voltage divider. This is a very simple circuit consisting of the LDR and a fixed resistor connected in series. The LDR is placed between the input voltage (e.g. 5V on the board) and the resistor. Arduino Uno) and the analog input pin, and the fixed resistor is connected between the pin and ground (GND).
The value of the fixed resistor is usually 10 kOhms, although it can vary depending on the sensitivity you want to achieve in your measurement.
Assembly and Code Examples
To build a basic system with Arduino and an LDR, the first thing you need to do is connect the following elements:
- One end of the LDR to 5V supply.
- The other end of the LDR to the analog input (A0, for example) and at the same time to a fixed resistor that will be connected to ground.
With this setup you can start reading the values ​​that the LDR provides through the analog input. The following code is a basic example to read those values:
const int pinLDR = A0;
void setup() {
Serial.begin(9600); // Iniciar monitor serie}
void loop() {
int valorLDR = analogRead(pinLDR); // Leer valor de LDR
Serial.println(valorLDR); // Imprimir valor en monitor
delay(500);
}
This code will print values ​​between 0 (i.e. when there is no light) and 1023 (maximum light received). These values ​​are proportional to the light perceived by the LDR.
The behavior of the resistance depending on the light
As mentioned earlier, the resistance of an LDR decreases as it receives more light. To obtain an accurate measurement of the amount of light , you need to know the resistance values ​​of your LDR under different lighting conditions.
In the GL55 series, for example, values ​​range from 5 kΩ to 200 kΩ in the presence of light and from 500 kΩ to 10 MΩ in dark conditions. These values ​​may vary from one model to another, so it is always advisable to consult the sensor manufacturer's data sheet.
An interesting characteristic of LDRs is that their sensitivity is greatest in the green portion of the light spectrum , approximately at wavelengths of 540 nm. This means that LDRs respond better to green light than to other parts of the visible spectrum.
Practical applications
The potential applications of LDRs connected to an Arduino are almost endless. Among the most practical are automatic lighting systems, where the circuit can activate or deactivate lights based on detected light levels. They are also used in light-following robots and home automation systems.
You can, for example, create a system where as light levels decrease, an LED turns on to compensate for the lack of light. Here is a simple code example:
int LDRPin = A0; // Pin para la LDR
int LEDPin = 13; // Pin para el LED
int threshold = 500; // Umbral para encender el LED
void setup() {
pinMode(LEDPin, OUTPUT);
pinMode(LDRPin, INPUT);}
void loop() {
int valorLuz = analogRead(LDRPin);
if (valorLuz < threshold) {
digitalWrite(LEDPin, HIGH); // Enciende el LED
} else {
digitalWrite(LEDPin, LOW); // Apaga el LED
}
delay(100);}
This small program reads the value from the LDR and if the light level is lower than the set threshold, it turns on the LED. Otherwise, it turns it off. An easy but highly functional example for lighting automation projects.
Limitations and precautions
Although the use of an LDR is very convenient in many projects, it is important to be aware of some of its limitations:
- They are not very precise if what you are looking for is to measure the exact intensity of light in lux.
- Its behavior may vary depending on the temperature.
- They work best to detect larger changes in light and not rapid variations.