Raspberry Pi - Door Sensor - Servo Motor

This tutorial instructs you how to use Raspberry Pi and door sensor to control servo motor. In detail:

The same procedure is carried out over and over again.

Hardware Preparation

1×Raspberry Pi 4 Model B
1×Door Sensor
1×Servo Motor
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 Servo Motor and Door Sensor

If you are unfamiliar with servo motors and door sensors (including pinouts, operation, and programming), the following tutorials can help:

Wiring Diagram

The wiring diagram between Raspberry Pi and Door Sensor Servo Motor

This image is created using Fritzing. Click to enlarge image

Please note that the wiring diagram shown above is only suitable for a servo motor with low torque. In case the motor vibrates instead of rotating, an external power source must be utilized to provide more power for the servo motor. The below demonstrates the wiring diagram with an external power source for servo motor.


Please do not forget to connect GND of the external power to GND of Arduino Raspberry Pi.

Raspberry Pi Code - Door Sensor Controls Servo Motor

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_servo.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-servo-motor import RPi.GPIO as GPIO import time # Constants won't change DOOR_SENSOR_PIN = 18 # Raspberry Pi GPIO pin connected to the door sensor's pin SERVO_PIN = 20 # Raspberry Pi GPIO pin connected to the servo motor's pin # Variables will change angle = 0 # The current angle of the servo motor prev_door_state = None # The previous state of the door sensor door_state = None # The current state of the door sensor # Setup GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(DOOR_SENSOR_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(SERVO_PIN, GPIO.OUT) # Create PWM instance for servo servo_pwm = GPIO.PWM(SERVO_PIN, 50) # 50 Hz frequency # Initialize servo position servo_pwm.start(0) try: while True: prev_door_state = door_state # Save the last state door_state = GPIO.input(DOOR_SENSOR_PIN) # Read new state if prev_door_state == GPIO.LOW and door_state == GPIO.HIGH: print("The door-opening event is detected") # Change angle of servo motor if angle == 0: angle = 90 else: angle = 0 # Control servo motor according to the angle duty_cycle = (angle / 18) + 2.5 # Convert angle to duty cycle servo_pwm.ChangeDutyCycle(duty_cycle) time.sleep(0.1) # Add a small delay to avoid rapid sensor readings except KeyboardInterrupt: servo_pwm.stop() GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 door_sensor_servo.py

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

  • Bring a magnet close to the reed switch, then move it away. Repeat this several times.
  • Check out the servo motor angle changing.

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!