Post Page Advertisement [Top]

Arduino - Sensor Interfacing - LDR Simple Light Sensor- How To?

Arduino - Sensor Interfacing - LDR Simple Light Sensor- How To?

LDR - Light Dependent Resistor - A Simple Light Sensor
The LDR is a variable resistor, whose resistance varies based on the light incident upon it. More the light, less the resistance. Now the LDR cannot be used directly. We would need to convert the change in resistance of the ldr to change in voltage. We can achieve this by constructing a potential divider using the ldr and a fixed resistor. Then we can take the output of the potential divider and connect it to an Analog In pin on the arduino.

Follow the images below and connect the LDR
This is the LDR
Place the LDR as shown and connect a resistor as shown
Connect Supply & Ground. Take a Wire from the Junction of the Resistor & LDR and connect it to Analog In 4 of the Arduino
Here is how the Finished Setup will look like
Try the following Code [RGB_button_ldr.ino]
/*  
 
 Pressing the Button changes the current active color of the RGB LED & prints the current temperature value to the Serial Monitor  
 
 and the light intensity detected by the LDR determines the intensity level of the active color  
 */


int intensity = 0, pin = 9;  
void setup() {     
  pinMode(2,INPUT);  
  Serial.begin(9600);  
}  

void loop() {  
  if(digitalRead(2)==0) // Switch being pressed  
  {  
    int temp = analogRead(2)/2;  
    Serial.print("Temperature is : ");  
    Serial.println(temp);  
    analogWrite(10,0);  
    analogWrite(9,0);  
    analogWrite(11,0);  
    if(pin<11)
      pin++;
    else
      pin = 9;
    intensity = analogRead(4)/4;  
    analogWrite(pin,intensity);  
    Serial.print(pin);  
    Serial.print(" ");  
    Serial.println(intensity); 
    while(digitalRead(2)==0);  
    delay(100);  
  }

}  

No comments:

Post a Comment

Bottom Ad [Post Page]