LDR resistor question


I want to use a simple setup of a laser pointer and an Arduino with an LDR resistor to help align an IR beam pair. Because they are quite far apart, I would like to use feedback from the LDR to know when the beams are aligned.

It is a simple set up and sketch. My problem is even when there is no light hitting the LDR, the LED is on (dimly). I would prefer it starts in the off position and then blinks more rapidly as the laser approaches and then hits it directly. Do I need to adjust the resistor?

/* Light sensor
 * ------------------ 
 * turns on and off a light emitting diode(LED) connected to digital  
 * pin 13. The amount of time the LED will be on and off depends on
 * the value obtained by analogRead() that is depending of the amount
 * of light in the LDR

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() {
  val = analogRead(LDR);       // read the value from the sensor
  digitalWrite(ledPin, HIGH);  // turn the ledPin on
  delay(val);                  // stop the program for some time
  digitalWrite(ledPin, LOW);   // turn the ledPin off
  delay(val);                  // stop the program for some time

What is the resistance of the LDR in the dark?

the ledpin is allways switched on for a brief moment

(code not tested)

#define THRESHOLD  20 
int LDR = 2;        
int ledPin = 13;  
int val = 0;  

void setup() 
  pinMode(LDR, INPUT);       
  pinMode(ledPin, OUTPUT); 

void loop() 
  val = analogRead(LDR);     

  if (val > THRESHOLD) // only switch on the LED above a certain THRESHOLD
    digitalWrite(ledPin, HIGH);  
  digitalWrite(ledPin, LOW);  
  delay(1023-val);          // sum of the two delays is allways approx 1 second
int LDR = 2;

Is this analog pin 2 or digital pin 2?

  pinMode(LDR, INPUT);

This is setting digital pin 2 to input mode.

  val = analogRead(LDR);

This is reading analog pin 2.


Thanks for taking the time to answer.

Analog pin 2.


I am using the following:

CdS PHOTOCELL Peak Response 550nm Min. Resistance (at 10 lux) 5K? Max. Resistance (at 10 lux) 10K? Min. Dark Resistance 500K? Peak AC Voltage 100V

from these values you can calculate the min and max voltage on the analog port of the Arduino, so you can calculate the values analogRead(2) will return.

You can do this for different values of the resistor, try { 1K, 10K, 50K, 100K, 500K } to find the value where you have the largest range Vmin-Vmax

Thanks, 1k still seems to work the best.


Hi, I saw a setup somewhere where a visible laser pointer was pre-aligned with the ir transmitter, and after that the transmitter-receiver could be quickly aligned visually and then the visual laser turned off.

I'd like to find the components for this approach to make easy-to-use invisible iR laser beams over a long distance.

Hi Terry,

That's exactly what I am using it for. I have an IR beam pair that goes over 100 ft. At that distance, I think it will be difficult to align the components by eye. So I am attaching a laser pointer to the top of the receiver and a CdS photocell (with an Arduino) on to the top of the transmitter. I'm hoping to use them for alignment, then turn them off.

I'm just trying to get the right combination of CdS, resistor and sketch. Right now, it is functioning. The LED is on dimly to show the unit is powered up. Then as the laser approaches the CdS, it get brighter and flickers. When it hits the CdS full on, it flashes regularly so I know I have alignment.

To improve I think I need a larger diameter CdS to make the aiming a little easier.

Have you looked at the code? Any suggestions to improve it?



The radiation angle of a typical IR emitter is about 30 degrees, that's not going to be too hard to align is it?

Isn't there a material that lights up under IR? - like fluorisent under UV?

arduinokov, how many of these are you thinking of making?

Anyone else think this is an important Arduino accessory.? I was thinking of an infrared laser for "long distance invisible beam" application, and a visible laser for alignment (once originally aligned, you could do the IR alignment visually). There are materials that fluoresce under IR, I've seen little cards that do that. Maybe a larger one that goes as a "target" on an IR receiver for an IR laser?? Rob, have you seen this material available somewhere?

Maybe the IR laser is tight enough to enable a combination transmit/receive unit that bounces off a mirror?

Somehow I think there must be an expensive product out there that does this. Just the kind of thing for the frugal Arduinoist to work on...

Isn't there a material that lights up under IR?

Yes but it is very expensive http://www.cascadelaser.com/irscq42r.html