Monday, October 03, 2011

InduinoX: Interfacing with the LDR

Now that I got my LCD display to work with the arduino board, I wanted to use it to show something useful. Since I needed to detect the ambient light for my home automation project, I decided to display the amount of light coming into a room using the light dependent resistor (LDR) that comes with the InduinoX board. Later I will use the LDR reading to determine whether I have to turn on the lights in the room or not.

Typical LDR (Source: http://www.induino.com/wiki/index.php?title=File:LDR.jpg)

The LDR's output is connected to analog pin 3. The voltage as read from pin 3 is inversely proportional to the light incident on it. The analog input is connected to a 10 bit analog to digital converter (ADC). Hence the values range from 0 (at 0V) to 1023 (at 5V). The analog pins can be referenced in the code using A0 (for analog input 0) to A5. For more information on analog inputs, check out http://www.arduino.cc/en/Tutorial/AnalogInputPins.

Now to get to the coding part

#include <LiquidCrystal.h>

const int LDR_PIN = 3;

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

void setup() {

  lcd.begin(16, 2);
}

void loop() {
  int ldrReading = analogRead(LDR_PIN);

  // LDR resistance is inversly proportional to the light

  ldrReading = 1023 - ldrReading;

  // Clear screen on LCD
  lcd.clear();

  // Print a message to LCD
  lcd.print("lux: ");
  lcd.print(ldrReading);

  delay(1000);
}

The code first sets up the LCD display. And in the loop(), keeps reading pin 3 every second, converts it to light intensity from 0 (completely dark) to 1023 (full light) and displays the value on the LCD display. Note that although I print the value as "Lux: X", the value is not really the luminance.

A few images of the setup under various lighting conditions

Under ambient light during day time

When the LDR is blocked with a paper

Under florescent lighting

Post a Comment