PiperCode: Ultrasonic Drum

PiperCode: Ultrasonic Drum


Ultrasonic Drum




With the new Piper Sensor Explorer, there are not only endless possibilities for creation but now we can use data to inform our creations! Today, we’re going to build a beat-making machine using the Piper Ultrasonic Range finder. Once we’re finished, we will have coded a special drum to produce rhythms!



You must have purchased the Piper Sensor Explorer that includes the Piper Ultrasonic Range Finder and 4-Pin Diode Jumper Cable to complete this project. If you have another range finder or cable, this project may work but we can’t make any promises!

To purchase the Piper Sensor Explorer visit our website here.





To complete this project you will need the following components:


  • Mini Breadboard
  • 4-Pin Jumper Cable
  • Piper Ultrasonic Range Finder
  • 3x Jumper wires










  • Did you know that you can detach the Raspberry Pi from the kit to use it remotely? Just unscrew the thumbscrews from the stand-offs. This will allow you better range when collecting data.






Connect your 4-pin jumper cable to the Piper Ultrasonic Range Finder and wire it up to mini-breadboard as shown. Connect a yellow, red and black jumper wire to the wells in the breadboard accordingly to the diagram. Your setup should look like this:





You won’t need the green wire for this project!









Next, connect the 3 jumper wires from the breadboard to your Raspberry Pi. They should look like this:




Use the Raspberry Pi pin map and pin chart below to double-check your work:








Now, let’s start coding our drum! Go to PiperCode and start a new project by clicking on “My Projects”. You can rename your project with whatever name you like. For example: “My mad dope beats”.


First, grab a start block found in the Logic menu, and add a repeat forever loop to it as shown below.



Next, get a wait block from the Chip menu, and place it inside the loop. Set the value to 250 ms. This controls how often our drum beats or the rate of drumming.






Next, we’re going to set our drum sounds. Go to the Variables menu and click “Create variable”. Name the variable ”sound”. Under Variables, take a set to block and use it to set sound to a synth note as shown below. The synth blocks can be found in the Sounds menu. Using the dropdown menu, select the mokugyo-A1 note.



Now, add a play block under Sounds. Have it play sound, which you’ll find under Variables.







Now, we’re ready to test your drum! Press Start button and turn up the volume on your kit. What you’re listening to is your base beat. Use the Speed slider in the bottom left corner to adjust how fast or slow you’d like the beat to play.






Next, we’re going to use our Piper Ultrasonic Range Finder to change this beat! We’ll use the range it detects as an input to produce a different sound as an output. When our hand is close to the sensor, it should play a different sound than when our hand is far away.






  • The Piper Ultrasonic Range Finder uses a wire to transmit a signal to the Raspberry Pi. By “reading the signal pin the sensor is wired up to, we can determine the distance sensed by our range finder.



Under the Logic menu, find the if [] = [], do block and place it into the programming area. We are going to use this to produce a sound according to the range finder’s distance data.

Find the Range Finder (cm) block under Expansions and place it into the left side of the equality comparison (=) block as shown. Change the inequality to greater than or equal to sign.

Finally, take a 0 block from Variables, and set it as the value to be compared against the RangeFinder data.







Next we will finish defining the range used to activate this instrument.

Right click on the greater than or equal to comparison block, and duplicate it. Change its values to check whether Range Finder (cm) is less than 9.

Take both of the comparison blocks we’ve created so far, and join them inside of an and block, found under Logic. Use the gif below to guide you:






  • The Piper Ultrasonic Range Finder creates an analog, or continuous input. It can collect a range of values.
  • In programming, these types of inputs are known as integers. If they contained a decimal value such as 3.14, then they would be floating-point numbers.



If our condition is true and the range finder reads a distance in the range 0-9, as we have coded above, then a different sound should play. To do this, we need to use a set sound to block from Variables, and connect it to a different synth block from Sounds. This time, select the Kick2 note from the dropdown menu.








Now, insert the code block you’ve created between the set and play statements in your main loop.




Your Piper Ultrasonic Range Finder can now play two different rhythmic instruments! Click START to run your program. Place your hand close to the Piper Ultrasonic Range Finder and listen for a change. What do you notice?

Try tweaking the wait value to play a faster or slower beat. You can also add even more instruments to your drum kit by duplicating the action. We will do that below!






  • Through duplicating and modifying code we’ve already made, we can quickly add similar functionalities to our program.
  • Once you have a system of blocks that does an action, you can use this system again to do a similar action. It is like you have created a “super block”. This process is known as abstraction.






Now, let’s add a third beat: Clap2. When the range finder reads a distance greater than or equal to 9, and less than 15, we want Clap2 to play.




Use the GIF below to add the fourth beat: Snare2. This will play when the range finder read a distance between 15 and 25!




Let’s test our Ultrasonic Drum! Move your hard over your Piper Ultrasonic Range Finder or see if you can create a beat using some unique dance moves!

Did you find a rhythm or set of beats you liked? Feel free to share it with us in the comments below or tag us on social media: @StartWithPiper.