DIY: I've built a music box for the 2nd x-mas of my son
Tags: diy, arduino, cpp, music
Published: 2022-11-27 20:20:18

For last Christmas, I wanted to offer my son a music box. But I wanted it to be unique so I decided to build it myself.

In this article, I describe the main components, with links to the source code and schematics. It's not a tutorial but it can be useful if you want to do something similar.

![The MP3 music player](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h36mkoi5irm4v86jgx3d.png)

## Presentation

The music box is an MP3 player with 6 playlists, volume buttons, previous, stop and next buttons.

Here are the components:
- ELEGOO nano (4€)
- DFPlayer Mini (3.5€)
- CQRobot speaker 3 Watt 8 Ohm (4€)
- Buttons (11€)
- PCB boards + wires kit (10€)
- Battery holders (3€)
- Wooden box (8€)

The cost of this project is less than 50€, if I don't count my mistakes (2 wooden boxes wasted because the first one was slightly too small, and the second one I drilled a hole on the wrong side).

## Electronics

The player is based on an Arduino nano and a DFPlayer Mini.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pc6s00745q5uo8tfdpkb.png)

The buttons are not shown on this schematic but they are connected to the following pins:

- volume down: B2
- volume up: B3
- playlists: B4 to B9
- previous: B12
- stop: A0

### Arduino nano

The Arduino Nano is a small, complete, and breadboard-friendly board based on the ATmega328. I decided to use an Elegoo model, which is compatible but much cheaper.

The board is powered by 6 AA batteries (~9V), and we use the +5V output to power the DFPlayer Mini.

### DFPlayer Mini

The DFPlayer Mini is a small MP3 module that can play mp3 files from an SD card or a USB stick. The documentation is available there: https://wiki.dfrobot.com/DFPlayer_Mini_SKU_DFR0299

It is actually challenging to find a legit DFPlayer Mini, the one I received is based on a MH2024K-16SS chip. This means that the firmware is different and the commands are different.

## Software

The source code of this project can be found here: https://github.com/MaximeCheramy/BabyMp3Player

To develop on the Arduino, I use [PlatformIO](https://platformio.org/).

### DFPlayer support

Probably the most difficult part of this project was getting the DFPlayer to work. I accidentally bought a cheap copy based on a different chipset. I thought it was broken but it just behaved differently (no checksums, different codes for commands).

There are several libraries available to control this chipset with an Arduino. I decided to use the [DFMiniMp3 library](https://github.com/Makuna/DFMiniMp3), and in order to make it work with my module, I contributed to the library: https://github.com/Makuna/DFMiniMp3/pull/77

### Main program

The logic is quite simple:

- Configuration of the Arduino for all the buttons

- Configuration of the DFPlayer

- In a loop, we read the state of all the buttons.
  - If a button is pressed (for at least 50ms), we store the button index in a variable named `currentButton`
  - If no button is pressed (for at least 50ms), we call the `buttonUp` function with `currentButton`.

- The `buttonUp` function calls the appropriate function to change the volume, playlist, etc.

The volume level and the current playlist are stored in the EEPROM of the Arduino.

## Conclusion

I built this box exactly 1 year ago, I now have some feedback about it.

One of my biggest fear was that it would simply not work properly. Especially because of this fake DFPlayer Mini which doesn't seem as good as the original (missing commands, need to add extra delays). It turns out that this module seems to work fine. However, I have some reliability issues with the battery holders that... don't hold the batteries after a drop. Or faulty contacts (I decided not to show pictures of the inside of the box because I'm too ashamed of my soldering).
The autonomy is good enough, but I didn't implement anything to automatically turn off the board after some inactivity. As a result, if the box isn't switched off, I will have to replace the batteries.

Apart from these minor issues, it works great. I really enjoyed working on this project and my son loves his music box.