Tutorial – Use a Light Dependent Resistor (LDR) to Measure Light Levels


This tutorial covers measuring light levels by interfacing a Light Dependent Resistor (LDR) to a Hobbyduino Mini.  The example sketch will vary the blink rate of an LED based on the intensity of light that strikes the LDR.

Suggested Applications

  • Automatic light dimmer
  • Automatic blinds or curtains
  • Automatic night-light
  • Camera light meter
  • Light sensitive switch

Parts List

  • Arduino or Arduino clone (a Hobbyduino Mini V3 is used for this tutorial)
  • LDR (also known as a photocell)
  • LED
  • 220 to 330 ohm resistor (used to control current through LED)
  • 1K to 10K resistor (used with the LDR to setup a voltage divider)
  • Hobbyduino Proto Plug (optional) or breadboard
  • FTDI Basic (Sparkfun)

Basic Wiring


Copy-n-paste or download the following sketch.

/* LDR1.pde
  Light Dependent Resistor (LDR) is used to vary the blink rate
  of a LED connected to digital pin 13 based on the amount of
  light that strikes the LDR.

#define LEDpin  13                   // LED connected to digital pin 13
#define LDRpin  0                    // LDR connected to analog pin 0

const int minDuration = 100;         // Minimum delay between blinks
const int maxDuration = 1000;        // Maximum delay between blinks

void setup()
  pinMode(LEDpin, OUTPUT);           // Set LED pin as an output
  Serial.begin(57600);               // Initialize serial port

void loop()
  int rate = analogRead(LDRpin);     // Read analog input
  float volts = (rate/1023.0) * 5.0;
  int rawRate = rate;                // Save raw analog input value

  if (rate < 100) rate = 100;        // Scale minimum limit
  if (rate > 1000) rate = 1000;      // Scale maximum limit

  // Scale blink rate between min and max duration
  rate = map(rate, 100, 1000, minDuration, maxDuration);

  Serial.print("Raw Rate: ");
  Serial.println(rawRate);           // Send raw analog value to serial port

  Serial.print("Map Rate: ");
  Serial.println(rate);              // Send scaled analog value to serial port

  Serial.print("Volts DC: ");

  digitalWrite(LEDpin, HIGH);        // Turn on LED
  delay(rate);                       // Delay is based on light level
  digitalWrite(LEDpin, LOW);         // Set LED off
  delay(rate);                       // Delay is based on light level

Upload the sketch to the Arduino and open the serial monitor (be sure the baud rate in the serial monitor matches that of the sketch).  The below light levels are what I measured but, your readings may vary based on the level of light and the LDR you chose to use.

Ambient Light:

Bright Light:

No Light:


The resistance of the LDR decreases when the amount of light it detects increases.  The level of light across the LDR changes the voltage level measured by analog pin 0.  You’ll notice in the above screen shots that the voltage is near 5V when maximum light is shined on the LDR and near 0V when minimum light is shined on the LDR.  The blink rate of the LED is scaled to a value between 100 and 1000 usec by way of the Arduino map() function.

map(value, fromLow, fromHigh, toLow, toHigh)

  • value – number to scale
  • fromLow – lower bound of value’s current range
  • fromHigh – upper bound of value’s current range
  • toLow – lower bound of value’s target range
  • toHigh – upper bound of value’s target range

The map() function uses integer math so, fractional remainders will be truncated (not rounded or averaged).

The ATMega328 used on the Hobbyduino Mini contains a 6 channel, 10-bit analog to digital converter (A/D).  The A/D maps input voltages between 0 and 5 VDC into an integer value between 0 and 1023.  This conversion is achieved by the Arduino analogRead() function.


  • pin – number of the analog pin to read (o to 5 for the Hobbyduino)

The analogRead() function returns a integer value between 0 and 1023.


Here is a video demonstrating the concepts of this tutorial:


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s