Skip to main content

PWM sound output

PWM signal can change the brightness of LEDs. You must be quite familiar with it through some projects. What's more, it allows the buzzer to generate sounds.

This example shows how to use the PWMOut to generate notes. It plays musical notes from 1 to 7 repeatedly.

What you need

  • SwiftIO Feather (or SwiftIO board)
  • Breadboard
  • Buzzer
  • Jumper wires

Circuit

  1. Plug the buzzer onto the breadboard.
  2. Connect any one leg to the pin GND and the other to the pin PWM5A.
circuit diagram

Example code

You can find the example code at the bottom left corner of IDE: example / SimpleIO / PWMSoundOutput.

// Produce different notes by changing the frequency of PWM signals.
// Import the library to enable the relevant classes and functions.
import SwiftIO

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

// Initialize a PWM output pin the speaker connects.
let speaker = PWMOut(Id.PWM5A)

// Specify several frequencies to produce different sound.
let fre = [262, 294, 330, 349, 392, 440, 494]

// Play recurrently these notes.
while true {
for f in fre {
// Set the frequency and the duty cycle of output to produce each note.
speaker.set(frequency: f, dutycycle: 0.5)
// Play each note for one second.
sleep(ms: 1000)
}
}

Background

Buzzer

The PWM signal outputs high and low voltage alternatively. There is a diaphragm inside the buzzer. It can move back and forth as the signal switches between on and off. The vibration causes the surrounding air to vibrate. Thus the buzzer produces the sound.

How buzzer works

There are two kinds of buzzers. An active buzzer has an internal circuit to create alternating voltage. So you can connect it to power. A passive buzzer needs a PWM signal to vibrate the diaphragm. The frequency will influence the pitch. A higher frequency will produce a higher pitch.

Code analysis

let fre = [262, 294, 330, 349, 392, 440, 494]

This array stores the frequencies of seven notes from 1 to 7. The first frequency corresponds to middle C.

speaker.set(frequency: f, dutycycle: 0.5)

The frequency is an essential factor for sounds. The method set() allows you to set both the frequency and duty cycle of the PWM signal.

sleep(ms: 1000)

sleep defines the duration of notes. Each note will last one second.

Reference

PWMOut - set the PWM signal.

MadBoard - find the corresponding pin id of your board.