ESP32 C3 Super Mini - Motion Sensor - Piezo Buzzer
Build a simple motion alarm with an ESP32 C3 Super Mini, an HC-SR501 motion sensor and a piezo buzzer. When someone moves near the sensor, the buzzer makes a sound or plays a song.
In this tutorial, you'll learn:
- How to connect the HC-SR501 motion sensor and a piezo buzzer to ESP32 C3 Super Mini
- How to set the sensor's adjusters before you start
- How to make a sound when motion is detected, and stop the sound when motion stops
- How to play a melody when motion is detected
- How to create the pitches.h file in the Arduino IDE

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Piezo Buzzer and Motion Sensor
The HC-SR501 is a motion sensor that detects moving people and animals, and the piezo buzzer is a small part that makes sound.
Key Features:
- HC-SR501 output: The OUTPUT pin is HIGH when motion is detected and LOW when there is no motion
- Piezo buzzer: Needs only one ESP32 C3 Super Mini GPIO pin
- Active buzzer: Makes a beep when its pin is HIGH, so it suits the simple sound code
- Passive buzzer: Plays different notes with tone(), so it suits the melody code
Technical Specifications:
- HC-SR501 power: 5V
- HC-SR501 OUTPUT signal: 3.3V when HIGH, safe for an ESP32 C3 Super Mini GPIO pin
- Detection range: Up to 7 meters, set by an adjuster on the sensor
- Active piezo buzzer voltage: 3V to 24V
- Piezo buzzer module voltage: 3.3V or 5V
Learn more about each part:
- 12V buzzer: This tutorial uses a buzzer at low voltage, but you can adapt it for a 12V buzzer. See the ESP32 C3 Super Mini - Buzzer tutorial
Wiring Diagram
Connect the motion sensor to GPIO5 and the buzzer to GPIO6 on the ESP32 C3 Super Mini.
- Note: Connect the HC-SR501 VCC pin to 5V. Its OUTPUT signal is 3.3V, so it is safe for GPIO5.
- Note: Check the polarity of the bare buzzer. The (+) pin has a longer leg or a + mark.
- Note: ESP32 C3 Super Mini GPIO pins are 3.3V only. Never feed 5V into a GPIO pin.
- The wiring diagram with power supply from USB cable

This image is created using Fritzing. Click to enlarge image
- The wiring diagram with power supply from 5V adapter

This image is created using Fritzing. Click to enlarge image
| HC-SR501 Pin | ESP32 C3 Super Mini Pin |
|---|---|
| VCC | 5V |
| GND | GND |
| OUTPUT | GPIO5 |
| Buzzer Pin | ESP32 C3 Super Mini Pin |
|---|---|
| Positive (+) | GPIO6 |
| Negative (-) | GND |
If you use a buzzer module, connect it like this:
| Module Pin | ESP32 C3 Super Mini Pin |
|---|---|
| GND | GND |
| VCC | 3.3V |
| I/O | GPIO6 |
Initial Setting
Set the adjusters and the jumper on the HC-SR501 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 the jumper as shown in the image below. |

ESP32 C3 Super Mini Code - Simple Sound
This code turns the buzzer on when motion starts and turns it off when motion stops.
What This Code Does:
- Reads the sensor: Checks the OUTPUT pin of the HC-SR501 on GPIO5 again and again
- Finds motion start: A change from LOW to HIGH turns the buzzer on through GPIO6
- Finds motion stop: A change from HIGH to LOW turns the buzzer off
- Reports events: Prints a message to the Serial Monitor at 115200 baud
Detailed Instructions
Follow these bullets to run the simple sound example.
- New to ESP32 C3 Super Mini? Complete our Getting Started with ESP32 C3 Super Mini tutorial first to set up your development environment.
- Wire the parts: Do the wiring as shown in the diagram above.
- Connect the board: Plug the ESP32 C3 Super Mini into your PC with a USB Type-C cable.
- Open the IDE: Open the Arduino IDE on your PC.
- Select the board: Make sure ESP32 C3 Super Mini is selected in Tools > Board.
- Select the port: Pick the COM port of your board.
- Copy the code: Copy the code above and paste it into the Arduino IDE.
- Upload the code: Click the Upload button in the Arduino IDE.
- Open the Serial Monitor: Set the baud rate to 115200.
- Test it: Move your hand in front of the sensor.
- Listen: The piezo buzzer makes a sound.
- Pro Tip: No sound? Use an active buzzer for this code. A passive buzzer stays silent when its pin is only held HIGH.
Serial Monitor Output
You will see these messages when motion starts and stops:
Line-by-line Code Explanation
The ESP32 C3 Super Mini code above contains line-by-line explanation. Please read the comments in the code!
ESP32 C3 Super Mini Code - Melody
This code plays a melody on the buzzer whenever the sensor detects motion.
What This Code Does:
- Reads the sensor: Checks the OUTPUT pin of the HC-SR501 on GPIO5 in every loop
- Plays the song: Calls buzzer() when the pin is HIGH, using tone() on GPIO6
- Stores the song: Keeps the notes in melody[] and their lengths in noteDurations[]
- Stays quiet: Plays nothing while there is no motion
- Reports events: Prints a message to the Serial Monitor at 115200 baud
Detailed Instructions
Follow these bullets to run the melody example.
- New to ESP32 C3 Super Mini? Complete our Getting Started with ESP32 C3 Super Mini tutorial first to set up your development environment.
- Select the board: Make sure ESP32 C3 Super Mini is selected in Tools > Board.
- Copy the code: Copy the code above and paste it into the Arduino IDE.
- Create the pitches.h file in the Arduino IDE by:
- Either click on the button just below the serial monitor icon and choose New Tab, or use Ctrl+Shift+N keys.
- Give the file the name pitches.h and click the OK button.
- Copy the code below and paste it into the new pitches.h file.
- Upload the code: Click the Upload button in the Arduino IDE.
- Open the Serial Monitor: Set the baud rate to 115200.
- Test it: Move your hand in front of the sensor.
- Listen: The piezo buzzer plays the melody.
- Pro Tip: Use the passive buzzer module for this code. An active buzzer cannot play different notes.


Serial Monitor Output
You will see this message each time the melody starts:
Line-by-line Code Explanation
The ESP32 C3 Super Mini code above contains line-by-line explanation. Please read the comments in the code!
Application/Project Ideas
A motion sensor and a buzzer are a good start for many ESP32 C3 Super Mini projects.
- Door Alarm: Beep when someone walks through a door
- Welcome Chime: Play a short song when a guest enters a shop
- Pet Alert: Make a sound when your pet goes near the kitchen table
- Garage Warning: Beep when someone moves near your car
- Halloween Prop: Play a scary tune when someone comes close
Challenge Yourself
Try these small tasks to learn more about the ESP32 C3 Super Mini and the HC-SR501.
- Easy: Change the melody to another song with notes from pitches.h
- Easy: Make the buzzer beep on and off instead of staying on
- Medium: Add an LED on GPIO3 that turns on together with the buzzer
- Medium: Keep the buzzer on for 5 seconds after motion stops
- Advanced: Use the ezBuzzer library so the melody does not block other code