A brand-new issue of Raspberry Pi Official Magazine is out today! One of our favourite projects featured therein is this one, in which Phil King teaches us how to play drum samples at the press of a button with Raspberry Pi Pico.

While the first ever drum machine is considered to be the Rhythmicon, developed by Leon Theremin (yes, he invented that instrument too) in the early 1930s, digital drum machines came to the fore in the 1980s with the likes of the Linn LM-1. The latter cost the equivalent of over $19,000 today, and yet we can now create a DIY drum machine using a $5 Raspberry Pi Pico.
Inspired by Arnov Sharma’s project on Hackster, your author decided to try to create a Pico-powered drum machine using a DF Mini Player to play the drum samples. While Arnov’s version uses a custom PCB for the buttons, we decided to keep it simple with a breadboard-based design — the downside being the spaghetti of jumper wires needed to connect everything up. Still, the principle is the same: you press different push buttons to trigger different drum samples on the DF Mini Player, outputting the audio to a mini speaker.
Building the circuit
To make it easier to wire the buttons to our Raspberry Pi Pico, we opted to put them on a second half-size breadboard, as you can see in the wiring diagram (Figure 1). Each four-legged button spans the division in the middle of the breadboard, with the pins on one side connected to a ground rail. The other side of each button is connected to a GPIO pin on Pico — we used GPIO 28, 27, 26, 21, 20, and 19 — that is pulled up (in the code). So when you press the button, the GPIO pin is pulled low, and the code senses this and triggers the DF Mini Player to play the relevant sample (more on that later).

The DF Mini Player was placed on the same breadboard as Pico, connected via UART RX and TX pins, along with 3.3V power and ground. We used its speaker output pins to connect a mini speaker (ours was 2W, 8Ω).
To play some sounds, we needed some drum samples. There are lots of free, open-source ones available online; we got ours from GitHub. We found that some of the samples were a little long — and the DF Mini Player can only play one file at a time — so we opted to edit them in Audacity. If, after trimming the end of a sample, you find it ends too abruptly, you can always apply a fade-out effect.

The microSD card must be formatted as FAT32, so we erased it in Raspberry Pi Imager: select Choose OS > Erase, then Choose Storage and select the card. In addition, the files must be named 0001, 0002, 0003, etc., with the relevant suffix — you can use MP3 or WAV files. As we used the latter, ours were named 0001.wav, 0002.wav… 0006.wav. We found that it doesn’t matter whether you put them in a folder or not.
Coding it
Unlike Arnov, who programmed his drum machine in C, we opted to use MicroPython. For this, we made use of Stewart Watkiss’ DF Mini Player library. Just download the dfplayermini.py script from there and then, using the Files tab in Thonny IDE, right-click and upload the file to your connected Pico (which already has MicroPython installed). You can then call the library in your programs.

To make sure our drum samples were playing correctly, we created a program (play_drums_seq.py) to test them in sequence:
from dfplayermini import DFPlayerMini
import time
player1 = DFPlayerMini(1, 4, 5)
player1.reset()
print ("Set SD Card")
read_value = player1.select_source('sdcard')
print ("Set Volume 30")
read_value = player1.set_volume(30)
read_value = player1.query_num_files()
print (f"Num files {read_value}")
for i in range(6):
print("Play",i+1)
read_value = player1.play(i+1)
time.sleep(1)
After importing the libraries, we set up a player1
object to work with UART 1 on GPIO pins 4 and 5. We then reset the DF Mini Player so it was ready to start communicating and selected the SD card as the audio source, before setting the volume — we maxed ours up to 30. Next, we queried the number of files on the card (which should be six) and played each one in turn.

We then adapted this to create our main program, which reads the button presses and triggers the sounds accordingly:
from dfplayermini import DFPlayerMini
from machine import Pin
import time
player1 = DFPlayerMini(1, 4, 5)
player1.reset()
print ("Set SD Card")
read_value = player1.select_source('sdcard')
print ("Set Volume 30")
read_value = player1.set_volume(30)
read_value = player1.query_num_files()
print (f"Num files {read_value}")
# Define GPIO pins for buttons
button_pins = [28, 27, 26, 21, 20, 19]
# Initialize input pins
buttons = [Pin(pin, Pin.IN, Pin.PULL_UP) for
pin in button_pins]
# Main loop
while True:
for i in range(len(buttons)):
# Read button state
if buttons[i].value() == 0:
player1.stop() # stop current sound
read_value = player1.play(i+1)
time.sleep(0.01) # Debounce delay
Here, we added a line at the top to import the Pin method from the machine library. We created a list to set the GPIO pins for the buttons, then initialised them as inputs with the pin pulled up. In the main loop, we read the button state and then, after stopping any currently playing sound, played the relevant drum sample. We added a very short debounce delay to prevent a button press causing multiple triggers.

Hands up: the performance wasn’t as good as we’d hoped, with a noticeable lag between pressing a button and the sound being played. As already mentioned, the DF Mini Player can only play one file at a time, which is a major limitation for a drum machine. An alternative would be to use an I2S-based audio board, such as the Waveshare Pico-Audio, to play the drum sounds. Still, our little Pico drum machine does work, and you could use it to trigger other samples, such as spoken phrases or funny noises. Alternatively, using four of the buttons, you could try out Stewart Watkiss’ MP3 player project to play songs stored on the microSD card.
Raspberry Pi Official Magazine #155 out NOW!
You can grab the latest issue right now from Tesco, Sainsbury’s, Asda, WHSmith, and other newsagents, including the Raspberry Pi Store in Cambridge. It’s also available from our online store, which ships around the world. And you can get a digital version via our app on Android or iOS.

You can also subscribe to the print version of our magazine. Not only do we deliver worldwide, but people who sign up to the six- or twelve-month print subscription get a FREE Raspberry Pi Pico W!
The post Raspberry Pi Pico–powered drum machine appeared first on Raspberry Pi.