Creating a Music Keyboard with Arduino

Creating a Music Keyboard with Arduino

Compared to instruments like trumpets and flutes, electronic keyboards are easier to understand and operate. If we use a USB keyboard as the instrument keys, we can create the simplest keyboard instrument. To achieve this goal, we need to solve two problems: sound generation and parsing USB keyboard data.

Creating a Music Keyboard with Arduino

·Sound Generation

First, we need a module that can produce musical instrument sounds. I naturally thought of various recording/playback modules, but often we need to play multiple different notes simultaneously (understood as pressing two keys at the same time), which recording/playback modules cannot handle. Next, I turned my attention to existing MIDI solutions.

MIDI stands for Musical Instrument Digital Interface, sometimes translated as “MIDI”. This technology was developed in the early 1980s to solve communication problems between electronic instruments. This solution specifies a communication mechanism for instruments, such as which piano key is pressed and how hard. When the sound source device receives this message, it plays pre-prepared sounds based on the content. Obviously, this solution saves a lot of space compared to direct recordings, for example, a 10-minute piano piece is only a few dozen KB, while a WAV file is at least several MB. After comparison, I decided to use the microchip Music Shield module based on the VS1053b chip (see Figure 1). The VS1053b chip is a single-chip Ogg Vorbis/MP3/AAC/WMA/MIDI audio decoder, which sends the instrument sound directly when it receives MIDI messages.

Creating a Music Keyboard with Arduino

Figure 1 Microchip Music Shield

The microchip Music Shield module communicates with Arduino via the SPI interface. The DataSheet of the VS1053b chip mentions that it supports a real-time MIDI mode, which can receive MIDI messages from the RX pin to generate sound after power-up. However, this mode requires hardware settings for the VS1053B pins, and commercially available modules do not support or reserve these pins. Therefore, this Music Shield still uses the SPI interface, and MIDI data is also sent to the chip via SPI.

·Parsing USB Keyboard Data

For parsing USB keyboard data, I easily thought of using the USB Host Shield I had used multiple times before, but it has the same drawbacks of using the SPI interface and complex code calls. After comparison, I ultimately decided to use the CH9350 chip produced by Nanjing Qinheng, which converts USB keyboard/mouse signals to serial communication control. Nanjing Qinheng has designed many USB-related chips, such as the widely used low-cost USB to serial solution CH340 series. The CH9350 chip is used to solve the problem of long-distance transmission of keyboard/mouse control signals.

Figure 2 shows the typical scenario of the CH9350, where the CH9350 chip on the right is responsible for parsing the data from the operator’s USB keyboard/mouse barcode scanner and converting it to TTL serial signals (TXD, RXD). This allows the signal to be sent to a remote location using RS-422/485 or network, and then received by another CH3950 chip, restoring it to a USB device for remote operation. The same method can also achieve the function of controlling multiple machines with one keyboard/mouse.

This time we connect via Arduino and the CH9350 module (see Figure 3) and read the parsed data to get the key press information.

Creating a Music Keyboard with Arduino

Figure 2 Application scenario of CH9350

Creating a Music Keyboard with Arduino

Figure 3 Installed on Arduino Proto Shield

CH9350 mini module

Creating a Music Keyboard with Arduino

Creating a Music Keyboard with Arduino

Figure 4 Hardware connections

Creating a Music Keyboard with Arduino

Figure 5 Working status of CH9350

The hardware connections are shown in Figure 4, and the actual connection uses an expansion board (Shield), which can be directly plugged in without special lines. It is important to note that the RSV on the CH9350 module needs to be grounded, which will make the chip work in state 3 after power-up (see Figure 5), automatically sending the parsed keyboard/mouse data through the serial port each time. On the software side, the main control is the Arduino Uno, leaving the hardware serial port for debugging and using the software serial to communicate with the CH9350. The main code is as follows.

// Get the current key press information sent by CH9350

while (Index<CH9350KBLENGTH-1) { // Do not save the first 0x57 frame header

if((USBKB.available())&&(0x57==USBKB.read())) {

while (Index<CH9350KBLENGTH-1) {

if (USBKB.available()) {

Current[Index++]=USBKB.read();

}

}

}

}

After the above processing, the key press information is stored in the Current [] array each time. Then the keyboard keys are mapped to MIDI notes (see Figure 6).

Creating a Music Keyboard with Arduino

Figure 6 Mapping keyboard to musical notes

Next, we continuously scan the current key array and compare it with the previous key array. If there are any keys pressed that were not previously pressed, we use the midiNoteOn() function to send them out.

// Check Current[Index], if there are keys not in Last[], they need to be sent out

for (Index=4;Index<CH9350KBLENGTH; Index++) {

Found=false;

if(Current[Index]!=0) {

// Look for it in the saved previous key array

for (i=4;i<CH9350KBLENGTH;i++) {

if (Current[Index]==Last[i]) {

Found=true;

}

}

// If not found in Last[] array, it means a new key has been pressed

if (Found==false) {

// For newly pressed keys, use midiNoteOn() function to send them out

player.midiNoteOn(0, KeyToNote(Current[Index]), 127);

Serial.print(“On “);

Serial.println(Current[Index], HEX);

Serial.print(“ >> “);

Serial.println(KeyToNote(Current [Index]));

}

}

}

We also need to check once more if there are keys that were pressed previously but are not pressed this time, indicating that a key has been released, and we need to use the midiNoteOff() function to notify the VS1053b to stop producing sound.

MIDI is a very interesting technology that ensures that a song is played in the same way in different places, representing a perfect combination of technology and art.

Creating a Music Keyboard with Arduino

Popular Science · Innovation · Practice · Sharing

Long press to recognize the QR code and follow the “Radio” magazine

Leave a Comment

×