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:

ESP32 C3 Super Mini - Motion Sensor - Piezo Buzzer

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×3-24V Active Piezo Buzzer
1×Active Piezo Buzzer Module
1×Passive Piezo Buzzer Module
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 .

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:

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
The wiring diagram between ESP32 C3 Super Mini Motion Sensor Piezo Buzzer

This image is created using Fritzing. Click to enlarge image

  • The wiring diagram with power supply from 5V adapter
The wiring diagram between ESP32 C3 Super Mini Motion Sensor Piezo Buzzer  5V power 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 motion sensor initial setting

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
/* * 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-piezo-buzzer */ #define MOTION_SENSOR_PIN 5 // The ESP32 C3 SuperMini pin GPIO5 connected to the OUTPUT pin of motion sensor #define BUZZER_PIN 6 // The ESP32 C3 SuperMini pin GPIO6 connected to Buzzer's pin int motionStateCurrent = LOW; // current state of motion sensor's pin int motionStatePrevious = LOW; // previous state of motion sensor's pin void setup() { Serial.begin(115200); // initialize serial pinMode(MOTION_SENSOR_PIN, INPUT); // set ESP32 C3 SuperMini pin to input mode pinMode(BUZZER_PIN, OUTPUT); // set ESP32 C3 SuperMini pin to output mode } void loop() { motionStatePrevious = motionStateCurrent; // store old state motionStateCurrent = digitalRead(MOTION_SENSOR_PIN); // read new state if (motionStatePrevious == LOW && motionStateCurrent == HIGH) { // pin state change: LOW -> HIGH Serial.println("Motion detected!, making sound"); digitalWrite(BUZZER_PIN, HIGH); // turn on } else if (motionStatePrevious == HIGH && motionStateCurrent == LOW) { // pin state change: HIGH -> LOW Serial.println("Motion stopped!, stops making sound"); digitalWrite(BUZZER_PIN, LOW); // turn off } }

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:

∞
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!, making sound Motion stopped!, stops making sound Motion detected!, making sound Motion stopped!, stops making sound
Ln 11, Col 1
ESP32C3 Dev Module on COM15
2

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
/* * 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-piezo-buzzer */ #include "pitches.h" #define MOTION_SENSOR_PIN 5 // The ESP32 C3 SuperMini pin GPIO5 connected to the OUTPUT pin of motion sensor #define BUZZER_PIN 6 // The ESP32 C3 SuperMini pin GPIO6 connected to Buzzer's pin // notes in the melody: int melody[] = { NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5, NOTE_D5, NOTE_G5 }; // note durations: 4 = quarter note, 8 = eighth note, etc, also called tempo: int noteDurations[] = { 8, 8, 4, 8, 8, 4, 8, 8, 8, 8, 2, 8, 8, 8, 8, 8, 8, 8, 16, 16, 8, 8, 8, 8, 4, 4 }; void setup() { Serial.begin(115200); // initialize serial pinMode(MOTION_SENSOR_PIN, INPUT); // set ESP32 C3 SuperMini pin to input mode } void loop() { int motionState = digitalRead(MOTION_SENSOR_PIN); // read new state if (motionState == HIGH) { Serial.println("The motion is detected"); buzzer(); } } void buzzer() { // iterate over the notes of the melody: int size = sizeof(noteDurations) / sizeof(int); for (int thisNote = 0; thisNote < size; thisNote++) { // to calculate the note duration, take one second divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. int noteDuration = 1000 / noteDurations[thisNote]; tone(BUZZER_PIN, melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(BUZZER_PIN); } }

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.
    Arduino IDE 2 adds file
    • Give the file the name pitches.h and click the OK button.
    Arduino IDE 2 adds file pitches.h
    • Copy the code below and paste it into the new pitches.h file.
    /************************************************* * Public Constants *************************************************/ #define NOTE_B0 31 #define NOTE_C1 33 #define NOTE_CS1 35 #define NOTE_D1 37 #define NOTE_DS1 39 #define NOTE_E1 41 #define NOTE_F1 44 #define NOTE_FS1 46 #define NOTE_G1 49 #define NOTE_GS1 52 #define NOTE_A1 55 #define NOTE_AS1 58 #define NOTE_B1 62 #define NOTE_C2 65 #define NOTE_CS2 69 #define NOTE_D2 73 #define NOTE_DS2 78 #define NOTE_E2 82 #define NOTE_F2 87 #define NOTE_FS2 93 #define NOTE_G2 98 #define NOTE_GS2 104 #define NOTE_A2 110 #define NOTE_AS2 117 #define NOTE_B2 123 #define NOTE_C3 131 #define NOTE_CS3 139 #define NOTE_D3 147 #define NOTE_DS3 156 #define NOTE_E3 165 #define NOTE_F3 175 #define NOTE_FS3 185 #define NOTE_G3 196 #define NOTE_GS3 208 #define NOTE_A3 220 #define NOTE_AS3 233 #define NOTE_B3 247 #define NOTE_C4 262 #define NOTE_CS4 277 #define NOTE_D4 294 #define NOTE_DS4 311 #define NOTE_E4 330 #define NOTE_F4 349 #define NOTE_FS4 370 #define NOTE_G4 392 #define NOTE_GS4 415 #define NOTE_A4 440 #define NOTE_AS4 466 #define NOTE_B4 494 #define NOTE_C5 523 #define NOTE_CS5 554 #define NOTE_D5 587 #define NOTE_DS5 622 #define NOTE_E5 659 #define NOTE_F5 698 #define NOTE_FS5 740 #define NOTE_G5 784 #define NOTE_GS5 831 #define NOTE_A5 880 #define NOTE_AS5 932 #define NOTE_B5 988 #define NOTE_C6 1047 #define NOTE_CS6 1109 #define NOTE_D6 1175 #define NOTE_DS6 1245 #define NOTE_E6 1319 #define NOTE_F6 1397 #define NOTE_FS6 1480 #define NOTE_G6 1568 #define NOTE_GS6 1661 #define NOTE_A6 1760 #define NOTE_AS6 1865 #define NOTE_B6 1976 #define NOTE_C7 2093 #define NOTE_CS7 2217 #define NOTE_D7 2349 #define NOTE_DS7 2489 #define NOTE_E7 2637 #define NOTE_F7 2794 #define NOTE_FS7 2960 #define NOTE_G7 3136 #define NOTE_GS7 3322 #define NOTE_A7 3520 #define NOTE_AS7 3729 #define NOTE_B7 3951 #define NOTE_C8 4186 #define NOTE_CS8 4435 #define NOTE_D8 4699 #define NOTE_DS8 4978
    • 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:

    ∞
    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
    The motion is detected The motion is detected The motion is detected
    Ln 11, Col 1
    ESP32C3 Dev Module on COM15
    2

    Line-by-line Code Explanation

    The ESP32 C3 Super Mini code above contains line-by-line explanation. Please read the comments in the code!

    ※ NOTE THAT:

    The above code using delay() function. This blocks other code during playing melody. To avoid blocking other code, use the ezBuzzer library instead. This library is designed for buzzer to beep or play melody without blocking other 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

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