Build an Itty Bitty Beat Box

UM TISH! UM TISH! UM TISH! Let's combine pHAT Stack, Speaker pHAT, Piano HAT, and Drum HAT into an Itty Bitty Beat Box! Using Speaker pHAT means that the whole thing is self-contained and could easily be housed in an enclosure to make the setup more permanent.

Itty Bitty Beat Box

Youll need the following:

Mounting the HATs and pHAT

If you have either the bare pHAT Stack PCB or the solder yourself kit, then you'll need to solder it up before you do anything else. You can follow our Getting Started with pHAT Stack tutorial to get tips on how to set up your pHAT Stack.

(Note that the order of the pHAT/HATs on pHAT Stack doesn't really matter, the below is just a suggestion)

If you haven't assembled your Speaker pHAT, then you can follow our Assembling Speaker pHAT tutorial to learn how to do so.

We'll mount the Speaker pHAT on the second set of pins from the top (pHAT slot 0). Screw a pair of the metal standoffs into the bottom set of mounting holes in pHAT slot 0, pushing the screws through from the underside of the pHAT Stack and then screwing the standoffs in from above. Mount the Speaker pHAT, and then secure to the standoffs with two more screws.

Attach another pair of standoffs to the bottom set of mounting holes in pHAT slot 2, but leave them slightly loose so that you can still reposition them (they're slots so that you can use them with HATs or pHATs, and the position varies slightly). Mount the Piano HAT in pHAT slot 1, secure on top with metal screws, and then tighten the bottom set of screws up.

Last, we'll attach a third pair of standoffs to the very bottom set of mounting holes on pHAT Stack, again leaving them a little loose until they're positioned correctly. Mount the Drum HAT on pHAT slot 3, secure on top with metal screws, and then tighten the bottom set of screws up, as for Piano HAT.

Connecting your pHAT Stack to your Pi

Use the GPIO ribbon cable to connect the pHAT Stack to your Pi. The ribbon cable should connect and lay across the top of your Pi, so that it runs over the HDMI and power ports rather than out over the top of your Pi.

Plug the other end of the ribbon cable onto your pHAT Stack, pop an SD card into your Pi, and connect the power, and you're all set!

Installing the software

We always recommend using the most up-to-date version of Raspbian, as this is what we test our boards and software against, and it often helps to start with a completely fresh install of Raspbian, although this isn't necessary.

All three of the bits of software that we'll need can be installed by our nifty one-line-installers - the Speaker pHAT software, and the Piano HAT and Drum HAT Python libraries.

Open a terminal on your Pi and type the following to install the Speaker pHAT software. This will configure the sound to be routed out through the Speaker pHAT and install the VU meter plugin (the row of 10 LEDs). Type y and press enter for any yes/no prompts (it'll probably prompt you to reboot too).

curl https://get.pimoroni.com/speakerphat | bash

Next, we'll install the Piano HAT Python library. Again in the terminal, type the following, and type y when it asks you if you want to perform a full install.

curl https://get.pimoroni.com/pianohat | bash

Finally, we'll install the Drum HAT Python library. Type the following in the terminal, again typing y when it asks you if you want to perform a full install.

curl https://get.pimoroni.com/drumhat | bash

Before we go any further, we'll check that the Piano HAT and Drum HAT libraries installed properly and that the audio is coming out through Speaker pHAT as it should. In the terminal, type:

cd /home/pi/Pimoroni/pianohat/examples
python simple-piano.py

Try pressing the keys on Piano HAT, and you should hear the piano notes playing and the LEDs on each key lighting up! If you hear drum sounds, then press the "Instrument" key to cycle to the piano sounds. The LEDs on Speaker pHAT should also be lighting. You can press control and c (at the same time) to stop the example running.

Next, we'll test that Drum HAT is installed properly. Type the following in the terminal:

cd /home/pi/Pimoroni/drumhat/examples
python drums.py

BADUM TISH! Tap the pads on Drum HAT and you should hear a cacophony of percussion sounds. Assuming that all of the above works, we can start putting together the code to build our Itty Bitty Beat Box!

Coding the Itty Bitty Beat Box

Because the simple-piano.py and the drums.py examples do everything that we want to do already, we're going to write a little bash script to run both of the examples and then stop them both running when we're done.

Copy the following and save it in a file called itty-bitty-beat-box.sh. You can then run it by typing bash itty-bitty-beat-box.sh in the terminal. We'll explain what it does below.

#!/usr/bin/env bash

python /home/pi/Pimoroni/pianohat/examples/simple-piano.py > /dev/null 2>&1 &
python /home/pi/Pimoroni/drumhat/examples/drums.py > /dev/null 2>&1 &

running=true

echo "Press control-c to quit!"

trap ctrl_c INT

function ctrl_c() {
    killall python
    running=false
}

while $running
do
    sleep 1
done

The two lines beginning python run the two examples. At the end of each line, the > /dev/null 2>&1 & silences the output of the scripts and runs them both in the background (so that they can both run at the same time).

We create a variable called running and set it to true, so that we can use it to keep the script running for as long as we want. We also print a message telling the user how to quit the script from running, using echo (the equivalent of print in Python).

The next few lines allow us to catch when the user presses control and c and then kill any Python scripts that are running, i.e. the two example scripts. As part of the ctrl_c function, we also set running to false.

The last part of the script is just a simple while loop that will keep the script running as long as the running variable is true, sleeping for 1 second on every iteration of the loop. When running is no longer true, this loop will exit and the script will end.

If you want to run this script automatically when your Pi boots, then you can add the line @reboot bash /home/pi/itty-bitty-beat-box.sh & to your crontab (type crontab -e in the terminal).

A post shared by pimoroni (@pimoroni) on

Taking it further

Try running one of the other Piano HAT examples, like 8bit-synth.py, or add some of your own sound samples to the simple-piano.py example (they'll need to be sequentially numbered wav files and sounds directory with the directory where the pianohat library is).

Why not switch out one of the HATs - Piano HAT or Drum HAT - for a Unicorn HAT HD and visualise the key or drum pad presses with different coloured flashes?

You could use an app like Loopy HD to record your jams, loop them, and then play over the top, and even mix in other instruments.

Shopping basket

Need something for this project? You can use the links below to add products to your Pimoroni Shop basket for easy checkout.

pHAT Stack
Fully Assembled Kit £14.00
pHAT Stack
Solder Yourself Kit £12.00
pHAT Stack
PCB Only £6.00
Speaker pHAT
£12.00
Piano HAT
£15.00
Drum HAT
£12.00
Raspberry Pi Zero
Pi Zero only £4.80
Raspberry Pi Zero
Pi Zero + Adaptors £9.00
Raspberry Pi Zero
Pi Zero + Adaptors + Pibow Zero Case £13.00
Raspberry Pi Zero W
Zero W only £9.60
Raspberry Pi Zero W
Zero W + Adaptors £14.00
Raspberry Pi Zero W
Zero W + Adaptors + Pibow Zero W £19.00
Raspberry Pi 3 Only
£32.00
Antex XS25 Soldering Iron (UK Plug)
£30.00
Antex Lead Free Solder 2m
£3.00
Want to checkout or change something? Click here to view your cart.

Tutorial
Beginner
Raspberry Pi, Pi Zero

Sandy Macdonald

sandy@pimoroni.com
@sandyjmacdonald
http://sandyjmacdonald.github.io
Formerly, Sandy worked at the University of York, in the biology department, analysing data and telling people that they should have used more replicates. Now a fully-fledged crew member at Pimoroni - head of digital content - working on learning materials and digital chunterings. Find him on Twitter and most everywhere else, as sandyjmacdonald.