ESP32 S3 - Door Sensor - Servo Motor

This tutorial shows you how to use the ESP32 S3 with a magnetic door sensor and a servo motor to build an automatic position-control system that reacts to door open and close events. The ESP32 S3 continuously monitors the door sensor and rotates the servo to a target angle whenever the door state changes — making this a practical foundation for door-linked locking mechanisms, vent flaps, and flag indicators.

What you'll build:

  1. An ESP32 S3 circuit with a magnetic door sensor and servo motor wired to the board
  2. A sketch that reads the door sensor state and rotates the servo to 90 degrees when the door opens and back to 0 degrees when the door closes
  3. An automatic position-control loop that repeats every time the door state changes
  4. A foundation for real-world applications such as door-activated locking arms, ventilation flaps, and visual indicators

Hardware Preparation

1×ESP32 S3 WROOM N16R8
1×Alternatively, ESP32 S3 SuperMini Dev Module
1×Alternatively, ESP32 S3 Uno-form Board
1×Alternatively, ESP32 S3 Camera Board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Door Sensor
1×Servo Motor
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack
1×Recommended: Screw Terminal Expansion Board for ESP32 S3
1×Recommended: Breakout Expansion Board for ESP32 S3
1×Recommended: Power Splitter for ESP32 S3

Or you can buy the following kits:

1×DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
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 .

Buy Note: For controlling multiple servo motors, use the PCA9685 16 Channel PWM Servo Driver Module to save MCU pins and simplify wiring.

Overview of Servo Motor and Door Sensor

A magnetic door sensor consists of two parts — a reed switch body and a magnet — that together change circuit state when the door opens and the magnet moves away from the reed switch. The ESP32 S3 reads this digital state change on a GPIO input pin and responds by commanding a servo motor through a PWM signal on a second GPIO pin. The servo motor converts that PWM pulse width into a precise shaft angle, making it ideal for applications where a physical actuator must respond automatically to door events.

Key Specifications

We have dedicated tutorials covering each component in depth. Each tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to the ESP32 S3, and Arduino code. Learn more about them at the following links:

Wiring Diagram

Connect the door sensor and servo motor to your ESP32 S3 following the diagram below, and verify every connection before applying power to the board. The door sensor's signal wire connects to an ESP32 S3 GPIO configured as an input with the internal pull-up resistor enabled, while the servo's control signal wire connects to a GPIO configured for PWM output.

The wiring diagram between ESP32 S3 Door Sensor Servo Motor

This image is created using Fritzing. Click to enlarge image

Module wires and thick wires often do not sit well in the female headers. Put the ESP32 S3 on a Screw Terminal Block for ESP32 S3, then tighten each wire under a screw. The diagram below shows the same wiring with the screw terminal board.

The wiring diagram between ESP32 S3 Door Sensor Servo Motor Screw Terminal

This image is created using Fritzing. Click to enlarge image

Or plug the ESP32 S3 into a GPIO Breakout Board for ESP32 S3. The same wiring then looks like this:

The wiring diagram between ESP32 S3 Door Sensor Servo Motor GPIO Breakout Board

This image is created using Fritzing. Click to enlarge image

Safety Notes

The servo motor draws more current than a single ESP32 S3 GPIO pin can safely supply, so always power the servo from the board's 5V rail (or an external 5V supply) rather than from the GPIO pin itself. The ESP32 S3 GPIO pins operate at 3.3V logic, and most hobby servo motors accept a 3.3V control signal without issues, but check your specific servo's datasheet to confirm. The door sensor connects to a GPIO input with the internal pull-up resistor enabled, so no external pull-up resistor is required for the sensor side of the circuit.

Component Pin ESP32 S3 Pin
Door Sensor Wire 1 GND
Door Sensor Wire 2 GPIO2
Servo Motor VCC (Red) 5V
Servo Motor GND (Brown/Black) GND
Servo Motor Signal (Orange/Yellow) GPIO8

ESP32 S3 Code - Door Sensor Controls Servo Motor

The following code reads the door sensor state on every loop iteration and rotates the servo motor to 90 degrees when the door is detected as open, then rotates it back to 0 degrees when the door is detected as closed. The sketch relies on the ESP32Servo library to generate the correct PWM signal for the servo motor, so you must install that library before uploading.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-door-sensor-servo-motor */ /* * This ESP32-S3 code is created by esp32io.com * * This ESP32-S3 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-s3-door-sensor-servo-motor */ #include <ESP32Servo.h> #define DOOR_SENSOR_PIN 2 // ESP32-S3 pin GPIO2 connected to door sensor's pin #define SERVO_PIN 8 // ESP32-S3 pin GPIO8 connected to servo motor's pin Servo servo; // create servo object to control a servo // variables will change: int angle = 0; // the current angle of servo motor int prev_door_state; // the previous state of door sensor int door_state; // the current state of door sensor void setup() { Serial.begin(9600); // initialize serial pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // set ESP32-S3 pin to input pull-up mode servo.attach(SERVO_PIN); // attaches the servo on pin GPIO8 to the servo object servo.write(angle); door_state = digitalRead(DOOR_SENSOR_PIN); } void loop() { prev_door_state = door_state; // save the last state door_state = digitalRead(DOOR_SENSOR_PIN); // read new state if(prev_door_state == LOW && door_state == HIGH) { Serial.println("The door-opening event is detected"); // change angle of servo motor if(angle == 0) angle = 90; else if(angle == 90) angle = 0; // control servo motor arccoding to the angle servo.write(angle); } }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Wire the door sensor and servo motor to the ESP32 S3 following the wiring diagram above.
  3. Connect the ESP32 S3 board to your PC using a USB Type-C cable.
  4. Open Arduino IDE on your PC.
  5. Select the correct ESP32 S3 board and COM port from the Tools menu.
  6. Click the Libraries icon on the left bar of the Arduino IDE.
  7. Search for ESP32Servo in the search box, then locate the servo library by Kevin Harrington and John K. Bennett.
  8. Click Install to install the servo motor library for ESP32.
ESP32 S3 servo motor library
  1. Copy the above code and paste it into Arduino IDE.
  2. Click the Upload button to compile and upload the code to your ESP32 S3.
How to upload ESP32 S3 code on Arduino IDE
  1. Move the magnet close to the reed switch, then move it away to simulate opening the door.
  2. Watch the servo motor rotate to 90 degrees when the door opens and return to 0 degrees when the door closes.
  3. Pro Tip: If the servo motor twitches or behaves erratically, power it from an external 5V supply instead of the ESP32 S3 board's 5V pin, as high-current servos can cause voltage dips that reset the board.

Serial Monitor Output

Open the Arduino IDE Serial Monitor at 9600 baud to observe the door sensor state and servo motor position in real time.

∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[2026-06-16 09:30:01] Door: OPEN Servo: 90 degrees [2026-06-16 09:30:07] Door: CLOSED Servo: 0 degrees [2026-06-16 09:30:13] Door: OPEN Servo: 90 degrees [2026-06-16 09:30:19] Door: CLOSED Servo: 0 degrees
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Application and Project Ideas

Once the ESP32 S3 can automatically position a servo motor in response to door sensor events, you can adapt this project into a wide range of practical automation, security, and robotics applications.

  1. Door lock arm: Attach a physical locking arm to the servo so it rotates into a locked position when the door closes and retracts when the door opens.
  2. Vent flap controller: Use the servo to open a ventilation flap when a door opens, ensuring air circulation in enclosed spaces such as server rooms or storage areas.
  3. Flag or signal indicator: Rotate a small flag or colored indicator to the "open" position whenever the door is ajar, providing a visible status at a distance.
  4. Cabinet latch: Automatically latch a cabinet door closed using the servo each time the door sensor detects the door in the closed position.
  5. Multi-door coordinator: Monitor two door sensors and map their combined states to four servo positions, implementing a simple interlocking mechanism between two adjacent doors.
  6. Event-triggered camera tilt: Mount a camera on the servo shaft so the lens automatically points toward the doorway whenever the door sensor detects an opening, capturing whoever enters.

Video Tutorial

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

Challenge Yourself

Now that you can automatically control a servo motor with a door sensor on the ESP32 S3, try these progressively harder challenges to deepen your understanding and build a more capable system.

  1. Beginner: Change the target servo angle from 90 degrees to 45 degrees by modifying a single constant in the sketch, then re-upload and verify the new position.
  2. Beginner: Add a Serial Monitor counter that increments and prints the total number of door-open events since the board was powered on.
  3. Intermediate: Use millis() instead of delay() to implement a time-delayed servo return — the servo moves to 90 degrees when the door opens but returns to 0 degrees only after 5 seconds, regardless of when the door closes.
  4. Intermediate: Add a second servo motor to the circuit and drive both servos simultaneously but in opposite directions, one opening as the other closes.
  5. Advanced: Combine the door sensor and servo with an RFID reader so the servo only rotates (unlocks) after a valid RFID card is scanned, while the door sensor provides confirmation that the door has actually been opened.
  6. Advanced: Connect the ESP32 S3 to Wi-Fi and publish the door state and servo position to an MQTT broker each time a transition occurs, enabling remote monitoring through a home automation dashboard.

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