iTranslated by AI
Building an Electronic Music Box with Seeed Studio XIAO RA4M1
Change Log
- rev.0 2025/01/28 Newly created
1. Introduction
I am researching the source code of the bootloader for Arduino UNO R4 Minima and summarizing it in articles.
Previous article:
Reading the Bootloader Source Code for Arduino UNO R4 Minima (SystemInit Edition)
I plan to summarize the clock settings next time, but before that, I'll take a short break and introduce an electronic project using a breadboard.
Electronic Music Box that Creates Melodies with MML
In this article, I will introduce how to make an electronic music box, which is a staple of electronic projects. The electronic music box created this time will allow you to create melodies using MML (Music Macro Language).
A microcontroller board is used to control the melody. This time, I will use the Seeed Studio XIAO RA4M1, which is equipped with the same microcontroller as the Arduino UNO R4.
This microcontroller board has a built-in charging circuit, and you can charge a battery connected to the battery connection pads on the back via the USB terminal. Although I didn't use it this time because it was a prototype on a breadboard, I felt it would be easy to charge if the USB terminal was accessible, making it easy to pack into a small box.
PSG Sound Source
The electronic music box to be created uses a PSG sound source that can output a three-voice square wave + noise sound.
However, I will not use an actual PSG sound source IC this time; instead, I will use the following emulator.
Specifically, I will integrate this PSG emulator into the XIAO RA4M1 and output the waveform calculated by the RA4M1 from the DAC pin.
Performance Demo
2. What You Will Need
Components
The electronic music box will be created using the following components.
| No. | Part Name | Model | Remarks |
|---|---|---|---|
| 1 | Microcontroller Board | XIAO-RA4M1 | Purchased at Switch Science. |
| 2 | Amplifier Module | AE-PAM8012 | Purchased at Akizuki Denshi Tsusho. |
| 3 | Pin Header | PH-1x40SG | Purchased at Akizuki Denshi Tsusho. |
| 4 | Trimmer Potentiometer (10 kΩ) | 3362P-1-103LF | Purchased at Akizuki Denshi Tsusho. |
| 5 | Speaker (8Ω) | MSI28-12R | Purchased at Akizuki Denshi Tsusho. |
| 6 | Breadboard | - | - |
| 7 | Jumper Wire | - | - |
Tools
| No. | Tool Name | Use |
|---|---|---|
| 1 | Solder and Soldering Iron | Used to attach pin headers to the microcontroller board and amplifier module. |
| 2 | Phillips Screwdriver | Used for adjusting the wiper of the trimmer potentiometer. |
| 3 | Computer | Used to write the program to the microcontroller. This article uses a Windows 10 PC. |
| 4 | USB Type-C Cable | Used for writing the program to the microcontroller and providing power. |
3. Building the Development Environment
Setting Up Arduino IDE
The electronic music box program is written to the microcontroller board using the Arduino IDE. Set up the Arduino IDE according to the Getting Started guide on the Seeed website below.
Obtaining the Source Code
Run the following git command to obtain the source code for the electronic music box.
git clone --recursive https://github.com/nyannkov/electronic-music-box
Inside this, there is a sketch named electronic-music-box.ino. Open this file in the Arduino IDE.
Next, obtain the MML decoder for the PSG sound source. First, open the Library Manager in the launched Arduino IDE. A search box will appear; type "Psgino" there and execute the installation.

Once the installation is complete, connect the XIAO RA4M1 to your PC and execute the program build and upload in the Arduino IDE.

After completion, disconnect the microcontroller board from the PC and proceed to the assembly of the electronic music box.
3. Assembling the Electronic Music Box
Installing Pin Headers (Amplifier Module)
First, install the pin headers on the amplifier module. The component on the left in the figure below is the amplifier module. Prepare two 4-pin segments by cutting the pin header as shown on the right.

Attach the pin headers to the amplifier module as shown below and fix them with solder.
| Front | Back |
|---|---|
![]() |
![]() |
Installing Pin Headers (Microcontroller Board)
Just like with the amplifier module, solder the pin headers to the microcontroller board. Use the provided pin headers.
| Front | Back |
|---|---|
![]() |
![]() |
Breadboard Wiring
Overall

Microcontroller Board
Connect the 3V3 terminal to the + rail and the GND terminal to the - rail of the breadboard. Also, connect the DAC output pin P014 (A0) to pin 3 of the trimmer potentiometer.

Trimmer Potentiometer
Connect pin 1 to the - rail and pin 2 to the IN+ terminal of the amplifier module. Also, to prevent the melody from playing at a high volume when the power is turned on, use a Phillips screwdriver to turn the wiper counter-clockwise until it stops.

Amplifier Module and Speaker
Connect the IN- and GND terminals to the - rail. Also, connect the +V terminal to the + rail. Connect the speaker to the SPK + and - terminals.

4. Operation Check
Connect the XIAO RA4M1 to your PC with a USB cable and gradually turn the wiper of the trimmer potentiometer clockwise. It's complete when the melody of "Momotaro" starts playing from the speaker.

5. How to Play
Creating and Changing Melodies
MML can be described within the code as a char array. You can change the melody by registering a pointer to the written MML using the SetMML method of a Psgino class object.
For example, if you register the following MML with the SetMML method, it will play a single-note scale of Do-Re-Mi-Fa-Sol-La-Ti-Do.
const char mml_1[] ="CDEFGAB>C";
Also, if you write it like this, a C major chord will play.
const char mml_2[] ="C,E,G";
It's a bit complex, but writing it as follows will play the drum sound heard at the beginning of the performance demo video.
const char mml_3[] =
"T120 V15"
"$E1$A0$H40$D100$S65$F500$R100"
"$P-720 I15"
"[2" "O2 A8 R8 H8 A8 A8 | R8 H8 R8" "] $P-1080 O5 A16A16 O4A16A16 O3A16R16"
"$F2000 I3 H1"
;
The MML syntax is summarized on this page.
Playing MML and Synchronizing External Devices
This MML decoder allows you to register user-defined callback processes within the MML. This enables you to describe control timing and content for external devices within the MML, synchronizing the performance with the actions of the external devices.
However, depending on the melody, it may be difficult to describe the process directly in the MML. In that case, you can create another Psgino class object as shown below to create a channel specifically for external device control.
...
const char mml[] = "T120L4[0 O4 CDEFGAB>C]";
const char mml_ext[] = "T120L4[0 @C(0x01) R4]";
void user_callback(uint8_t ch, int32_t param) {
// ch: The calling PSG channel ( 0-2 )
// param: Parameter of the @C command ( 0x01 fixed in this case )
}
void setup() {
...
psgino.Initialize(psg_write, PSG_EMU_CLOCK, MML_PROC_RATE);
psgino.SetMML(mml);
psgino.Play();
// Specify nullptr for psg_write since this channel will not operate the PSG sound source.
psgino_2.Initialize(nullptr, PSG_EMU_CLOCK, MML_PROC_RATE);
psgino_2.SetMML(mml_ext);
psgino_2.SetUserCallback(user_callback);
psgino_2.Play();
...
}
void loop() {
unsigned long time_now = micros();
if ( (time_now - time0_mml) >= EXEC_CYCLE_MML ) {
time0_mml = time_now;
psgino.Proc();
psgino_2.Proc();
}
}




Discussion