ESP32 S3 - Motion Sensor MP3 Player

This tutorial shows you how to combine an HC-SR501 PIR motion sensor and a serial MP3 player module with the ESP32 S3 to automatically play a pre-recorded audio file whenever human presence is detected. This project is versatile and can be adapted for many real-world scenarios, such as delivering automated audio announcements, playing warning tones, or triggering themed sound effects whenever motion is identified within the sensor's field of view.

What you'll build:

  1. A circuit connecting an HC-SR501 PIR motion sensor and a serial MP3 player module to the ESP32 S3
  2. A sketch that monitors the motion sensor output pin and sends playback commands to the MP3 module over UART
  3. A system that automatically starts playing audio when motion is detected and stops when motion is no longer present
  4. A foundation for building motion-activated announcement systems, security alerts, or interactive audio installations
ESP32 S3 HC-SR501 motion sensor and serial MP3 player - play a sound on a speaker when motion is detected

Overview of Motion Sensor and MP3 Player

The HC-SR501 PIR motion sensor and serial MP3 player module are an effective pairing for building motion-activated audio projects on the ESP32 S3. The PIR sensor detects changes in infrared radiation emitted by a moving person or animal, outputs a HIGH signal on its data pin, and the ESP32 S3 reads that signal to trigger serial playback commands to the MP3 module, which decodes the audio and sends it to the connected speaker.

Key Specifications

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

Wiring Diagram

Connect the motion sensor's output pin and the MP3 module's UART pins to your ESP32 S3 by following the diagram below. Double-check all connections before applying power, paying particular attention to the RX/TX polarity on the MP3 module and the sensor's VCC supply voltage requirement.

The wiring diagram between ESP32 S3 Motion Sensor MP3 Player

This image is created using Fritzing. Click to enlarge image

Safety Notes

The HC-SR501 motion sensor requires a supply voltage between 5 V and 20 V on its VCC pin — use the 5 V output available on the ESP32 S3 board to power the sensor while keeping its signal output compatible with the 3.3 V GPIO input. Never connect the speaker's amplifier power directly to the ESP32 S3's 3.3 V rail; use a dedicated 5 V source to protect the board and ensure sufficient current for the speaker.

Preparation

Before uploading code, complete the following hardware setup steps to ensure the system works correctly from the first power-on.

Time Delay AdjusterScrew it in anti-clockwise direction fully.
Detection Range AdjusterScrew it in clockwise direction fully.
Repeat Trigger SelectorPut jumper as shown on the image.
ESP32 S3 motion sensor initial setting

Pre-store your recorded MP3 file on the Micro SD Card, insert the card into the MP3 player module, connect the speaker to the MP3 module's 3.5mm Aux jack, and ensure the speaker is connected to a power source before proceeding.

ESP32 S3 Code - Motion Sensor Controls MP3 Player

The following code monitors the HC-SR501 motion sensor output on the ESP32 S3 and sends a play command to the serial MP3 module as soon as motion is detected, then sends a stop command when the sensor output returns LOW. The sensor's onboard time-delay trimmer controls how long the output stays HIGH after motion is detected, so you can fine-tune the hold time at the hardware level without modifying the code.

/* * 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-mp3-player */ /* * 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-mp3-player */ #define CMD_PLAY_NEXT 0x01 #define CMD_PLAY_PREV 0x02 #define CMD_PLAY_W_INDEX 0x03 #define CMD_SET_VOLUME 0x06 #define CMD_SEL_DEV 0x09 #define CMD_PLAY_W_VOL 0x22 #define CMD_PLAY 0x0D #define CMD_PAUSE 0x0E #define CMD_SINGLE_CYCLE 0x19 #define DEV_TF 0x02 #define SINGLE_CYCLE_ON 0x00 #define SINGLE_CYCLE_OFF 0x01 #define MOTION_SENSOR_PIN 14 // The ESP32-S3 pin GPIO14 connected to motion sensor's pin int prev_motion_state; // the previous state of motion sensor int motion_state; // the current state of motion sensor void setup() { Serial.begin(9600); Serial2.begin(9600, SERIAL_8N1, 42, 41); // ESP32-S3 RX GPIO42 (module TX), TX GPIO41 (module RX) delay(500); // wait chip initialization is complete mp3_command(CMD_SEL_DEV, DEV_TF); // select the TF card delay(200); // wait for 200ms pinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input mode motion_state = digitalRead(MOTION_SENSOR_PIN); } void loop() { prev_motion_state = motion_state; // save the last state motion_state = digitalRead(MOTION_SENSOR_PIN); // read new state if (motion_state == LOW && prev_motion_state == HIGH) { // pin state change: LOW -> HIGH Serial.println("Motion detected!"); mp3_command(CMD_PLAY, 0x0000); // Play the first mp3 file } else if (motion_state == HIGH && prev_motion_state == LOW) { // pin state change: HIGH -> LOW Serial.println("Motion stopped!"); } } void mp3_command(int8_t command, int16_t dat) { int8_t frame[8] = { 0 }; frame[0] = 0x7e; // starting byte frame[1] = 0xff; // version frame[2] = 0x06; // the number of bytes of the command without starting byte and ending byte frame[3] = command; // frame[4] = 0x00; // 0x00 = no feedback, 0x01 = feedback frame[5] = (int8_t)(dat >> 8); // data high byte frame[6] = (int8_t)(dat); // data low byte frame[7] = 0xef; // ending byte for (uint8_t i = 0; i < 8; i++) { Serial2.write(frame[i]); } }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Pre-store the MP3 file you want to play on the Micro SD Card and insert it into the MP3 player module.
  3. Wire the components following the wiring diagram shown above.
  4. Connect the ESP32 S3 board to your PC using a USB Type-C cable.
  5. Open Arduino IDE on your PC.
  6. Select the correct ESP32 S3 board and COM port from the Tools menu.
  7. Copy the above code and paste it into Arduino IDE.
  8. Click the Upload button to compile and upload the code to the ESP32 S3.
Arduino IDE Upload Code
  1. Move your hand in front of the motion sensor.
  2. Listen for audio playback from the speaker when motion is detected.
  3. Stay still and observe playback stop after the sensor's time-delay period expires.
  4. Check the Serial Monitor to see the live detection state and playback commands printed in real time.
  5. Pro Tip: Adjust the two onboard trimmers on the HC-SR501 — the sensitivity trimmer sets the detection range (up to about 7 metres) and the time-delay trimmer sets how long the output stays HIGH after motion stops — fine-tune both for your installation environment before finalising the project.

Serial Monitor Output

The following Serial Monitor output shows the ESP32 S3 reading motion data from the HC-SR501 sensor and issuing commands to the MP3 module in real time. Each line displays the current motion state and the corresponding playback action taken by the sketch.

∞
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 10:35:01] Motion: NOT DETECTED | MP3: STOPPED [2026-06-16 10:35:04] Motion: DETECTED | MP3: PLAYING [2026-06-16 10:35:05] Motion: DETECTED | MP3: PLAYING [2026-06-16 10:35:12] Motion: NOT DETECTED | MP3: STOPPED
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Application and Project Ideas

Once you can trigger MP3 audio from a motion sensor on the ESP32 S3, you can extend this pattern to a broad range of motion-activated audio and notification projects.

  1. Automated announcements: Play a pre-recorded welcome message or safety instruction whenever a person enters a specific area such as a shop entrance or warehouse zone.
  2. Security deterrent: Trigger a loud alarm tone or spoken warning via the MP3 module when an intruder is detected in a restricted area.
  3. Interactive exhibit: Activate themed sound effects or narration in a museum, escape room, or trade show display when a visitor approaches a specific point of interest.
  4. Smart doorbell: Play a custom chime or greeting message through a speaker whenever someone approaches the front door.
  5. Motion-activated toy: Build an interactive toy or prop that speaks, plays music, or makes sound effects whenever a child moves in front of it.
  6. Wildlife monitoring station: Trigger audio recordings or alerts at an outdoor monitoring post whenever an animal passes through the sensor's detection zone.

Video Tutorial

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

Challenge Yourself

Now that you can trigger MP3 playback from a motion sensor on the ESP32 S3, try these progressively challenging extensions to build more capable motion-activated audio systems.

  1. Beginner: Add an LED that lights up while the MP3 module is playing, giving a clear visual indication of active playback state alongside the audio output.
  2. Beginner: Store multiple MP3 files on the Micro SD Card and modify the code to play a randomly selected track each time motion is detected, so the audio varies with each trigger event.
  3. Intermediate: Add a push button that allows the user to enable or disable motion-triggered playback at runtime, with an LED indicating whether the system is armed or disarmed.
  4. Intermediate: Connect an I2C OLED display to the ESP32 S3 and show a live counter of how many motion-triggered playback events have occurred in the current session alongside the current detection state.
  5. Advanced: Send an MQTT message or HTTP request to a home automation platform each time motion triggers playback, using the ESP32 S3 Wi-Fi connection to log events or notify a remote dashboard.
  6. Advanced: Build a multi-zone system with two HC-SR501 sensors on separate GPIO pins and map each zone to a different MP3 track, so the audio played depends on which zone detects motion first.

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