LDR sensor control with Arduino
Hello friends, Welcome to my SriTu Hobby blog. So far we have described 20 articles through the blog. Each of these articles explains the basics of Arduino components. You can read these previous articles. So let’s go to today’s post. Today we are going to talk about the LDR sensor.
What is an LDR sensor?
It is called a Light Dependent Resistor. It is a resistor. This is also known as a photoresistor. This is a semiconductor device. Depending on the amount of light falling on the sensor surface, the resistance value increases or decreases. It depends on the electron value. The higher the light, the higher the electron value, and the lower the light, the lower the electron value. As the electron value increases, the resistance value decreases as the current is easier to flow. When the electron value decreases, the resistance value increases as the current becomes difficult to flow. We can use this method to create light-sensitive circuits. This sensor is also used for night-active street lamps. We can use this sensor for various projects. It depends on your creativity. Ok, let’s check this sensor activity using a multimeter. Use a multimeter and sensor for this.
First time rotate the multimeter 20k ohm scale. After, connect the sensor legs to the multimeter probes. Now you can see the sensor resistance value through the multimeter. Cover the sensor by hand and see how the value changes.
Ok, let’s look at how to connect the Arduino board to this sensor. We have to use a special method to get the values through this sensor. That is, we need to create a voltage divider circuit. It depends on the sensor resistance value. We need only a resistor for this circuit. When this is given a potential of 5 volts, the values can be obtained from the voltage difference in the voltage divider point. For that, we can use analog pins on the Arduino board.
Today let’s see how to blink an LED bulb using LDR sensor values. The required components are as follows.
- Arduino Uno board — Our Store / Amazon
- LDR sensor — Our Store / Amazon
- LED bulb — Our Store / Amazon
- Breadboard — Our Store / Amazon
- 180ohm resistor — Our Store / Amazon
- 10k resistor — Our Store / Amazon
- Jumper wires — Our Store / Amazon
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Step 1
Let us identify these components.
Step 2
Connect these components using the circuit diagram below. Remember to connect the LED bulb with the PWM pin. Use analog pins to get sensor values.
Step 3
Let’s look at the code below.
#define led 3
#define sensor A2
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop() {
int value = analogRead(sensor);
value = map(value, 0, 1023, 0, 255);
Serial.println(value);
analogWrite(led, value);
}
For the first time, we have defined the pins we use.
#define led 3
#define sensor A2
Then, the pin connected to the LED bulb is set to the output pin and the serial monitor is activated. These codes are included in the void setup function.
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
}
After that, the sensor value is read and put into the integer variable. The variable name is called “value”.
int value = analogRead(sensor);
This code converts the value from 0 – 1024 to 0 – 255. This is because the PWM value ranges from 0 – 255.
value = map(value, 0, 1023, 0, 255);
Then, using this value, the LED bulb is turned on and off.
analogWrite(led, value);
With this code, the amount of light in the bulb increases or decreases depending on the amount of light received by the sensor. Look at the code below. This code turns on the LED bulb when the sensor value reaches a certain value or higher. For that, the if condition is used. Please upload and check these two codes separately.
- The complete program of this project – Download
/*LDR sensor control circuit.
created by the SriTu Tech team.
Read the code below and use it for any of your creation
*/
#define led 3 //led pin
#define sensor A2 //sensor pin
void setup() {
Serial.begin(9600);//serial monitor
pinMode(led, OUTPUT);
}
void loop() {
int value = analogRead(sensor);//get value
Serial.println(value);
if (value >= 950) {
digitalWrite(led, HIGH);//LED ON
Serial.println("LED ON");
} else {
digitalWrite(led, LOW);//LED OFF
Serial.println("LED Off");
}
}
Step 4
Now, select the board and port. After, click the upload button.
Everything is done. Test this circuit by increasing or decreasing the light. Please upload the second code and check it out. The full video guide is below. So, we will meet in the next post. Have a good day.
LDR sensor control with Arduino