InduinoX User Guide - Programming the LDR

The LDR on the InduinoX is connected to Analog Pin 3 through a Voltage Divider configuration. The Voltage divider is in such a configuration that the voltage sensed by the Analog Pin3 would be inversely proportional to the light incident on the LDR. The voltage would vary in the range of 0-5 Volts

The Analog Input on Arduino is of 10-bit resolution. 10-bit resolution means that the Voltage range of 0-5 Volts is represented in 1024 steps from 0-1023. So you would be reading an input value in the range of 0-1023 where 1023 would correspond to 5 Volts(or very very less light!)

Arduino[the IDE!] comes with a serial library that can be used to transmit data serially to a computer. We shall make use of this library to transmit our LDR value to a Computer every 1 second.

Here's a Video of the Sample Program




Here's the Source Code.

 int val = 0; // A Variable to Store the Light Value from the LDR  
 void setup()  
 {  
  Serial.begin(9600);// Start a Serial Connection  
 }  
 void loop()  
 {  
  val = analogRead(3);// Reads a 10-bit value corresponding to the voltage applied on analog input pin 3.  
  Serial.print("Light Intensity is : ");// Prints the given string / value to the serial monitor  
  Serial.println(val);// prints the value of the variable val to the serial monitor and moves the cursor to the next line (the ln part of println does this  
  delay(1000);  
 }  


Now Lets build a Simple Lighting Level Controller using the LDR and the 3 on-board LEDS[11,12 & 13]. We will try and increase / decrease the number of lights that are ON based on changing Light Intensity Levels Measured by the LDR.

Make note of the values shown in the serial monitor for various levels of light intensity. Use these values as threshold and every time the LDR value increases above one of the thresholds, switch On a LED and everytime it decreases below the threshold, switch Off the LED.

Here's a Video of the project in Action



Here's the source code

 /*  
 ### LDR Based Lighting Level Controller ###  
  This is a simple program where 3 LEDS are switched ON / OFF one by one as the Light Intensity Sensed by the LDR Decreases / Increases  
  */  
 #define threshold1 650 // First Threshold Value of Darkness above which the first LED is switched ON  
 #define threshold2 750 // Second Threshold Value of Darkness above which the second LED is switched ON  
 #define threshold3 950 // Third Threshold Value of Darkness above which the third LED is switched ON  
 int val = 0; // A Variable to Store the Light Value from the LDR  
 void setup()  
 {  
  pinMode(11,OUTPUT); // LED 1   
  pinMode(12,OUTPUT); // LED 2  
  pinMode(13,OUTPUT); // LED 3  
  Serial.begin(9600);// Start a Serial Connection  
 }  
 void loop()  
 {  
  val = analogRead(3);// Reads a 10-bit value corresponding to the voltage applied on analog input pin 3.  
  Serial.print("Light Intensity is : ");// Prints the given string / value to the serial monitor  
  Serial.println(val);// prints the value of the variable val to the serial monitor and moves the cursor to the next line (the ln part of println does this  
  if(val > threshold1) // Checks & Turns the First LED ON / OFF based on Light Intensity  
   digitalWrite(11,HIGH);  
  else  
   digitalWrite(11,LOW);  
  if(val > threshold2) // Checks & Turns the Second LED ON / OFF based on Light Intensity  
   digitalWrite(12,HIGH);  
  else  
   digitalWrite(12,LOW);  
  if(val > threshold3) // Checks & Turns the Thirdf LED ON / OFF based on Light Intensity  
   digitalWrite(13,HIGH);  
  else  
   digitalWrite(13,LOW);  
 }