How to connect and use LDR (Light Dependent Resistor) with Raspberry Pi

How to connect and use LDR (Light Dependent Resistor) with Raspberry Pi

In this post, we are going to use/connect a LDR to Raspberry pi. This can be useful in many applications. An LDR is a component that has a (variable) resistance that changes with the light intensity that falls upon it. This allows them to be used in light sensing circuits.

 

See also:

The most common type of LDR has a resistance that falls with an increase in the light intensity falling upon the device (as shown in the image above). The resistance of an LDR may typically have the following resistances:

Daylight = 5000Ω
Dark = 20000000Ω
To complete this tutorial, you will need these components. Some are available in our Shop:

 

Setup

Raspberry Pi can’t process analog sensors so ADS1115 which is an ADC (analog to digital converter) have to be incorporated. My earlier post on How to make your own Baby monitor device with Raspberry Pi  explains very well on how to configure ADS1115 and its library:

  • ADS VCC –> RPi 3.3v (pin 1 or 17)
  • ADS GND –> RPi GND
  • ADS SCL –> RPi SCL (pin 5 BCM 3)
  • ADS SDA –> RPi SDA (pin 3 BCM 2)
  • ADS A0 –> ldr

 

LDR doesn’t have any maked leg/pin as positive or negative so use your own discretion to choose although the image below should help you. Connect one leg of the LDR to GND through your 10k resistor and the other leg to RPi 5volts pin. Attach the other leg to ADS1115 A0 pin. The LED is attached to RPi BCM pin 16.

 

raspberry LDR
raspberry LDR

 

Code and Results

First, lets access the raw sensor data from LDR via ADS1115:

 

 

 

You should be getting values like these depending on the lightening condition in your room or environment. Now, flash a torch light at the LDR or increase the amount of light in the room, note the increase in values:

 

 

 

Now, off the torch light or decrease the amount of light in the room, note the decrease in values:

 

If you like the values in percent, you can use numpy to interploate the values between 0 – 100%:

 

 

You can also switch an LED ON or OFF when the value gets to a certain threshold:

 

Full Code:

Leave a Reply

Your email address will not be published. Required fields are marked *