LEGO MINDSTORMS EV3 - Sound Block - Music Header Small

How to Make Lego MINDSTORM EV3 Play Music – The Sound Block

In Lego Mindstorms by Glenn Turnbull1 Comment

Welcome to our post about playing music on the Lego MINDSTORMS EV3 Brick. We will cover everything you need to know from the basics to the more advanced techniques. We’ll also include step by step examples along the way to help you better understand how playing music on the EV3 Brick works.

The Sound Block

The green sound block can be used to to play music or a sound on the Lego MINDSTORMS EV3 brick.

Green Sound Block

When it comes to playing music there are three different modes we can use to do this:

  • Play Note: this mode allows us to play a musical note and select it from a piano style keyboard
  • Play Tone: this mode will play a tone at a specified frequency
  • Play File: we can import a wav or mp3 file to play using this mode.

Playing Music on a EV3 Brick using Play Note Mode

Playing music on a Lego MINDSTORMS EV3 Brick using the sound block’s play note mode is quite simple if you can read music or have experience playing a piano or keyboard. Don’t worry if you don’t have this experience we will step you through the process below and hopefully by the end of this post you will be an expert composer using EV3 Music.

There are four inputs for the sound block’s Play Note mode:

  • Note: The musical note to be played
  • Duration: the length of time the note should be played, this input accepts decimals
  • Volume: how loud the note should be played
  • Play Type: used to tell the program to either wait for the last note to finish, repeat the last note or play the note once

For the example play note mode program lets play Jingle Bells on the EV3 Brick. Even though this program will contain a lot of programming blocks, it is quite straightforward as we will have a green sound block to represent each note. For you reference I’ve linked the sheet music for Jingle Bells here.

How to Play Jingle Bells on EV3 Brick

One thing to keep in mind is the note length, for a whole note I tend to set the duration input to 1, for a half note (or minim) set it to 0.5 and for an eighth note 0.3. That seems to give an Ok representation of this song – feel free to play around with this to get it to your liking. Here’s a good explanation of these notes and what they look like. I’ve noted the duration changes within the instructions below.

To program Jingle Bells into the Lego MINDSTORM EV3 brick:

  1. Drag and drop a green Sound block next to the start button
    • Set its Mode to Play Note
    • Set Duration input to 0.5
    • Set Volume to 5
    • Set Play type to Wait for Completion
    • Set Note to E5
LEGO MINDSTORMS EV3 - Sound Block - Play Jingle Bells - Step 1
  1. Copy and paste the previous Sound block to create the next note (copying and pasting the block will save time, rather than using a new block and setting up the volume and note length again)
    • Select the previous block
    • Copy the block by either pressing CTRL + C or selecting Edit | Copy
    • Paste the block by pressing CTRL + V or selecting Edit | Paste
    • Drag the block to connect it to the previous block
    • The next note is E5, so no need to change anything
LEGO MINDSTORMS EV3 - Sound Block - Play Jingle Bells - Step 2
  1. Continue the copy and paste process for the following notes:
    • E5
    • E5
    • E5
    • E5
    • E5
    • G5
    • C5
    • D5

That takes care of the first line of the music and as you can see there’s a lot of blocks and scrolling already. One tip we have for readability of the program is to start the next line under the Start button. That way if there is a bug in the program it’s easier to go back and find the position of the incorrect note.

  1. Start a new line and continue the song:
    • Copy and paste one of the E5 notes and position it under the last programming block (D5).
      • Since it’s a whole note, set its duration input to 1
    • Drag the connector from the last block into the left side of the new E5 block
    • Move the E5 block so it’s positioned under the Start block
LEGO MINDSTORMS EV3 - Sound Block - Play Jingle Bells - Step 3
LEGO MINDSTORMS EV3 - Sound Block - Play Jingle Bells - Step 3.2
  1. Continue adding the next line of sheet music:
    • Copy and paste the following notes:
      • F5
        • Set it’s duration to 0.5
      • F5
      • F5
      • F5
      • F5
      • E5
      • E5
      • E5
        • Set it’s duration to 0.3
      • E5
        • Set it’s duration to 0.3
  1. Create a new line within the program (as per step 4) and start it with an E5 again and set it’s duration to 0.5.
    • Continue adding the following notes:
      • D5
      • D5
      • E5
      • D5
        • Set its duration to 1
      • G5
        • Set its duration to 1
      • E5
        • Set its duration to 0.5
      • E5
      • E5
        • Set its duration to 1
      • E5
        • Set its duration to 0.5
      • E5
      • E5
      • E5
        • Set its duration to 1
  1. Create a new line within the program (as per step 4) and start it with an E5 again and set its duration to 0.5.
    • Continue adding the following notes:
      • G5
      • C5
      • D5
      • E5
        • Set its duration to 1
      • F5
        • Set its duration to 0.5
      • F5
      • F5
      • F5
  1. Create a new line within the program (as per step 4) and start it with an F5.
    • Continue adding the following notes:
      • E5
      • E5
      • E5
      • E5
      • G5
      • G5
      • F5
      • D5
      • C5
        • Set its duration to 1

Congratulations, you have now completed Jingle Bells on the Lego MINDSTORMS EV3. Hit the download and play button to hear your work.

Click here to download the Jingle Bells project.

Playing the C Major Scale on a EV3 Brick using Play Tone Mode

The Lego MINDSTORM EV3 brick Sound block’s play tone mode uses a frequency value to play a sound. This provides some cool options as we can use maths within our program to play music. Each note on a musical scale is a frequency and thankfully Lego have included the note name (and frequency value) within a drop down associated with the Hz input.

LEGO MINDSTORMS EV3 - Sound Block - Play Tone

A complete list of musical notes and their frequency can be found here on Wikipedia.

For this example we will play the C major scale by calculating the frequency of the next note in the scale using the following formula:

fn = f0 * (1.059463)n

  • fn = the next note (frequency)
  • f0 = the current note (frequency)
  • n = the number of half steps required to the next note

This formula is from the Physics of Music page on the MTU website, click here for more information.

Create a My Block to Calculate the Next Note

The following program uses a My Block (a custom block) to calculate the next note in the scale. If you have not used a My Block, please see our post here which goes over creating a My Block in more detail.

The steps below describe how we will recreate the formula above within a reusable My Block to help make the program a lot simpler and easier to read.

  1. Open a new program
  2. Calculate the (1.059463)n part of the formula:
    • Drag and drop a red Math block next the to the start button
    • Set its mode to Exponent
    • Set the a input to 1.059463
    • We’ll wire the n input in once we set up the My Block, as this is something we will pass into the equation
LEGO MINDSTORMS EV3 - Sound Block - Play Tone - C Maj Scale 1.1
  1. Complete the formula
    • Drag and drop another red Math block next to the one above
    • Set its mode to Multiply
    • Wire the Result from the first math block into the a input of the second one
    • We’ll wire in the b input when we create the My Block in the next step
  1. Create the My Block
    • Select both the math blocks and click on the following menu options: Tools | My Block Builder
    • Enter the following:
      • Name: CalcNextNote
      • Description: Calculate the next note based on the passed frequency and number of half steps
      • Select an icon of your choice (I used the speaker)
    • Add a parameter for the number of half steps:
      • Click the + button on the top picture
      • Select the Parameter Setup tab and enter the following:
        • Name: n
        • Leave the defaults
LEGO MINDSTORMS EV3 - Sound Block - Play Tone - C Maj Scale 1.3
  • Add another parameter by clicking the + button on the top icon
    • Select the Parameter Setup tab and enter the following:
      • Name: Frequency
      • Leave the defaults
      • Click the Parameter Icons tab and select b
  • Add one more parameter for the output of the formula by clicking the + button on the top icon
    • Select the Parameter Setup tab and enter the following:
      • Name: NextFrequency
      • Parameter Type: Output
      • Data Type: Number
      • Click the Parameter Icons tab and select “=” sign
LEGO MINDSTORMS EV3 - Sound Block - Play Tone - C Maj Scale 1.4

  1. Click Finish button on the bottom of the window
  2. Wire the inputs into the formula:
    • Wire a My Block input into the n input of the first block
    • Wire b My Block input into the b input of the second math block
    • Wire the result output of the second math block into the = output
  1. Select File | Save and close the CalcNextNote tab

Write the Program to Play C Major Scale

Now we have the My Block to take care of the next note calculation, lets write a program to run through the C Major scale:

  1. Create a new program within the same project the My Block above was created.
  2. Create a variable to hold the current note’s frequency value
    • Drag and drop a red Variable block next to the start block
    • Set its mode to Write | Numeric
    • Click Add Variable and name the variable CurrentNote
    • Set its value to 261.626 (middle C i.e. C4) as per the this frequency table on Wikipedia.
LEGO MINDSTORMS EV3 - Sound Block - Play Tone - C Maj Scale 1.6
  1. Read and play the note
    • Read the note:
      • Drag and drop a red Variable block next to step 2’s block
      • Set its mode to Read | Numeric
      • Select CurrentNote variable name
    • Play the note
      • Drag and drop a green Sound block next
      • Set its mode to Play Tone
      • Wire the previous read variable block into the Hz input
LEGO MINDSTORMS EV3 - Sound Block - Play Tone - C Maj Scale 1.7
  1. Calculate the next note within the scale using our maths formula above (and our My Block):
    • From the teal palette, drag an drop the CalcNextNote My Block next in the program
    • The next note is 2 half steps away (i.e. C# + D). Set the a input (number of steps) to 2.
    • Wire the read CurrentNote into the b input
LEGO MINDSTORMS EV3 - Sound Block - Play Tone - C Maj Scale 1.8
  1. Save and play the next note (D):
    • Save the note:
      • Drag and drop a red Variable block after the My Block
      • Set its mode to Write | Numeric
      • Select the variable name CurrentNote
      • Wire the output (=) from the My Block into the variable block
    • Play the note:
      • Read the next note:
        • Drag and drop a red Variable block next
        • Set its mode to Read | Numeric
        • Select the variable name CurrentNote
      • Play the note:
        • Drag and drop a green Sound block
        • Set its mode to Play Tone
        • Wire the read variable block into the Hz input
LEGO MINDSTORMS EV3 - Sound Block - Play Tone - C Maj Scale 1.9
  1. Play the next note in the C major scale (D):
    • Repeat steps 3, 4 and 5
    • Set the number of steps in the My Block to 2
LEGO MINDSTORMS EV3 - Sound Block - Play Tone - C Maj Scale 1.10
Note: setting the duration on the sound blocks to 0.5 will play the scale faster
  1. Play the next note: E
    • Repeat steps 3, 4 and 5
    • Set the number of steps in the My Block to 1
  2. Play the next note: F
    • Repeat steps 3, 4 and 5
    • Set the number of steps in the My Block to 2
  3. Play the next note: G
    • Repeat steps 3, 4 and 5
    • Set the number of steps in the My Block to 2
  4. Play the next note: C5
    • Repeat steps 3, 4 and 5
    • Set the number of steps in the My Block to 1

If you would like the program to download, please email me at [email protected] and I’m happy to send it on.

Sound Block’s Play File Mode – Recording your own Sounds and Importing Music

The Lego MINDSTORM programming software allows us to record our own sound (or music) and also import a music file. When it comes to music files it supports mp3 and wav formats.

How to Record and Playback Sounds in Lego MINDSTORMS EV3

Within this section we will take you through how to record your own sounds (or music) within the Lego Mindstorms EV3 programming software.

Before recording, ensure you have a microphone plugged into the computer.

  1. Open a new program
  2. Select Tools | Sound Editor from the menu
  3. To start recording, click the red record button on the Sound Editor window
LEGO MINDSTORMS EV3 - Sound Block - Record Step 1.1
  1. Click the Stop button when you have finished.
  2. Click Save
  3. Enter a name for the recording and click Ok
LEGO MINDSTORMS EV3 - Sound Block - Record Step 1.2
  1. Click Close to exit the Sound Editor window.

To use the sound recording:

  1. Drag and drop a green Sound block into the program
    • Set its mode to Play File
    • Click the file selector (top right on the block) and select the sound from the Project Sounds folder
LEGO MINDSTORMS EV3 - Sound Block - Record Step 1.3

How to Import Music Files into Lego MINDSTORMS EV3 Programs

In the example below we will import a music file into a program and play it from the EV3 Brick.

Note: The mp3 or wav file needs to be under 8mb, I’ve found anything larger than this will fail stating that the file is too large.

  1. Open a new program within the Lego MINDSTORMS EV3 Programming software
  2. Select Tools | Sound Editor from the menu
  3. Click the Open button on the Sound Editor window
LEGO MINDSTORMS EV3 - Sound Block - Play File 1.1
  1. From the file dialog box, navigate to and select the file you wish to import and click Open.
  2. Once the file has been loaded within the Sound Editor window, click Close.
  3. When prompted click Save to import the file into the project.
LEGO MINDSTORMS EV3 - Sound Block - Play File 1.2
  1. Enter a name for the file and click Ok (I’ll call mine Test MP3 in the example below)
LEGO MINDSTORMS EV3 - Sound Block - Play File 1.3
  1. Drag and drop a green Sound block to your program
    • Set its mode to Play File
    • Click the top right hand corner of the block and from the Project Sounds folder select the sound that was imported.
LEGO MINDSTORMS EV3 - Sound Block - Play File 1.4

How to Remove MP3 / Wav Files from a Lego MINDSTORMS EV3 Project

If there’s a sound or music file that’s been imported into a project that you no longer wish to use then this can be removed by following the steps below:

  1. Navigate to the Project Properties tab:
    • Click the Spanner tab in the top left hand side of the project.
LEGO MINDSTORMS EV3 - Sound Block - Delete File 1.1
  1. Select the Sounds tab
  2. Select the sound file you wish to remove and click Delete
LEGO MINDSTORMS EV3 - Sound Block - Delete File 1.2

Comments

Leave a Comment