Arduino Starter’s Guide (4/7): Light Dependent Resistor and LED



Whilst getting input from a potentiometer can be useful for human controlled experiments, what do we use when we want an environmentally controlled experiment? We use exactly the same principles but instead of a potentiometer (twist based resistance) we use a photo resistor (light based resistance). The Arduino cannot directly sense resistance (it senses voltage) so we set up a voltage divider. The exact voltage at the sensing pin is calculable, but for our purposes (just sensing relative light) we can experiment with the values and see what works for us. A low value will occur when the sensor is well lit while a high value will occur when it is in darkness.

Hardware Used:

  • 1 – Light Dependent Resistor
  • 1 – LED
  • 1 – 10k Resistor
  • 1 – 100 ohm Resistor

Software Used:

Arduino IDE

Set up the Hardware:



Code Breakdown:

the analog Pin for the photoresistor. this is simply a raw sensor value (relative light)

the pin the LED is connected to. we are controlling brightness so we use one of the PWM (pulse width modulation) pins

sets the led pin to output

Read the lightlevel. analogRead() Reads the value from the specified analog pin. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023.

map() = Re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc.

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

Try MOREEE!!!!

Reverse the response:

Perhaps you would like the opposite response. Don’t worry we can easily reverse the response just change:


Upload and watch the response change.

Night light:

Rather than controlling the brightness of the LED in response to light, let’s instead turn it on or off based on a threshold value. Change the loop() code with.



Tags : arduinoldrLedLight Dependent ResistorresistorStarter Kit

Leave a Response