LDR Sensor Control With Arduino

by STEAM-DIY in Circuits > Arduino

7 Views, 0 Favorites, 0 Comments

LDR Sensor Control With Arduino

0101010.jpg

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.


Supplies

  1. Arduino Uno board
  2. LDR sensor
  3. LED bulb
  4. Breadboard
  5. 180ohm resistor
  6. 10k resistor
  7. Jumper wires

Components

Screenshot 2024-10-24 102736.jpg

Let us identify these components.

Let’s Look at the Code Below

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.

  1. The complete program of this project
/*LDR sensor control circuit.
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");
}
}


Now, select the board and port. After, click the upload the code

Everything is done.


Have a good day.