Interfacing a LDR
Hardware Required
- ARDUINO UNO
- LDR
- Buzzer
- Connecting Wires
About Arduino
Arduino is a popular open-source development board for engineers and makers to develop electronics projects in an easy way. It consists of both a physical programmable development board (based on AVR series of microcontrollers) and a piece of software or IDE which runs on your computer and used to write and upload the code to the microcontroller board.
About LDR
A photoresistor (or light-dependent resistor, LDR, or photo-conductive cell) is a light-controlled variable resistor. The resistance of a photoresistor decreases with increasing incident light intensity; in other words, it exhibits photoconductivity. A photoresistor can be applied in light-sensitive detector circuits, and light-activated and dark-activated switching circuits.
A photoresistor is made of a high resistance semiconductor. In the dark, a photoresistor can have a resistance as high as several megohms (MΩ), while in the light, a photoresistor can have a resistance as low as a few hundred ohms.
If the LDR detects dark then the green light in the LDR module will not glow
Using digital input to interface LDR
Code
void setup()
{
pinMode( 9, INPUT);//LDR
pinMode( 5, OUTPUT);//Buzzer
Serial.begin(9600);
}
void loop()
{
if (( ( digitalRead(9) ) == ( HIGH ) ))
{
Serial.println("Dark");
digitalWrite(5,HIGH);
}
if (( ( digitalRead(9) ) == ( LOW ) ))
{
Serial.println("Light");
digitalWrite(5,LOW);
}
}
Code Explanation
In the code for LDR there's no need for explanation but in short i'll explain you; in the code for LDR you just need to give a condition that if the LDR detects Dark(HIGH) or Light(LOW) then buzzer should beep(according to my code).
Using ANALOG Input to interface LDR
Code
int x = 0;
void setup()
{
pinMode(5,OUTPUT);
Serial.begin(9600);
}
void loop()
{
x = analogRead(5);
// Serial.println(x);
if (( ( analogRead(5) ) > ( 300 ) ))
{
Serial.println("Dark");
digitalWrite(5,HIGH);
}
if (( (analogRead(5) ) < ( 300 ) ))
{
Serial.println("Light");
digitalWrite(5,LOW);
}
}
Code Explanation
There is only one change in the connections that is you have to disconnect the wire from D0 to A0 because we are using analog input.
No comments:
Post a Comment