Mission: Create a series of circuits that will send a signal to the Raspberry Pi when a button is pressed. On detecting a completed circuit, the Raspberry Pi will play sounds from a drumkit program, resulting in a playable electronic drumkit!
- Build three live switch circuits
- Connect the circuits to the Raspberry Pi
- Setup Scratch 2 so that it can use the Raspberry Pi’s GPIO output
- Select the sounds that will be used by the drumkit program
- Build a program that can trigger the bass drum
- Build and test the rest of the drumkit
- Improve the playability of the drumkit
What you will need
- 5x Male to Female Jumper Cables (Red, Black, Blue, White, Green)
- 3x Male to Male Jumper Cables (Orange, Red, Yellow)
- 3x 330 Ohm (330R) Resistors
- 3x Switches
Part 1: Build three live switch circuits
1. Place the first switch so that two pins are in Column 3 & Column 5 of Row G, and the other two pins are in Row F of the same columns (The switch should sit over the groove in the middle of the breadboard
2. Add a second switch, with the two sets of pins placed in Columns 19 & 21
3. Add the final switch so the pins sit in Columns 25 & 27
4. Place the first 330 Ohm (330R) resistor between Column 3, Row L & the negative power rail
5. Place the second 330 Ohm (330R) resistor between Column 19, Row L & the negative power rail
6. Place the final 330 Ohm (330R) resistor between Column 25, Row L & the negative power rail
7. Using the orange male to male jumper cable, connect Column 5, Row L to the positive power rail
8. Connect Column 21, Row L to the positive power rail, using the red male to male jumper cable
9. Connect Column 27, Row L to the positive power rail, using the yellow male to male jumper cable
10. Insert the male end of the green male to female power cable, into Column 3, Row K
11. Insert the male end of the white male to female power cable, into Column 19, Row K
12. Insert the male end of the blue male to female power cable, into Column 25, Row K
13. Place the male end of the red male to female jumper cable, into the positive power rail
14. Connect the male end of the black male to female jumper cable, into the negative power rail
You have just built a series of circuits that, when completed, will send an electrical signal to the Raspberry Pi’s GPIO pins.
Part 2: Connect the circuits to the Raspberry Pi
1. Connect the female end of the Red jumper cable, to the first pin (3.3V), on the Raspberry Pi
2. Connect the female end of the Black jumper cable, to the 14th pin (Ground) on the Raspberry Pi
3. Connect the female end of the Green jumper cable, to GPIO 16 on the Raspberry Pi
4. Connect the female end of the White jumper cable, to GPIO 20 on the Raspberry Pi
5. Connect the female end of the Blue jumper cable, to GPIO 21 on the Raspberry Pi
Part 3: Setup Scratch 2 so that it can use the Raspberry Pi’s GPIO output
As we’re building our program in Scratch 2, we need to configure the program so that we can utilise the Raspberry Pi’s GPIO pins.
1. Open Scratch 2 by clicking on the Raspberry Pi Start Menu, navigating to "Programming" and then selecting Scratch 2
2. Select "More Blocks" from the "Scripts" tab and click on "Add an Extension"
3. You will be presented with two options. Double-click on "Pi GPIO" to install the extension
4. Now that you have the GPIO extension installed, you will gain access to the GPIO control blocks and can start constructing the program
Part 4: Select the sounds that will be used by the drumkit program
Before we program our drum controller, we need to choose three sound samples.
1. Click on the "Sounds" tab in Scratch
2. Remove the "Meow" sound from your new program (unless you want to use it later)
3. Open the sound library by clicking the speaker icon
4. To make it easier to find the sounds we want, select the "Percussion" category in the sound library
5. Select the Bass Drum and click "Ok" to confirm the selection
6. You should now see that the bass drum has appeared in the Sounds tab
7. Repeat the process two more times, selecting both the high hat and the snare drum sounds
Part 5: Build a program that can trigger the bass drum
We have built a controller, connected it to the Raspberry Pi and chosen the sounds we want to control. Now we must produce a program, where pushing a button will send a signal to the Raspberry Pi and in response, the Raspberry Pi will play a chosen sound.
1. From the "Events" options, drag a "when Green Flag clicked" tile into the canvas
2. Drag a "set gpio… to…" tile into the canvas, attaching it to the bottom of the green flag tile
3. Open the gpio list, and select gpio 21 (aka - the left button on our controller)
4. Unlike our LED project, where we output power to the LED, this project is going to detect power to function. Open the "output high" dropdown and select the "input" option
5. We want the bass drum button to work whenever we press it, so drag a "forever" loop from the "Control" section and attach it under the "set gpio..." tile
6. Add an "if <> then" loop into the "forever" loop you just added
7. Add a "gpio… is high?" tile from the "More Blocks" section, into the diamond box of the "if <> then" loop
8. Select gpio 21 from the "gpio.. is high?" dropdown
9. Drag a "play sound…" tile from the "Sound" section, and drop it into the "if gpio 21 is high? Then" loop
10. Select the bass drum from the dropdown list in the "play sound…" tile
11. Run the program by clicking on the green flag. You should be able to get a bass drum sound when you push the left button on your breadboard!
Part 6: Build and test the rest of the drumkit
Now that you’ve successfully built a bass drum controller, let’s add a high hat and snare drum, so you can produce a basic 4/4 drum beat!
1. Repeat the same process as you did for the bass drum program, creating two more separate commands for controlling the high hat and snare drum
2. Try playing a basic beat on your new drumkit!
- Count out loud ONE, TWO, THREE, FOUR
- Play the kick drum when you say ONE (left button)
- Play the snare drum when you say THREE (middle button)
- Play the high hat on every TWO and FOUR (right button)
Part 7: Improve the playability of the drumkit
You may notice that it is hard to control the kit, as each sound repeats itself at a fast rate. To fix this issue, we must specify a tempo and rest period between the triggering of each sound.
1. Separate your bass drum "forever" loop from the "set gpio 21 to input" tile
2. Add a "set tempo to… bpm" tile under the "set gpio 21 to input" tile and connect the forever loop to the bottom of the "set tempo to… bpm" tile
3. Repeat the process until all three parts of the drumkit program include a "set bpm" tile
Now that we have a fixed tempo for each part of the kit, we need to add a rest between each drum trigger.
4. Drop a "rest for… beats" tile, under each of the "play sound…" tiles in the "if <> then" loops.
5. Now that each part of the kit has a quarter-beat rest between each trigger, you should find it easier to play the beat from earlier
Enjoy your new drumkit!
- Don’t be afraid to experiment with new sounds
- Find out the bpm of your favourite tracks… Maybe you could play along?
- Add more buttons by using other GPIO pins on the Raspberry Pi!