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. Place the shield on top of your SwiftIO board.

  2. Connect the buzzer module to pin PWM2B (D10).

Circuit diagram

Example code

// Import the SwiftIO library to use everything in it.
import SwiftIO

// Import the board library to use the Id of the specific board.
import MadBoard

// Import it to set how the music will be played with PWM signals.
import PWMTone

// Set the parameters of the music.
var halfStep = 0
var bpm = 60
// Initialize a player to get ready for the music.
let player = PWMTone(PWMOut(Id.PWM2B), bpm: bpm)

while true {
// Play the music.
player.play(track: Music.twinkle)

// Make the tempo faster.
bpm += 40
player.setBPM(bpm)

// Make the pitch higher
halfStep += 12
player.setFixedHalfStep(halfStep)

sleep(ms: 1000)
}

Background

Musical notes

Before coming to the code, you will know something about the 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.

  • In the file main.swift, you will set the PWM signal to play the music.

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

import SwiftIO
import MadBoard
import PWMTone

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

And you will use another library PWMTone. It allows you 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.

Package.swift
import PackageDescription

let package = Package(
name: "Mission12_Buzzer_Music",
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/madmachineio/SwiftIO.git", from: "0.0.1"),
.package(url: "https://github.com/madmachineio/MadBoards.git", from: "0.0.1"),
.package(url: "https://github.com/madmachineio/MadDrivers.git", from: "0.0.1"),
.package(url: "https://github.com/madmachineio/PWMTone.git", from: "0.0.1"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "Mission12_Buzzer_Music",
dependencies: [
"SwiftIO",
"MadBoards",
"MadDrivers",
"PWMTone"
]),
.testTarget(
name: "Mission12_Buzzer_MusicTests",
dependencies: ["Mission12_Buzzer_Music"]),
]
)

Then let's continue the analysis.

var halfStep = 0 

The value halfStep is used to change the pitch. It equals 0, 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. It 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.

  • The 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.
  • The halfStep has a default value of 0. You will set it later. So you can ignore it here.
bpm += 40 
player.setBPM(bpm)

Then increase the bpm to make the tempo a little faster. This increment is a random number you prefer.

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.

SwiftIOBoard - find the corresponding pin id of your board.