Sense HAT Music Player
Sense HAT Music Player
Projects
Step 1 Introduction
In this project, you will make your very own MP3 player using a Raspberry Pi and a Sense HAT. You’ll be able to shift
between tracks in your playlist, change the volume, and show a cool disco display on the LED matrix.
This project covers elements from the following strands of the Raspberry Pi Digital Making Curriculum (http://rp
f.io/curriculum):
Optional:
• Speakers or headphones
1 of 17 8/30/2023, 1:19 PM
Sense HAT music player https://projects.raspberrypi.org/en/projects/sensehat-scratch-mp3-player...
If you need to print this project, please use the printer-friendly version (https://projects.raspberrypi.org/en/pr
ojects/sensehat-scratch-mp3-player/print).
2 of 17 8/30/2023, 1:19 PM
Sense HAT music player https://projects.raspberrypi.org/en/projects/sensehat-scratch-mp3-player...
First, you will get the Sense HAT to show a random sparkly display to accompany your favorite music.
• Click on the menu icon in the top left-hand corner of the screen.
3 of 17 8/30/2023, 1:19 PM
Sense HAT music player https://projects.raspberrypi.org/en/projects/sensehat-scratch-mp3-player...
Choose a sprite for your MP3 player — in this example, the sprite is a speaker.
You can search for a sprite, or browse for one by category. Click on a sprite to add it to your project.
You can delete any scripts or sounds that are already on your sprite.
4 of 17 8/30/2023, 1:19 PM
Sense HAT music player https://projects.raspberrypi.org/en/projects/sensehat-scratch-mp3-player...
• Click on the Add Extension button in the bottom left-hand corner of the screen
Now you’re all set up, you can begin to make your MP3 player by creating a disco e�ect on the display.
Begin by adding a when �ag clicked block, and then add a forever block below it.
when clicked
forever
5 of 17 8/30/2023, 1:19 PM
Sense HAT music player https://projects.raspberrypi.org/en/projects/sensehat-scratch-mp3-player...
Then, click on the block, and you should see the colour of the pixel on the Sense HAT change.
Now you can change the pixel position and colour to be random.
Click on the block again, and you should see random pixels being set to random colours.
Now place the set pixel block into your forever loop.
when clicked
forever
Click on the green �ag to try running your program. You should see the LEDs on the Sense HAT begin to
randomly sparkle.
6 of 17 8/30/2023, 1:19 PM
Sense HAT music player https://projects.raspberrypi.org/en/projects/sensehat-scratch-mp3-player...
Now you have a disco e�ect, it’s time to make your MP3 player play some tunes!
Gather a few MP3 �les together to begin with — you’ll want a minimum of three. Save them in the same
directory as your Scratch code.
Use the Upload sound from �le button to upload all of your MP3 �les.
7 of 17 8/30/2023, 1:19 PM
Sense HAT music player https://projects.raspberrypi.org/en/projects/sensehat-scratch-mp3-player...
— /task —
Now it’s time to play your tracks. You can play a sound using the start sound block. Add this block above
your forever loop.
when clicked
forever
8 of 17 8/30/2023, 1:19 PM
Sense HAT music player https://projects.raspberrypi.org/en/projects/sensehat-scratch-mp3-player...
Now click on the green �ag to play your track as the disco lights start. Make sure you have speakers or
headphones plugged into your Raspberry Pi. You might also need to change the sound output to Analog by
right-clicking on the speaker icon in the header bar of the desktop and then clicking on this option.
9 of 17 8/30/2023, 1:19 PM
Sense HAT music player https://projects.raspberrypi.org/en/projects/sensehat-scratch-mp3-player...
Next, you’re going to display the number of the track that’s currently playing. You can have a go at trying this on your
own — use the hints if you get stuck.
10 of 17 8/30/2023, 1:19 PM
Sense HAT music player https://projects.raspberrypi.org/en/projects/sensehat-scratch-mp3-player...
• Type in the name of your variable. You can choose whether you would like your variable to be
available to all sprites, or to only this sprite. Press OK.
• Once you have created the variable, it will be displayed on the Stage, or you can untick the variable in
the Scripts tab to hide it.
�. Use the display text block to display the number stored in track on the Sense HAT.
�. Play the track with the number stored in your track variable.
11 of 17 8/30/2023, 1:19 PM
Sense HAT music player https://projects.raspberrypi.org/en/projects/sensehat-scratch-mp3-player...
I need a hint
Finish the script by changing the track that is being played to the track variable.
when clicked
set track to 1
forever
12 of 17 8/30/2023, 1:19 PM
Sense HAT music player https://projects.raspberrypi.org/en/projects/sensehat-scratch-mp3-player...
You can now add some extra controls to your MP3 player. First of all, set up your script so that the Sense HAT joystick
can control the volume.
The Raspberry Pi treats the Sense HAT joystick in the same way as the arrow keys on your keyboard. This means that,
for example, pushing the joystick up is the same as pushing the up arrow on your keyboard.
Change the script you already have so that the volume starts at 100%.
when clicked
set track to 1
forever
13 of 17 8/30/2023, 1:19 PM
Sense HAT music player https://projects.raspberrypi.org/en/projects/sensehat-scratch-mp3-player...
Add in two new scripts that use when joystick pushed blocks to change volume.
change volume by 10
Test your scripts again: click the green �ag, and then use the Sense HAT joystick to change the volume of your music.
14 of 17 8/30/2023, 1:19 PM
Sense HAT music player https://projects.raspberrypi.org/en/projects/sensehat-scratch-mp3-player...
Now that you know how to use the joystick, it’s time to try to use it to change which track is playing.
�. When the program starts, you will need to stop all sounds.
�. Use the when joystick pushed left and the when joystick pushed right blocks to stop
all sounds and to increase or decrease the value of your track variable.
�. You can also use a scroll message block to show again which track is being played.
I need a hint
Then you just need to scroll message and play sound for both arrow keys.
change track by 1
change track by -1
15 of 17 8/30/2023, 1:19 PM
Sense HAT music player https://projects.raspberrypi.org/en/projects/sensehat-scratch-mp3-player...
That’s it — your MP3 player is complete! Click on the green �ag and then have a go at changing your tracks using the
Sense HAT joystick.
16 of 17 8/30/2023, 1:19 PM
Sense HAT music player https://projects.raspberrypi.org/en/projects/sensehat-scratch-mp3-player...
• Can you make your MP3 player stop and start again when the joystick is pushed down?
• A nice feature to have on an MP3 player is the option to shu�e the tracks so that they play in a random order.
The Sense HAT can detect changes in the air’s humidity, which means you can trigger events by gently blowing
on it.
Can you change your MP3 player code so that when you blow on the Sense HAT, it detects a change in humidity
and then plays a random track?
17 of 17 8/30/2023, 1:19 PM