Making Music in Scratch
A step-by-step primary lesson on the Music blocks in Scratch: add the Music extension, play notes and drums, set the tempo, and build a short tune with a loop. With a worked project and quiz.
Key takeaways
- The Music extension adds blocks to play notes, drums and rests
- 'play note for beats' plays one musical note; higher numbers are higher notes
- 'play drum for beats' makes a percussion sound like a snare or cymbal
- Tempo sets how fast or slow the beats play, and loops repeat a tune
Making music with code
Did you know you can write code that plays a tune? In Scratch, the Music blocks turn your computer into a little band. You tell it which note to play, which drum to hit, and how fast β and it makes real music. By the end of this lesson you will have coded a short song with notes and a beat.
If Scratch is new to you, start with getting started with Scratch and come back here. Music is also a great place to practise loops, so loops and repeats is a helpful companion lesson.
Step 1: Turn on the Music blocks
Like the Pen, the Music blocks are an extension you switch on.
- Click the Add Extension button in the bottom-left corner of the Scratch screen.
- Choose Music from the list.
A new pink Music category appears in your blocks palette. The blocks we will use are:
- play note ( ) for ( ) beats β plays one musical note for a length of time.
- play drum ( ) for ( ) beats β plays a percussion sound, like a snare or cymbal.
- rest for ( ) beats β a silence, a musical "gap" with no sound.
- set instrument to ( ) β changes the sound, from piano to guitar to many others.
- set tempo to ( ) β sets how fast the music plays, in beats per minute.
Step 2: Play your first note
Click a sprite and build this tiny script:
when green flag clicked
play note (60) for (0.5) beats
Click the green flag. You hear one note! Two numbers control it:
- The note number (60) is the pitch β how high or low it sounds. A bigger number is a higher note; a smaller number is a lower note. Note 60 is "middle C" on a piano.
- The beats (0.5) is how long the note lasts. A bigger number holds the note longer.
Click the note number and a little keyboard pops up so you can pick notes just by tapping keys. Try note 67 β it sounds higher.
Step 3: Play several notes in a row
Music is notes one after another. Sequencing is just putting steps in order β see sequences: putting steps in order if you want a refresher. Stack a few play note blocks:
when green flag clicked
play note (60) for (0.5) beats
play note (62) for (0.5) beats
play note (64) for (0.5) beats
play note (65) for (0.5) beats
play note (67) for (0.5) beats
Click the green flag. The notes play one after another, climbing higher β that is the start of a musical scale. Scratch always runs the blocks top to bottom, so the order you stack them is the order they play.
Step 4: Add a beat with drums
A tune feels alive with a drum beat. The play drum block makes percussion sounds. Click the drum number to choose one, such as (1) Snare Drum or (2) Bass Drum.
when green flag clicked
repeat 4
play drum (1) for (0.25) beats
play drum (2) for (0.25) beats
This repeat 4 loop plays a snare, then a bass drum, four times β a simple "tss-dum, tss-dum" beat. Loops save you from dragging out the same blocks over and over.
Step 5: Control the speed with tempo
Tempo is the speed of the music, measured in beats per minute. Set it once near the top of your script:
when green flag clicked
set tempo to 100
play note (60) for (0.5) beats
play note (62) for (0.5) beats
- A small tempo, like
set tempo to 60, plays slowly. - A big tempo, like
set tempo to 180, plays fast.
Try the same notes at tempo 60 and then tempo 160 to hear the difference.
A worked project: code a short tune
Let's put it all together and code the first line of "Twinkle, Twinkle, Little Star". The notes are: C C G G A A G. In Scratch note numbers, that is 60, 60, 67, 67, 69, 69, 67.
Add the Music extension, then build this on a sprite:
when green flag clicked
set instrument to (1)
set tempo to 120
play note (60) for (0.5) beats
play note (60) for (0.5) beats
play note (67) for (0.5) beats
play note (67) for (0.5) beats
play note (69) for (0.5) beats
play note (69) for (0.5) beats
play note (67) for (1) beats
Here is how it works, step by step:
- set instrument to (1) picks the Piano sound.
- set tempo to 120 sets a steady, medium speed.
- The seven play note blocks play in order, top to bottom, giving the familiar "Twin-kle twin-kle lit-tle star" melody.
- The last note lasts 1 beat instead of 0.5, so the tune ends on a longer "star".
Click the green flag and listen β you have coded a real song!
Try it: make it your own
You can play notes, drums and set the speed. Now compose your own music with these challenges:
- Finish the song: the next line of Twinkle is 65, 65, 64, 64, 62, 62, 60. Add those notes after yours.
- Add a beat under the tune: make a second script on a different sprite with a
foreverloop of drums, so a beat plays while the melody plays. - Change the instrument: try
set instrument to (4)for an electric guitar or other sounds, and hear how the tune changes. - Make it interactive: use
when [space] key pressedto play a note, so the keyboard becomes an instrument you play live.
If a note sounds wrong, check its number β small changes in the note number change the pitch a lot. Testing and fixing like this is called debugging, and every coder does it. Have fun making music! π΅
Quick quiz
Test yourself and earn XP
Where do you find the Music blocks in Scratch?
The Music blocks are an extension. Click 'Add Extension' at the bottom-left and choose 'Music' to add the pink Music category.
In 'play note (60) for (0.5) beats', what does the 60 control?
The first number is the note number (pitch). A bigger number plays a higher note; the beats number controls how long it lasts.
What does 'set tempo to 60' do?
Tempo is the speed of the music in beats per minute. A smaller tempo is slower; a bigger tempo is faster.
Which block plays a drum sound instead of a note?
'play drum for beats' triggers a percussion sound such as a snare drum or cymbal, chosen from its dropdown.
Why might you wrap a tune in a 'repeat' loop?
A repeat loop plays the same notes again and again, so you can loop a tune without dragging the blocks out many times.
FAQ
First make sure your device volume is up and not muted. In Scratch, check that you added the Music extension and used 'play note' or 'play drum' blocks, not the empty 'rest' block. Also try clicking the green flag again, as some browsers need a click before they allow sound.
The Sound blocks (purple) play recorded sound files like 'Meow' or a song clip. The Music extension blocks (pink) play musical notes and drums you choose with numbers, so you can compose your own tunes.
Keep exploring
More in Coding