Raspberry Pi - Door Sensor - LED

This tutorial instructs you how to use the Raspberry Pi and door sensor to control the LED. We will learn two different applications:

Application 1 - LED is on when door is open and LED is off when door is closed. The LED state is synchronized with the door sensor's state. In detail:

Application 2 - The LED state is toggled each time the door is opened. More specifically:

Hardware Preparation

1×Raspberry Pi 4 Model B
1×Door Sensor
1×LED
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi
1×(Optional) Power Adapter for Raspberry Pi 4B
1×(Optional) Plastic Case 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 LED and Door Sensor

If you are unfamiliar with LED and door sensor (including pinout, operation, and programming), the following tutorials can help:

Wiring Diagram

The wiring diagram between Raspberry Pi and Door Sensor LED

This image is created using Fritzing. Click to enlarge image

Application 1 - The LED state is in sync with the door sensor state

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 door_sensor_led.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-door-sensor-led """ import RPi.GPIO as GPIO import time # Set the GPIO mode to BCM GPIO.setmode(GPIO.BCM) # Define the GPIO pin numbers for the door sensor and the LED DOOR_SENSOR_PIN = 18 LED_PIN = 20 # Setup the GPIO pin for the door sensor as an input with pull-up resistor GPIO.setup(DOOR_SENSOR_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Setup the GPIO pin for the LED as an output GPIO.setup(LED_PIN, GPIO.OUT) try: while True: # Read the state of the door sensor (HIGH when open, LOW when closed) door_state = GPIO.input(DOOR_SENSOR_PIN) if door_state == GPIO.HIGH: print("Door is OPEN") GPIO.output(LED_PIN, GPIO.HIGH) # Turn on the LED else: print("Door is CLOSED") GPIO.output(LED_PIN, GPIO.LOW) # Turn off the LED time.sleep(0.1) # Add a small delay to avoid excessive reads except KeyboardInterrupt: print("Exiting...") GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 door_sensor_led.py
  • Open and close the door
  • Check out the change in the LED's state. You will see that the LED state is in sync with the door sensor state.

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

Code Explanation

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

Application 2 - Door Sensor toggles LED

Detailed Instructions

  • Create a Python script file door_sensor_toggle_led.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-door-sensor-led """ import RPi.GPIO as GPIO import time # Set the GPIO mode to BCM GPIO.setmode(GPIO.BCM) # Define the GPIO pin numbers for the door sensor and the LED DOOR_SENSOR_PIN = 18 LED_PIN = 20 # Setup the GPIO pin for the door sensor as an input with pull-up resistor GPIO.setup(DOOR_SENSOR_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Setup the GPIO pin for the LED as an output GPIO.setup(LED_PIN, GPIO.OUT) # Variable to keep track of the previous door state prev_door_state = GPIO.input(DOOR_SENSOR_PIN) # Variable to keep track of the LED state led_state = False # False for LED off, True for LED on try: while True: # Read the state of the door sensor (HIGH when open, LOW when closed) door_state = GPIO.input(DOOR_SENSOR_PIN) if door_state != prev_door_state: if door_state == GPIO.HIGH: print("Door is OPEN") led_state = not led_state # Toggle the LED state GPIO.output(LED_PIN, led_state) # Update the LED state # Update the previous door state prev_door_state = door_state time.sleep(0.1) # Add a small delay to avoid excessive reads except KeyboardInterrupt: print("Exiting...") GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 door_sensor_toggle_led.py

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

  • Open and close the door several times.
  • Check out the change in the LED's state. You will see that the LED state is toggled once each time the door is closed.

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!