0% found this document useful (0 votes)
14 views17 pages

Sense HAT Music Player

The document outlines a project to create an MP3 player using a Raspberry Pi and Sense HAT, allowing users to play music, change tracks, and control volume with a joystick. It includes step-by-step instructions for programming in Scratch, integrating sound files, and displaying track information on an LED matrix. Additionally, it suggests a challenge to implement a shuffle feature using the Sense HAT's humidity sensor.

Uploaded by

kavyaddn2009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views17 pages

Sense HAT Music Player

The document outlines a project to create an MP3 player using a Raspberry Pi and Sense HAT, allowing users to play music, change tracks, and control volume with a joystick. It includes step-by-step instructions for programming in Scratch, integrating sound files, and displaying track information on an LED matrix. Additionally, it suggests a challenge to implement a shuffle feature using the Sense HAT's humidity sensor.

Uploaded by

kavyaddn2009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Sense HAT music player https://projects.raspberrypi.org/en/projects/sensehat-scratch-mp3-player...

Projects

Sense HAT music player


Build a sparkling MP3 player with Scratch and the
Sense HAT

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.

What you will make

What you will learn

This project covers elements from the following strands of the Raspberry Pi Digital Making Curriculum (http://rp
f.io/curriculum):

• Use basic programming constructs to create simple programs (https://curriculum.raspberrypi.org/progr


amming/creator/)
• Use basic digital, analogue, and electromechanical components (https://curriculum.raspberrypi.org/phy
sical-computing/creator/)

What you will need

• A Raspberry Pi (not a Zero/Zero W)


• A Sense HAT

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...

Additional information for educators

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).

You can �nd the completed project here (http://rpf.io/p/en/sensehat-scratch-mp3-player-get).

2 of 17 8/30/2023, 1:19 PM
Sense HAT music player https://projects.raspberrypi.org/en/projects/sensehat-scratch-mp3-player...

Step 2 Making it sparkle

First, you will get the Sense HAT to show a random sparkly display to accompany your favorite music.

Open Scratch 3 from the application menu.

Opening Scratch 3 on the Raspberry Pi

To open up Scratch 3 on the Raspberry Pi:

• Click on the menu icon in the top left-hand corner of the screen.

• Hover your mouse pointer over Programming.

• Select Scratch 3 from the menu that appears.

• Your application should open in a few seconds.

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.

Add a sprite from the Sprite Library

Click on Choose a Sprite to open the Sprite Library:

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...

Add the Pi SenseHAT extension in Scratch.

How to add the Sense HAT extension

• Click on the Add Extension button in the bottom left-hand corner of the screen

• Choose the Raspberry Pi Sense HAT extension from the list

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...

Find the set pixel block in the Sense HAT extension.


Try adding numbers into the x andy �elds, and changing the colour in the to �eld.

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.

set pixel x pick random 0 to 7 y pick random 0 to 7 to pick 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

set pixel x pick random 0 to 7 y pick random 0 to 7 to pick random

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...

Step 3 Playing some tunes

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.

Click on the Sounds tab for your sprite.

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...

You should now see your tracks uploaded in Scratch.

— /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

start sound track_01

forever

set pixel x pick random 0 to 7 y pick random 0 to 7 to pick random

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...

Step 4 Displaying the track number

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...

Try the following:

�. Create a new variable called track and set it to 1.

Add a variable in Scratch

• Click on Variables in the Code tab, then click on Make a Variable.

• 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

display text join track track

start sound track

forever

set pixel x pick random 0 to 7 y pick random 0 to 7 to pick random

12 of 17 8/30/2023, 1:19 PM
Sense HAT music player https://projects.raspberrypi.org/en/projects/sensehat-scratch-mp3-player...

Step 5 Changing the volume

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

display text join track track

set volume to 100 %

start sound track

forever

set pixel x pick random 0 to 7 y pick random 0 to 7 to pick random

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.

when joystick pushed up

change volume by 10

when joystick pushed down

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...

Step 6 Changing the track

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.

when joystick pushed right

stop all sounds

change track by 1

display text join track track

start sound track

when joystick pushed left

stop all sounds

change track by -1

display text join track track

start sound track

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...

Step 7 Challenge: shu�e the tracks

• 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?

Published by Raspberry Pi Foundation (https://www.raspberrypi.org) under a Creative Commons license (ht


tps://creativecommons.org/licenses/by-sa/4.0/).
View project & license on GitHub (https://github.com/RaspberryPiLearning/sensehat-scratch-mp3-playe
r)

17 of 17 8/30/2023, 1:19 PM

You might also like