Using Photoresistor From Raspberry PI To Detect Light
Photoresistor (also known as photocell) is a Light Dependent Resistor (LDR). As the name suggests, this components act just like a resistor, changing its resistance in response to how much light is falling on it. Ususally, photoresistors have a very high resistance in near darkness and a very low resistance in bright ligh.

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 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 light is high or low.
In this article, I’ll show you how to use photoresistor with Raspberry PI and python to detect if 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 ecommerce shopping chart all needed hardware, so that at the end you will be able to evaluate overall costs and decide if continuing with the project or removing them from shopping chart. So, hardware will be only:
- Raspberry PI Zero W (including proper power supply or using a smartphone micro usb charger with at least 3A) or newer Raspberry PI Board
- high speed micro SD card (at least 16 GB, at least class 10)
- Photoresistor
- NPN Transistor (I used a PN2222)
- resistors
- dupont wiring
- breadboard (optional)
Many of listed hardware (except from Raspberry PI Zero W and micro SD Card) can be bought alone or can be also found in the useful Elegoo starter kit.
Check hardware prices with following links:
Wiring Diagram
Please find below wiring diagram to arrange. Please note that VCC+ is connected to 3,3V port, in order to leave an acceptable voltage entering in reading GPIO (GPIOs can read up to 3,3V, otherwise you risk to damage it):

Following picture also shows you my project assembling:

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 GPIO reading point. As said, photoresistor has an internal resistance that vary depending on light received. In dark conditions, my photoresistor has a resistance around 50K ohm. In bright light, my photoresistore has a resistance around 500 ohm.
Next schemas will try to simplify concepts. Infact, in analogic world a 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 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, photoresistor increases its internal resistance so increasing voltage at NPN transistor base. This enables the transistor which connects reading point to Ground. Current flowing from VCC to ground throught 300 ohm resistore, GPIO reading point and transistor Collector->Emitter. Final result is reading point to 0V, so reading a false (0 or low) logic result:

Circuit in Light Conditions
In light conditions, photoresistor decreases its internal resistance sodriving voltage at NPN transistor base near to ground. This switches off the transistor, which leaves reading point to VCC level. Current flowing from VCC to ground mainly throught 100k ohm resistore and photoresistor. Final result is reading point near to VCC (3,3V), so reading a true (1 or high) logic result:

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
Install Python Libraries
We also need to install the useful RPI.GPIO package in order to manage GPIO reading from python:
sudo apt install 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 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 enought in a medium illuminated room putting your hand over the up side 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 to 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)
Main loop will simply read input PIN and wait 1 second for 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 switching 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 untill you find light conditions which best fit your needs.
Enjoy!