ESP32 C3 Super Mini - Motion Sensor - Servo Motor

Make a servo motor react when someone walks by. In this beginner tutorial, the ESP32 C3 Super Mini reads an HC-SR501 motion sensor and moves a servo motor. It is an easy first step into motion-triggered automation.

In this tutorial, you'll learn:

ESP32 C3 Super Mini - Motion Sensor - Servo Motor

Hardware Preparation

1×ESP32 C3 Super Mini
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

Or you can buy the following kits:

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

A motion sensor is a module that detects moving people and animals, and a servo motor is an actuator that turns to an exact angle.

Key Features:

  • HC-SR501 motion sensor: gives a digital HIGH or LOW signal.
  • Servo motor: moves to the angle set by your code.
  • Together: a detected movement can open, close or point something by itself.

Technical Specifications:

  • HC-SR501 power: 5V
  • HC-SR501 detection range: up to 7 meters, adjustable
  • Servo motor power: 5V
  • Servo motor angle: 0° to 180°

New to these parts? These tutorials explain the pinout, how they work, and how to program them:

Pinout

Each part has three pins.

HC-SR501 motion sensor:

  • VCC pin: connect to 5V.
  • GND pin: connect to GND.
  • OUTPUT pin: sends the HIGH or LOW signal to the board.

Servo motor:

  • VCC pin: (red) connect to 5V.
  • GND pin: (brown or black) connect to GND.
  • Signal pin: (yellow or orange) receives the control signal from the board.

Wiring Diagram

Connect both parts to the ESP32 C3 Super Mini as shown below.

  • Note: The HC-SR501 needs 5V on VCC. Its OUTPUT pin gives about 3.3V, so it is safe for GPIO5.
  • Note: The 5V pin is fine for one small servo motor. Use an external power supply for a bigger servo motor.
The wiring diagram between ESP32 C3 Super Mini Motion Sensor Servo Motor

This image is created using Fritzing. Click to enlarge image

HC-SR501 Pin ESP32 C3 Super Mini Pin
VCC 5V
GND GND
OUTPUT GPIO5
Servo Motor Pin ESP32 C3 Super Mini Pin
VCC (Red) 5V
GND (Brown/Black) GND
Signal (Yellow/Orange) GPIO10

Initial Setting

Set up the HC-SR501 like this before you run the code.

Setting Configuration
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 C3 Super Mini motion sensor initial setting

ESP32 C3 Super Mini Code - Motion Sensor Controls Servo Motor

This code reads the motion sensor on GPIO5 and moves the servo motor on GPIO10.

What This Code Does:

  • Starts Serial: opens the Serial Monitor at 115200 baud.
  • Sets the pins: GPIO5 is an input and the servo motor is attached to GPIO10.
  • Starts at 0°: the servo motor goes to its start angle in setup().
  • Watches for changes: loop() compares the new sensor state with the last one.
  • Moves the servo motor: it rotates to 90° or 0° and prints a message.
/* * This ESP32 C3 Super Mini code was developed by newbiely.com * * This ESP32 C3 Super Mini code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-motion-sensor-servo-motor */ #include <ESP32Servo.h> #define MOTION_SENSOR_PIN 5 // The ESP32 C3 SuperMini pin connected to motion sensor's pin #define SERVO_PIN 10 // The ESP32 C3 SuperMini pin 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(115200); // initialize serial pinMode(MOTION_SENSOR_PIN, INPUT); // set ESP32 C3 SuperMini pin to input mode servo.attach(SERVO_PIN); // attaches the servo on GPIO10 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 == HIGH && lastMotionState == LOW) { // pin state change: LOW -> HIGH Serial.println("Motion detected!, rotating servo motor to 90°"); servo.write(90); } else if (currentMotionState == LOW && lastMotionState == HIGH) { // pin state change: HIGH -> LOW Serial.println("Motion stopped!, rotating servo motor to 0°"); servo.write(0); } }

Detailed Instructions

  • New to ESP32 C3 Super Mini? Complete our Getting Started with ESP32 C3 Super Mini tutorial first to set up your development environment.
  • Wire it up: Connect the parts as shown in the wiring diagram above.
  • Connect the board: Plug the ESP32 C3 Super Mini into your computer with a USB Type-C cable.
  • Open the IDE: Open Arduino IDE on your computer.
  • Select the board: Make sure ESP32 C3 Super Mini is selected in Tools > Board.
  • Select the port: Pick the COM port of your board in Tools > Port.
  • Install the library: Click the Libraries icon on the left bar, search for "ESP32Servo", and install the library by Kevin Harrington and John K. Bennett.
  • Search for ESP32Servo created by Kevin Harrington,John K. Bennett and click the Install button.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32C3 Dev Module
Library Manager
Type:
All
Topic:
All
ESP32Servo by Kevin Harrington,John K. Bennett
This library provides advanced PWM control for ESP32 boards using LEDC hardware, with enhanced MCPWM support on ESP32S3.
ESP32S3: 20 PWM channels (8 LEDC + 12 MCPWM) with intelligent allocation
All ESP32 variants: LEDC-based PWM with variable-frequency support
Supports variable-frequency PWM and fixed-frequency servo control
Automatic hardware fallback ensures maximum channel availability
More info
3.2.0
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32C3 Dev Module on COM15
1
  • Copy the code: Copy the code above and paste it into Arduino IDE.
  • Upload: Click the Upload button to send the code to the ESP32 C3 Super Mini.
Arduino IDE Upload Code
  • Open Serial Monitor: Set the baud rate to 115200.
  • Test the sensor: Move your hand in front of the motion sensor.
  • Watch the servo motor: See the change of the servo motor.
  • Check the result: The Serial Monitor shows the events. It looks like the below:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32C3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32C3 Dev Module' on 'COM15')
New Line
9600 baud
Motion detected!, rotating servo motor to 90° Motion stopped!, rotating servo motor to 0° Motion detected!, rotating servo motor to 90° Motion stopped!, rotating servo motor to 0°
Ln 11, Col 1
ESP32C3 Dev Module on COM15
2
  • Pro Tip: If the servo motor shakes or the board restarts, the servo motor may need more current. Try an external power supply and connect its GND to the board's GND.

Code Explanation

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

Project Ideas

You can use this motion sensor and servo motor pair in many small builds.

  • Automatic Pet Door: Open a small flap when your pet comes close.
  • Touchless Trash Bin: Lift the lid when someone walks up to it.
  • Halloween Prop: Move a scary figure when a visitor walks by.
  • Air Vent Flap: Open a small vent when people are in the room.
  • Model Gate: Raise a barrier when something moves near it.

Challenge Yourself

Try these small upgrades to your ESP32 C3 Super Mini motion sensor servo motor project.

  • Easy: Change the target angle from 90° to 45° or 180°.
  • Easy: Wait a few seconds before the servo motor returns to 0°.
  • Medium: Add an LED on GPIO3 that shows when motion is detected.
  • Medium: Count the motion events and print the total in the Serial Monitor.
  • Advanced: Use a second motion sensor on GPIO6 to cover another direction.

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