Raspberry Pi - Light Sensor

This tutorial instructs you how to use the light sensor with Raspberry Pi. In detail:

If you're looking for a light sensor that outputs two level (LOW/HIGH) based on a settable threshold, We highly recommend checking out the LDR Light Sensor Module tutorial. It is much easier and more convenient to use.

Hardware Preparation

1×Raspberry Pi 4 Model B
1×Light Sensor
1×10 kΩ resistor
1×ADS1115 ADC Module
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of Light Sensor

This tutorial utilizes a light sensor known as a photoresistor. It is also referred to as a light-dependent resistor, LDR, or photocell.

It is not only used to detect light, but also to measure the brightness/illuminance level of the environment.

The Light Sensor Pinout

A photoresistor has two pins which do not need to be distinguished, as it is a type of resistor that is symmetric.

Light Sensor pinout

How It Works

The greater the amount of light that the photoresistor's face is exposed to, the lower its resistance will be. Thus, by measuring the photoresistor's resistance, we can determine the brightness of the surrounding illumination.

How Light Sensor Works


The value obtained from the light sensor only gives an approximate indication of the intensity of light, not a precise measure of luminous flux. Therefore, it should not be used in situations where high accuracy is required.

Raspberry Pi - Light Sensor

The Raspberry Pi board does not come with a built-in ADC, so we will utilize an external ADC module, such as the ADS1115, to read analog voltage from a light sensor. Follow these steps to set up the system:

  • Connect the light sensor to the ADS1115 module's analog input.
  • The ADS1115 module will perform analog-to-digital conversion of the voltage from the light sensor, providing an ADC value.
  • Establish a connection between the Raspberry Pi and the ADS1115 module using the I2C interface.
  • Make sure I2C is enabled on the Raspberry Pi and set up the necessary configurations.
  • Use suitable libraries or code to read the ADC value from the ADS1115 module via the I2C interface on the Raspberry Pi.

By following these steps, you can successfully read analog voltage from the light sensor by utilizing the ADS1115 module with your Raspberry Pi.

Wiring Diagram

The wiring diagram between Raspberry Pi and Light Sensor

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Code

This code reads the value from a photocell and evaluates the light level qualitatively.

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Install the Adafruit_ADS1x15 library by running the following commands on your Raspberry Pi terminal:
sudo pip install Adafruit-ADS1x15
  • Create a Python script file light_sensor.py and add the following code:
# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-light-sensor import time import Adafruit_ADS1x15 # Create an ADS1115 ADC object adc = Adafruit_ADS1x15.ADS1115() # Set the gain to ±4.096V (adjust if needed) GAIN = 1 # Define the threshold values for dim and bright levels DIM_THRESHOLD = 2000 # Adjust this based on your sensor's calibration BRIGHT_THRESHOLD = 3000 # Adjust this based on your sensor's calibration # Function to classify the light level def classify_light_level(raw_value): if raw_value < DIM_THRESHOLD: return "Dim" elif raw_value >= DIM_THRESHOLD and raw_value < BRIGHT_THRESHOLD: return "Moderate" else: return "Bright" # Main loop to read the analog value from the LDR and classify the light level try: while True: # Read the raw analog value from channel A3 raw_value = adc.read_adc(3, gain=GAIN) # Convert the raw value to voltage (not needed for classification) voltage = raw_value / 32767.0 * 4.096 # Assumes 4.096 V range for GAIN=1 # Classify the light level light_level = classify_light_level(raw_value) # Print the results print("Raw Value: {} \t Voltage: {:.2f} V \t Light Level: {}".format(raw_value, voltage, light_level)) # Add a delay between readings (adjust as needed) time.sleep(1) except KeyboardInterrupt: print("\nExiting the program.")
  • Save the file and run the Python script by executing the following command in the terminal:
python3 light_sensor.py
  • Cover light sensor by your hand or shine light onto the sensor.
  • Check the Terminal to view the result.
PuTTY - Raspberry Pi
Raw Value: 1000 Voltage: 0.61 V Light Level: Dim Raw Value: 1800 Voltage: 1.10 V Light Level: Dim Raw Value: 2100 Voltage: 1.29 V Light Level: Moderate Raw Value: 2500 Voltage: 1.53 V Light Level: Moderate Raw Value: 2800 Voltage: 1.71 V Light Level: Moderate Raw Value: 3200 Voltage: 1.96 V Light Level: Moderate Raw Value: 3600 Voltage: 2.20 V Light Level: Bright Raw Value: 4000 Voltage: 2.45 V Light Level: Bright

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

Video Tutorial


  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!