Skip to main content

Mission12_Buzzer_Music

In this project, let's play a known song "Twinkle twinkle little star" using a buzzer.

What you need

The parts you will use are all included in the Maker kit.

  • SwiftIO board
  • Shield
  • Buzzer
  • 4-pin cable

Circuit

  1. Plug the shield on top of your SwiftIO board.
  2. Connect the buzzer module to pin PWM2B (D10).
Circuit diagram

Example code

Open the project Mission12_Buzzer_Music in the folder MadExamples/Examples/MakerKit if you downloaded the folder.

import SwiftIO
import MadBoard
import PWMTone

@main
public struct Mission12_Buzzer_Music {
public static func main() {
var halfStep = 0
var bpm = 60
let player = PWMTone(PWMOut(Id.PWM2B), bpm: bpm)

for _ in 0..<3 {
player.play(track: Music.twinkle)

bpm += 40
player.setBPM(bpm)

halfStep += 12
player.setFixedHalfStep(halfStep)

sleep(ms: 1000)
}

while true {
sleep(ms: 1000)
}
}
}

Background

Musical notes

At first, let's learn something about notes.

There are 88 keys in total on a piano keyboard, from A0 to C8. These notes correspond to different frequencies from low to high. And the note do, re, mi, fa, sol, la, ti correspond respectively to C, D, E, F, G, A, B.

Piano keyboard

A half step, or semitone, is the smallest interval between notes. The interval between the key A and key A# is a half step. Two half steps constitute a whole step, like the interval between A and B.

An octave consists of 12 half steps. It is the distance between a note and the next note with the same name, for example, the first key A to the second key A forms an octave.

A quarter note is the common note length in music and has one beat. Then other notes are based on it: a half note has two beats, a whole note has four beats, an eighth note has half beat, etc.

BPM, or beat per minute, is used to measure the tempo of a piece of music. For example, 60 BPM means there would be 60 beats in a minute. Each beat lasts 1 second.

Code analysis

You can find two files in the project:

  • The file Twinkle.swift records the notes of the melody and the duration of each note.
  • The file Mission12_Buzzer_Music.swift sets the PWM signal to play the music.

Let's take a look at the file Mission12_Buzzer_Music.swift.

import SwiftIO
import MadBoard
import PWMTone

SwiftIO and MadBoard are the basic libraries used in all these projects.

Besides, you will use another library PWMTone to set the PWM in accordance with the musical score. But before you use it, you need to add it to your project. In the file Package.swift, add the library and indicate its location as the two highlighted lines.

var halfStep = 0 

The value halfStep is used to change the pitch. It equals 0 now, so the pitch remains unchanged.

var bpm = 60 

Set the bpm to 60, so each beat will last one second.

let player = PWMTone(PWMOut(Id.PWM2B), bpm: bpm) 

Initialize the player. init(_:bpm:noteGap:) has three parameters:

  • the first one is the PWM pin,
  • the second is the bpm,
  • the third one sets the interval between each note. It equals 0.1 by default.
player.play(track: Music.twinkle) 

This statement is to play the music. The method play(track:halfstep) has two parameters:

  • track is an array of tuples that indicates notes and note length. Music.twinkle refers to the score written in the file Twinkle.swift. In the struct Music, there is a constant twinkle that stores the notes and the note length. So the track is written as Music.twinkle.
  • halfStep has a default value of 0. You will set it later.
bpm += 40 
player.setBPM(bpm)

Then increase the bpm to make the tempo a little faster.

halfStep += 12
player.setFixedHalfStep(halfStep)

These two statements aim to raise an octave, so the pitch of the music sounds higher.

Reference

PWMOut - set the PWM signal.

PWMTone - play notes and change how it sounds by setting PWM signals.

MadBoard - find the corresponding pin ids of your board.