Using Photoresistor From Raspberry PI To Detect Light

4
(1)

Last Updated on 9th June 2024 by peppe8o

A photoresistor (also known as photocell) is a Light Dependent Resistor (LDR). As the name suggests, these components act just like a resistor, changing their resistance in response to how much light is falling on it. Usually, photoresistors have a very high resistance in near darkness and very low resistance in bright light.

photoresistor

This component is used to manage electronic or electric devices to answer light conditions enabling or disabling functions.

Photoresistors are analogic components. So it can be used with microcontrollers having analogic inputs (like Arduino) to read light level.

Unfortunately, Raspberry PI has only digital inputs (with the threshold between High and Low being around 1V). This means that, without specific analogic-to-digital hardware, we’ll be able only to read if the light is high or low.

In this article, I’ll show you how to use a photoresistor with Raspberry PI and python to detect if the light is high or low. I’ll use a Raspberry Pi Zero W, but this article applies also to newer Raspberry PI boards.

What We Need

As usual, I suggest adding from now to your favourite e-commerce shopping cart all the needed hardware, so that at the end you will be able to evaluate overall costs and decide if to continue with the project or remove them from the shopping cart. So, hardware will be only:

Raspberry PI Zero WH board

Wiring Diagram

Please find below the wiring diagram to arrange. Please note that VCC+ is connected to a 3,3V port, in order to leave an acceptable voltage entering in reading GPIO (GPIOs can read up to 3,3V, otherwise you risk damaging it):

Raspberry Pi photoresistor wiring

Following picture also shows you my project assembling:

Raspberry Pi photoresistor picture

Step-by-step Procedure

Before starting, let’s dig on circuit logic.

Circuit Explaination

This circuit uses a cheap NPN transistor as a switch to enable current passing from the GPIO reading point. As said, photoresistor has an internal resistance that varies depending on light received. In dark conditions, my photoresistor has resistance around 50K ohm. In bright light, my photoresistor has resistance around 500 ohm.

Next schemas will try to simplify concepts. In fact, in analogic world, an NPN transistor will never completely block current. It will more likely get very high resistance levels.

Photoresistor and 100k ohm resistor work together as a Voltage reduction circuit (see also learningaboutelectronics.com: How to Reduce Voltage with Resistors for more details). With low LDR resistance, their middle Voltage level goes down near the ground. It disables the NPN transistor. With high LDR resistance, their middle Voltage level goes up. It enables the NPN transistor.

Circuit In Dark Conditions

In dark conditions, the photoresistor increases its internal resistance so increasing voltage at NPN transistor base. This enables the transistor which connects the reading point to the ground. Current flowing from VCC to ground through 300 ohm resistor, GPIO reading point and transistor Collector->Emitter. The final result is a reading point to 0V, so reading a false (0 or low) logic result:

photoresistor circuit on dark

Circuit in Light Conditions

In light conditions, the photoresistor decreases its internal resistance so driving voltage at NPN transistor base near to the ground. This switches off the transistor, which leaves the reading point to VCC level. Current flowing from VCC to ground mainly through 100k ohm resistor and photoresistor. The final result is a reading point near to VCC (3,3V), so reading a true (1 or high) logic result:

photoresistor circuit on light

Prepare Raspberry PI OS

Install OS with the Install Rasperry PI OS Lite guide. Make your OS up to date. From terminal:

sudo apt update
sudo apt upgrade

RPI.GPIO should be already installed (otherwise, you can get it installed with the command “sudo apt install python3-rpi.gpio”).

Create (or download) Python Script

Python script becomes really simple, as logic is built on hardware. Please download it from my download page with the terminal command:

wget https://peppe8o.com/download/python/photoresistor_rpi.py

and simply run it by typing:

python3 photoresistor_rpi.py

You can use CTRL+C to stop python script.

The script will start reading “1” in light conditions and reading “0” in dark conditions. It will be enough in a medium illuminated room putting your hand over the upside to create a dark environment:

pi@raspberrypi:~ $ python3 photoresistor_rpi.py
Read: 1
Read: 0
Read: 0
Read: 0
Read: 1
Read: 1
Read: 1
Read: 1
Read: 0
Read: 0
Read: 1

Script is just running following tasks. Needed libraries are imported:

import RPi.GPIO as GPIO
import time

Your read PIN is associated with a specific variable. BCM pin naming will be used. readPIN is set to read (GPIO.IN):

readPIN = 14

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(readPIN,GPIO.IN)
GPIO.setwarnings(True)

The main loop will simply read input PIN and wait 1 second for the next measurement. GPIO reading is converted to string with str() function to concatenate it inside print() function:

print (" Read: " + str(GPIO.input(readPIN)))
time.sleep(1)

Final Toughts

To end, you could need to switch from “0” to “1” result at different light levels. For this purpose, you can try varying the 100k ohm resistor with a lower or higher one until you find light conditions which best fit your needs.

Enjoy!

How useful was this post?

Click on a star to rate it anonymously!

Average rating 4 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?