Raspberry Pi - Light Sensor Relay

This tutorial instructs you how to use Raspberry Pi and light sensor to trigger relay. In detail, The light sensor module outputs a LOW signal when light is present and a HIGH signal when light is not detected. The Raspberry Pi is programmed to read the signal from the light sensor module and accordingly activate the relay when light is absent, and vice versa.

By connecting a relay to a light bulb, LED strip, motor, or actuator.. We can then use the Raspberry Pi and light sensor to regulate the light bulb, LED strip, motor, or actuator...

The light sensor is also known as photoresistor, light-dependent resistor, photocell, LDR. The Raspberry Pi uses a light sensor to measure the ambient light level, and if it detects darkness, it activates the relay, while if it senses brightness, it deactivates it.

Hardware Preparation

1×Raspberry Pi 4 Model B
1×LDR Light Sensor Module
1×Relay
1×Warning Light Bright Waterproof
1×12V Power Adapter
1×DC Power Jack
1×Breadboard
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi
1×(Optional) USB-C Power Cable with On/Off Switch for Raspberry Pi 4B
1×(Optional) Plastic Case and Cooling Fan for Raspberry Pi 4B

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.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Relay and Light Sensor

If you are not familiar with relay and light sensor (including pinout, functionality, programming, etc.), please check out the following tutorials:

Wiring Diagram

The wiring diagram between Raspberry Pi and Light Sensor Relay Light Bulb

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Code

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
  • Create a Python script file ldr_relay.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-relay """ import RPi.GPIO as GPIO import time # Set the GPIO mode to BCM GPIO.setmode(GPIO.BCM) # Define the PIN numbers for the light sensor and the relay LIGHT_SENSOR_PIN = 18 RELAY_PIN = 21 # Set up the light sensor PIN as an input with a pull-up resistor GPIO.setup(LIGHT_SENSOR_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set up the relay PIN as an output GPIO.setup(RELAY_PIN, GPIO.OUT) try: while True: # Read the state of the light sensor light_state = GPIO.input(LIGHT_SENSOR_PIN) # Control the relay based on the light sensor state if light_state == GPIO.LOW: # Light is present, turn off the relay GPIO.output(RELAY_PIN, GPIO.LOW) else: # Light is not present, turn on the relay GPIO.output(RELAY_PIN, GPIO.HIGH) # Small delay to avoid rapid state changes time.sleep(1) except KeyboardInterrupt: # Clean up GPIO settings on program exit GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 ldr_relay.py

class="key-combo">Ctrl + C in the terminal.

  • Cover the LRD light sensor module by your hand
  • Check out the relay's state.

The script runs in an infinite loop continuously until you press

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

Video Tutorial

※ OUR MESSAGES

  • 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!