Adafruit Circuit Playground Music Player Tutorial

By Nath Tumlin. Feel free to send comments, questions, concerns, corrections, or cash to ngtumlin@crimson.ua.edu

Video demo

In this tutorial we learn how to make the Adafruit Circuit Playground play music, in this case the first 4 measures of Row, Row, Row Your Boat, when we press the left button.

Initialization

We'll start by including Adafruit's CircuitPlayground library.

#include "Adafruit_CircuitPlayground.h"

Next we need to define some variables which will hold the notes we need to play (in hertz) and how long we should play them for (in eighth notes).

static int notes[]    = {523, 523, 523, 587, 659,
                         659, 587, 659, 698, 784};

static int duration[] = {  3,   3,   2,   1,   3,
                           2,   1,   2,   1,   6};

We also declare a playNote() function, which will play a note for a specified duration.

void playNote(int frequency, int duration);

Then we need to define the setup() function, which runs one time when we turn on the board, and in this case just sets up the CircuitPlayground library

void setup() {
  CircuitPlayground.begin();
}

Playing the song

The loop() function runs over and over once setup() is finished. Here it checks to see if the left button is pressed,

void loop() {
  if (CircuitPlayground.leftButton()) {

and if it has, it loops through all 10 notes and plays them for their duration.

for (int i=0; i<10; i++) {
  playNote(notes[i], duration[i]);
}

Playing a note

The function CircuitPlayground.playTone() plays a note for a specified duration, but doesn't wait for that duration before it returns. To keep all our notes from playing at once, playNote() also calls delay() so that we wait for one note to finish before playing the next one.

void playNote(int frequency, int duration) {
  CircuitPlayground.playTone(frequency, duration * 150);
  delay(duration * 160);
}

We multiply duration when passing it to CircuitPlayground.playTone() and delay() because they both accept arguments in milliseconds, and duration is in eighth notes, so the multiplication does the proper conversion. The slightly higher multiplier in the delay() call is so that we have a slight pause between notes.

The complete code

#include "Adafruit_CircuitPlayground.h"

static int notes[]    = {523, 523, 523, 587, 659,
                         659, 587, 659, 698, 784};

static int duration[] = {  3,   3,   2,   1,   3,
                           2,   1,   2,   1,   6};

void playNote(int frequency, int duration);
						   
void setup() {
  CircuitPlayground.begin();
}

void loop() {
  if (CircuitPlayground.leftButton()) {
    for (int i=0; i<10; i++) {
      playNote(notes[i], duration[i]);
    }
  }
}

void playNote(int frequency, int duration) {
  CircuitPlayground.playTone(frequency, duration * 150);
  delay(duration * 160);
}