Arduino Nano 33 IoT - MP3 Player

In this guide, we will learn how to build an MP3 player using an Arduino Nano 33 IoT, an MP3 player module, a Micro SD Card, and a speaker. The MP3 files, which can be songs or spoken audio, are stored on the Micro SD Card. The Arduino Nano 33 IoT will be programmed to tell the MP3 player module which song to play from the SD Card, change it into sound, and then send that sound to the speaker. We will cover these topics:

After that, you can improve the code further by adding a potentiometer or rotary encoder to change the volume.

Arduino Nano 33 IoT MP3 player module

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×Serial MP3 Player Module
1×Micro SD Card
1×3.5mm Aux Speaker
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

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 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 Serial MP3 Player Module and Speaker

Serial MP3 Player Module Pinout

A serial MP3 player module includes three connection points:

  • The Arduino Nano 33 IoT has 4 connections:
  • RX pin: This is a data pin that should be connected to the TX pin of the Arduino.
  • TX pin: This is a data pin that should be connected to the RX pin of the Arduino.
  • VCC pin: This is the power pin that should be connected to 5V.
  • GND pin: This is the ground pin that should be connected to 0V.
  • The speaker connects with a 3.5mm aux output female jack.
  • The Micro SD Card connects through a Micro SD Card socket located at the back of the module.
Serial MP3 Player Module Pinout

Speaker Pinout

A speaker normally has two types of connections:

  • Audio signal interface: This uses a 3.5mm aux plug (male) that connects directly to the MP3 player module.
  • Power interface: It can use a USB cable, a 5V power adapter, or any other suitable power connector.

How It Works

To start, please make sure you have these things:

  • Collect a set of songs or audio recordings you want to play and save them on a micro SD Card.
  • Insert the micro SD Card into the MP3 player module.
  • Connect the MP3 player module to the Arduino Nano 33 IoT and attach the speaker to the MP3 player module. Also, make sure the speaker has a power connection.

Every MP3 file on the micro SD card is given an ID number that starts at 0. Then you can instruct the Arduino Nano 33 IoT to control the MP3 player module to do different tasks, such as:

  • Play: Begin the selected song.
  • Pause: Stop the song for now.
  • Play Next: Skip to the next song.
  • Play Previous: Return to the last song.
  • Change Volume: Adjust the sound level.

When the MP3 player module receives a command, it opens the MP3 file from the micro SD card, changes it into sound, and sends that sound to the speaker using a 3.5mm Aux cable.

Wiring Diagram

The wiring diagram between Arduino Nano and 33 IoT MP3 player module

This image is created using Fritzing. Click to enlarge image

Arduino Nano 33 IoT Code - Play Music

This code plays the first song on the Micro SD card.

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-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 RX1PIN 8 // Arduino Nano Pin connected to the TX of the Serial MP3 Player module #define TX1PIN 7 // Arduino Nano Pin connected to the RX of the Serial MP3 Player module void setup() { Serial.begin(9600); Serial1.begin(9600, SERIAL_8N1, RX1PIN, TX1PIN); delay(500); // wait chip initialization is complete mp3_command(CMD_SEL_DEV, DEV_TF); // select the TF card delay(200); // wait for 200ms mp3_command(CMD_PLAY, 0x0000); // Play mp3 //mp3_command(CMD_PAUSE, 0x0000); // Pause mp3 //mp3_command(CMD_PLAY_NEXT, 0x0000); // Play next mp3 //mp3_command(CMD_PLAY_PREV, 0x0000); // Play previous mp3 //mp3_command(CMD_SET_VOLUME, 30); // Change volume to 30 } void loop() { } 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++) { Serial1.write(frame[i]); } }

Detailed Instructions

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Read the guide in the "How It Works" section.
  • Copy the code above and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to your Arduino Nano 33 IoT.
  • Enjoy the music!

Arduino Nano 33 IoT Code - Play Music with control buttons

The code below is an improved version of the previous code. It adds four buttons that let you control the MP3 player.

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-mp3-player */ #include <ezButton.h> #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 RX1PIN 8 // Arduino Nano 33 IoT Pin connected to the TX of the Serial MP3 Player module #define TX1PIN 7 // Arduino Nano 33 IoT Pin connected to the RX of the Serial MP3 Player module ezButton button_play(2); // create ezButton object for pin D2 ezButton button_pause(3); // create ezButton object for pin D3 ezButton button_next(4); // create ezButton object for pin D4 ezButton button_prev(5); // create ezButton object for pin D5 void setup() { Serial.begin(9600); Serial1.begin(9600, SERIAL_8N1, RX1PIN, TX1PIN);; delay(500); // wait chip initialization is complete mp3_command(CMD_SEL_DEV, DEV_TF); // select the TF card delay(200); // wait for 200ms button_play.setDebounceTime(50); // set debounce time to 50 milliseconds button_pause.setDebounceTime(50); // set debounce time to 50 milliseconds button_next.setDebounceTime(50); // set debounce time to 50 milliseconds button_prev.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { button_play.loop(); // MUST call the loop() function first button_pause.loop(); // MUST call the loop() function first button_next.loop(); // MUST call the loop() function first button_prev.loop(); // MUST call the loop() function first if (button_play.isPressed()) { Serial.println("Play mp3"); mp3_command(CMD_PLAY, 0x0000); } if (button_pause.isPressed()) { Serial.println("Pause mp3"); mp3_command(CMD_PAUSE, 0x0000); } if (button_next.isPressed()) { Serial.println("Play next mp3"); mp3_command(CMD_PLAY_NEXT, 0x0000); } if (button_prev.isPressed()) { Serial.println("Play previous mp3"); mp3_command(CMD_PLAY_PREV, 0x0000); } } 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++) { Serial1.write(frame[i]); } }

Wiring diagram for the code above:

The wiring diagram between Arduino Nano and 33 IoT MP3 player speaker

This image is created using Fritzing. Click to enlarge image

You can now change the projects to add more features, such as:

※ NOTE THAT:

Please note that the Arduino Nano 33 IoT pins A4 and A5 have built-in pull-up resistors for I2C communication. Although these pins can be used as digital input pins, it is recommended to avoid using them for digital input. If you must use them, do not use internal or external pull-down resistors for these pins

Video Tutorial

Function References

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