Play Tunes and a Piano with a BBC micro:bit

This example shows you how to use Simulink® models to play musical tunes and a piano with BBC micro:bit.

Introduction

The Simulink® Coder Support Package for BBC micro:bit provides Audio blocks that help you to generate musical notes and play them.

To view the Audio blocks:

1. Enter slLibraryBrowser at the MATLAB® prompt.

2. In the Simulink Library Browser, navigate to Simulink Coder Support Package for BBC Micro:bit and double-click Audio blocks.

You can see the Note Generator block and Play Note block.

In this example, you will learn about two Simulink models that are designed using the Audio blocks:

  • The Play Tune Simulink model contains all the notes of "Happy Birthday", and they are programmed through a logic to play the song.

  • The Piano Simulink model maps the pins on the BBC micro:bit to particular notes, and the note is played if you short the pin to the GND (ground) pin.

In both the models, the audio is generated on a specified pin of the BBC micro:bit board using a variable frequency PWM, and the audio can be heard by connecting a small speaker or a headphone to that pin.

Prerequisites

Required Hardware

To run this example, you need the following hardware:

  • BBC micro:bit board

  • USB type A to Micro-B cable

  • A small speaker or a headphone. Connect the speaker or headphone to pin P2 on the BBC micro:bit.

Tip: Connect GND pin to the tip part of the headphone jack and connect P2 to the next part below on the headphone jack, using crocodile clips and wires.

  • (Optional) Edge Connector Breakout Board for BBC micro:bit

Task 1 - Play the Happy Birthday Tune and Understand How the Simulink Model Works

1. Open the Play Tunes model.

2. In the Hardware tab of the Simulink model window, click Build, Deploy & Start.

After the code deployment is successful, the Happy Birthday tune starts playing on the speakers.

Simulink Model - Play Tune

In the Play Tune Simulink model, Note Selector is a stateflow, which has nine inputs and one output:

  • Each of the eight Note Generator input blocks define a Note and its Scale (Scale can take a value of Low, Mid, or High, placing the note in one of three available octaves). For example, the Note Generator4 block defines the Note E and its Scale Mid. Double-click each block to view the Note and its Scale.

  • The One Beat Duration input block is a constant that is used to specify the duration of one beat. This constant value gets multiplied by the number of beats for each note.

  • The Play Note output block plays the output note from the stateflow on the speakers connected to pin P2.

To understand the logic, open the Note Selector stateflow diagram in the Simulink model.

In the first state (Set_Notes_and_Beats), all the notes in the song are entered in an array. The notes are entered along with the beat duration of each note.

For example, if the notes A, B, and C are to be played for 1, 0.5, and 2 beats respectively, enter them into the array as:

Notes = [A 1 B 0.5 C 2]

In Play Tune model, the beat duration is also multiplied by a constant using the One Beat Duration block.

Once the array is entered, the next state, Play_Note, plays a single note. For the example array mentioned above, the note A is sent out of the stateflow first. This note is played for 1 beat, and a counter is incremented. The next note, B, goes is played for 0.5 beats.

Repeating a Note

In the Happy Birthday tune, the same note is sometimes played repeatedly. When two consecutive notes are the same in an array, the Play Tune model's default behavior is to combine the two notes and add their durations together. Instead of generating two notes, one single note is generated and played for the entire duration.

To avoid this behavior in the Play Tunes model, a small pause is added after the first note. During this pause, a low inaudible frequency is played. This creates a small gap and allows two notes to be heard instead of one. This is acheived by using the Sleep state as shown in the model.

The One Beat Duration block is used to specify the beat duration and the sample time. In this model, the sample time is set to 0.001 s or 1 ms. This facilitates the small pause (0.005 sec) in the stateflow chart that is required when two notes of the same pitch are to be played consecutively. If the sample time is less, the pause cannot be set to a small value.

Playing the Audio

The Play Note block is used to play audio. This block interprets the input given to it as frequency. For example, to generate a 500Hz audio, connect a constant block with value 500 to the Play Note block.

The audio is played on the pin specified in the Play Note block.

Task 2 - Play a Piano and Understand How the Simulink Model Works

1. Open the Piano model.

2. In the Hardware tab of the Simulink model window, click Build, Deploy & Start.

After the code deployment is successful, you can start playing a simulated piano. The pins on the BBC micro:bit are the keys of the piano. When a pin is shorted to the GND (ground) pin, a single note is played.

You can use the Edge Connector Breakout Board for BBC micro:bit to short the pins effectively.

Simulink Model - Piano

The Sum block in the Piano Simulink model uses the following blocks to transfer the musical notes to the Play Note output block:

  • Seven Note Generator blocks that correspond to the seven notes on a major scale: C D E F G A B. The Note Generator generates the frequency value of the specified note. For example, A(Mid) generates 440Hz.

  • Seven Digital Read blocks that reads the logical state of the seven pins that are used as keys.

  • Seven Switch blocks that pass the input note to the Sum block only when the pin is shorted to GND. For example, when the pin P3 is shorted to GND pin, the note C is played.

Other Things to Try with Audio Blocks

  • Change the notes in the Play Tunes model to play a different song.

  • Change the tempo of the song in the Play Tunes model by changing the Constant value in the One Beat Duration block.

Copyright 2017 The MathWorks, Inc.