The Circuit Playground Library

The Arduino development environment provides a handy function for creating a varying square wave on a digital pin: the tone command. It is basically the same function as the makeTone function we used on the last page. You can read more on the arduino.cc website here.

For Circuit Playground, Adafruit provides a number of handy functions to use all the goodies on the board - all wrapped up into a library called CircuitPlayground.

Adding the library gives us great functionality which we'll explore in the examples going forward.

Let's start by rewriting the demo program on the previous page.

We will add the following:

A call to CircuitPlayground.begin to initialize the library

Reading the buttons is accomplished via CircuitPlayground.leftButton and CircuitPlayground.rightButton

Finally, the Circuit Playground library plays tones through the speaker through the CircuitPlayground.playTone function.

Note we do not have to know anything about the underlying hardware to use the library, rather handy.

Here is the whole example:

// Adafruit Circuit Playground - Two tone sounds    Support Open Source, buy at Adafruit
//   2016-08-05 Version 1  by Mike Barela for Adafruit Industries
// Uses the CircuitPlayground library to easily use the full functionality of the board

#include <Adafruit_CircuitPlayground.h>

void setup() {
  CircuitPlayground.begin();
}
 
void loop() {
  if(CircuitPlayground.leftButton()) {   // if reading the left button returns true
    CircuitPlayground.playTone(440,100);    // output a 440 Hz sound for a tenth of a second
  }
  else if(CircuitPlayground.rightButton()) { // if reading the right button returns true
    CircuitPlayground.playTone(1760,100);   // output a 1760 Hz sound for a tenth of a second                    
  }
}

The code is alot shorter and is easy to read.  It should also work on any board which supports the Circuit Playground library.

The program will play one tone when you push the left pushbutton, another tone when you push the right pushbutton.

We'll use other Circuit Playground library functions as we explore what can be done with the board's sound capabilities. You'll see why 440 and 1760 Hertz were chosen above.

Last updated on 2016-08-17 at 12.15.03 PM Published on 2016-08-17 at 02.09.02 PM