ESP32 S3 - Motion Sensor - Servo Motor

This tutorial shows you how to use the ESP32 S3 with an HC-SR501 PIR motion sensor to automatically control a servo motor based on detected movement. By combining passive infrared motion detection with precise angular actuation, you can build responsive presence-triggered systems that react instantly when someone enters the sensing area.

What you'll build:

  1. An ESP32 S3 circuit with an HC-SR501 motion sensor and a servo motor wired to the board
  2. A sketch that reads the motion sensor output continuously on a digital pin
  3. A servo motor that rotates to 90° automatically when motion is detected
  4. A servo motor that returns to 0° automatically when no motion is detected

https://newbiely.com/images/cover/esp32-s3-motion-sensor-servo-motor.jpg

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×HC-SR501 Motion Sensor
1×Alternatively, AM312 Mini Motion 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 Motion Sensor

The HC-SR501 PIR motion sensor and servo motor are a powerful combination for the ESP32 S3: the sensor detects infrared radiation changes caused by moving warm bodies and outputs a clean digital HIGH/LOW signal, while the servo delivers precise, repeatable angular movement in direct response to that signal. Together they enable a wide class of presence-aware automation projects — from automatic doors and security triggers to lighting control arms and access barriers.

Key Specifications

We have specific tutorials about each component covering detailed hardware pinout, working principles, wiring to the ESP32 S3, and full code examples. Learn more at the links below:

Wiring Diagram

Connect the HC-SR501 and the servo motor to your ESP32 S3 as shown in the diagram below. The HC-SR501 operates at 5V so connect its VCC pin to the 5V rail, and ensure the servo motor power wire is also supplied from 5V for reliable torque.

Safety Notes

Servo motors can briefly draw several hundred milliamps under load, which can stress the ESP32 S3's USB supply if used for extended periods. For bench testing, the USB port is generally sufficient; for any deployment scenario, power the servo from a dedicated 5V external supply and share only the GND rail with the ESP32 S3. The HC-SR501 motion sensor also requires 5V on its VCC pin — connecting it to 3.3V will result in incorrect or absent trigger signals.

The wiring diagram between ESP32 S3 Motion Sensor Servo Motor

This image is created using Fritzing. Click to enlarge image

Component Pin ESP32 S3 Pin
Motion Sensor VCC 5V
Motion Sensor GND GND
Motion Sensor OUT GPIO42
Servo Motor GND (Brown/Black) GND
Servo Motor VCC (Red) 5V
Servo Motor Signal (Yellow/Orange) GPIO9

Loose wires are a common cause of strange bugs. You can plug the ESP32 S3 into a Screw Terminal Block for ESP32 S3 and connect the wires to its screw terminals. Below is the wiring diagram with the screw terminal board.

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

This image is created using Fritzing. Click to enlarge image

Here is the same circuit with a GPIO Breakout Board for ESP32 S3, which breaks out every GPIO to pin headers:

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

This image is created using Fritzing. Click to enlarge image

Initial Setting

Detection Range Adjuster Fully screw it in the clockwise direction.
Time Delay Adjuster Fully screw it in the anti-clockwise direction.
Repeat Trigger Selector Put jumper like the below image.
ESP32 S3 motion sensor initial setting

ESP32 S3 Code - Motion Sensor Controls Servo Motor

The following code uses the ESP32 S3 to read the digital output of the HC-SR501 motion sensor and translate detected motion into a servo motor angle. When motion is detected, the ESP32 S3 commands the servo to rotate to 90°; when motion is no longer present, the servo returns to 0°. The ESP32Servo library handles PWM signal generation for the servo, keeping the sketch straightforward and easy to adapt.

/* * 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-motion-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-motion-sensor-servo-motor */ #include <ESP32Servo.h> #define MOTION_SENSOR_PIN 42 // ESP32-S3 pin GPIO42 connected to motion sensor's pin #define SERVO_PIN 9 // ESP32-S3 pin GPIO9 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 lastMotionState; // the previous state of motion sensor int currentMotionState; // the current state of motion sensor void setup() { Serial.begin(9600); // initialize serial pinMode(MOTION_SENSOR_PIN, INPUT); // set ESP32-S3 pin to input mode servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(angle); currentMotionState = digitalRead(MOTION_SENSOR_PIN); } void loop() { lastMotionState = currentMotionState; // save the last state currentMotionState = digitalRead(MOTION_SENSOR_PIN); // read new state if (currentMotionState == LOW && lastMotionState == HIGH) { // pin state change: LOW -> HIGH Serial.println("Motion detected!, rotating servo motor to 90°"); servo.write(90); } else if (currentMotionState == HIGH && lastMotionState == LOW) { // pin state change: HIGH -> LOW Serial.println("Motion stopped!, rotating servo motor to 0°"); servo.write(0); } }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Wire the components: Follow the wiring diagram shown above to connect the HC-SR501 motion sensor and servo motor to your ESP32 S3.
  3. Connect ESP32 S3: Use a USB Type-C cable to connect the board to your computer.
  4. Setup Arduino IDE: If this is the first time you use ESP32 S3, see how to setup environment for ESP32 S3 on Arduino IDE.
  5. Select your board: Choose the ESP32 S3 board and its COM port under Tools in Arduino IDE.
  6. Install ESP32Servo library: Click the Libraries icon on the left bar of the Arduino IDE. Type ESP32Servo in the search box, then look for the servo library by Kevin Harrington, John K. Bennett. Click Install to install the servo motor library for ESP32.
ESP32 S3 servo motor library
  1. Upload the code: Copy the code above and paste it into Arduino IDE, then click the Upload button.
Arduino IDE Upload Code
  1. Open Serial Monitor: Open the Serial Monitor at 9600 baud to see live motion detection status and servo angle updates.
  2. Test the sensor: Move your hand in front of the motion sensor and observe the servo motor responding to detected motion.
  3. Pro Tip: If the servo twitches erratically after the sensor stops detecting motion, add a small capacitor (100–470 µF) across the servo's power rails to filter voltage spikes caused by the motor returning to 0°.

Serial Monitor Output

Open the Serial Monitor at 9600 baud after uploading. You will see output similar to the following as you move in front of the sensor:

∞
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:22:11 -> Motion: NOT DETECTED | Servo angle: 0° 2026-06-16 09:22:14 -> Motion: DETECTED | Servo angle: 90° 2026-06-16 09:22:17 -> Motion: DETECTED | Servo angle: 90° 2026-06-16 09:22:21 -> Motion: NOT DETECTED | Servo angle: 0°
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Application Ideas

Combining the ESP32 S3 with a motion sensor and servo motor unlocks a broad set of presence-triggered actuation projects across home automation, security, and robotics. Here are some practical ideas to get you started:

  1. Automatic door or gate: Use the servo to swing a barrier open when a person is detected in the sensing area, then close it automatically once motion stops.
  2. Security camera pan trigger: Rotate a servo-mounted camera toward the detected motion zone to automatically frame any intruder or visitor.
  3. Presence-activated vent or damper: Open or close an air vent flap with the servo when people are present in a room, improving HVAC efficiency.
  4. Touchless access barrier: Lift a servo-driven arm to grant entry when someone approaches a restricted area, then lower it once they have passed.
  5. Pet feeder trigger: Rotate a servo-controlled dispensing wheel to release food when your pet approaches the feeding station.
  6. Alarm latch: Physically engage or disengage a servo-driven locking pin when motion is detected within a security perimeter.

Video Tutorial

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

Challenge Yourself

Now that your ESP32 S3 motion-sensor-servo circuit is working, try these progressively harder challenges to deepen your understanding of PIR sensing and servo actuation:

  1. Beginner: Change the servo target angle in the code from 90° to 45° or 180° and observe how the servo responds differently to the same motion events.
  2. Beginner: Add a delay after motion stops before the servo returns to 0°, so the servo holds its position for a few seconds after the last detected movement.
  3. Intermediate: Add an LED indicator to the ESP32 S3 that lights up when motion is detected, providing a visual cue alongside the servo movement.
  4. Intermediate: Log motion events with a timestamp to the Serial Monitor, recording each time the sensor transitions from NOT DETECTED to DETECTED and back.
  5. Advanced: Add an OLED display to the ESP32 S3 that shows the current motion status and the number of motion events detected since power-on.
  6. Advanced: Build a multi-zone system using two HC-SR501 sensors covering different directions, each independently triggering its own servo when motion is detected in its zone.

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