ESP32 - Motion Sensor MP3 Player

In this guide, we will explore how to employ the ESP32, an HC-SR501 motion sensor, and an MP3 player to initiate the playback of a pre-recorded audio file when motion is detected. This project offers versatility and can be customized for various applications, such as delivering automated audio instructions or warnings whenever human presence is identified.

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×HC-SR501 Motion Sensor
1×Serial MP3 Player Module
1×Micro SD Card
1×3.5mm Aux Speaker
1×Jumper Wires
1×(Recommended) ESP32 Screw Terminal Adapter

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to MP3 Player and Motion Sensor

Unfamiliar with MP3 player and motion sensor, including their pinouts, functionality, and programming? Explore comprehensive tutorials on these topics below:

Wiring Diagram

ESP32 Motion Sensor MP3 Player Wiring Diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.


  • Pre-store the recorded mp3 file that we want to play to the micro SD Card.
  • Insert the micro SD Card to the MP3 player module
  • Connect the MP3 player module to ESP32
  • Connect the speaker to the MP3 player module to a
  • Connect the speaker to a power source.
  • Connect the motion sensor to the ESP32.
  • Do setting for motion sensor as below table
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 motion sensor initial setting

ESP32 Code - Motion Sensor Controls MP3 Player

/* * This ESP32 code is created by * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit */ #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 21 // The ESP32 pin GPIO21 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); 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]); } }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Connect ESP32 to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
  • Move your hand in front of sensor
  • Check out the audio from MP3 player

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.