Interfacing LDR with Arduino?

Hello, I am trying to connect a LDR to Arduino as per this guide. http://www.libelium.com/squidbee/index.php?title=Adding_a_light_sensor And my requirement is: I would like LDR to light the LED (turn the LED ON) when the light is not falling on it and turn it off when the light falls on it. Can anyone please help me to satisfy my condition. According to the guide, the LED kept on blinking. Please help with a program. Thank you.

Anurag,

you need to modify the code on the page you linked. If you take it as it is, the light intensity influences how fast the led will blink. You will have to look at the if-statment and else-statment to achieve this.

Have fun,

Korman

@Korman, Thanks mate. I had tried with the If else statement, maybe i had put it into wrong syntax. Will try it again. Thank you.

For best results, you will have to check against a threshhold value. Everything below that value is considered dark, everything above light. If you're unsure what good values are, look at this tutorial: http://arduino.cc/en/Tutorial/AnalogReadSerial

Instead of the potentiometer, use the circuit from your other example. In the code you need to replace A0 by 2 (or define LDR as in the other example and use LDR with analogRead() to do it nice and proper). Then play around with various levels of light to find out what value on your serial monitor is considered dark enough to turn the led on or off.

Korman

I have write the program as follows:

int LDR = 2; // select the input pin for the LDR int ledPin = 13; // select the pin for the LED int val = 0; // variable to store the value coming from the sensor

void setup() { pinMode(LDR, INPUT); // declare the LDR as an INPUT pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT }

void loop() { if (val = analogRead(LDR)) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); // turn the ledPin off }} Its not working.

if (val = analogRead(LDR))  {
 digitalWrite(ledPin, HIGH); }
else
{
 digitalWrite(ledPin, LOW);   // turn the ledPin off
}

The analogRead function returns a value between 0 and 1023, proportional to the amount of light hitting the LDR (and the size of the other resistor in the voltage divider). You are storing this in val (good).

Then, you have, effectively,

if(val)
{
 digitalWrite(ledPin, HIGH);
}
else
{
 digitalWrite(ledPin, LOW);   // turn the ledPin off
}

This is most likely not what you want.

Put the { on a new line. Line the matching } up with it. Indent everything in the block consistently. This will make the code much easier to follow.

void loop()
{
  val = analogRead(LDR)  // this reads the LDR value and puts it into variable "val"

  if (val > 127)  // use whatever number is your selected threshold
  {
    digitalWrite(ledPin, HIGH);    // turn the ledPin on
  }
  else
  {
   digitalWrite(ledPin, LOW);   // turn the ledPin off
  }
}

It dint worked.

I tried, removing the analog pin but the led continues to flash, but when i remove the +5v pin it stops flashing.

Did you replace the “127” with some number in the middle of the range of values you get from your LDR?

Yes i tried the values. And yeah thanks a lot.
It working now.
I changed the value to 1000. And its working awesome. Initially i tried with lower values >250.
And it dint showed the proper result.
Now its showing the proper correct result. I will upload the video on Youtube. Thanks. :slight_smile:

Here’s the Code for LDR

/* This is program to make a photoresistor act as a light sensor.
It will turn the buzzer on when the light is not falling on it 
and it will turn the buzzer off when the light is present on it. */ 


int LDR = 0;       // select the input pin for the LDR
int ledPin = 11;   // select the pin for the Buzzer. NOTE: I havent changed the integer 'ledPin' for Buzzer. You can put anything.       
int val ; 

void setup() {
  pinMode(LDR, INPUT);       // declare the LDR as an INPUT
  pinMode(ledPin, OUTPUT);  // declare the Buzzer as an OUTPUT
}

void loop()
{
  val = analogRead(LDR); //Reads the LDR value and stores in variable 'val'
  if (val > 1000)    // This is the Treshold value of LDR. Try changing it according to your needs.
  {
    digitalWrite(ledPin, HIGH); // It will turn the buzzer on when the light is restricted.
  }
  else
  {
    digitalWrite(ledPin, LOW); //It will turn off the buzzer then the light is falling on it.
  }
}