Using the Circuit Playground Speaker

You will want to review Adafruit tutorials on how to use Circuit Playground and program in the Arduino integrated development environment.  If you are new to using your computer to program Arduino style projects, please review the tutorial Circuit Playground Lesson #0.  That will familiarize you with the Circuit Playground board and ensure your programming environment is set up and ready to go.

Below are the components on the Circuit Playground board we will be looking at using in this tutorial:

If you prefer to go to the Circuit Playground library for using the speaker rather than peeking under the hood, you can skip to the next page without worries.

Our First Sounds!

The speaker on the Circuit Playground is connected to the microcontroller digital pin #5.  The left button is on digital pin #4 and the right button is on digital pin #19 (it is handy that these pin numbers are printed in white on the circuit board!).

What we will do first is play one tone (a sound at one frequency) when the left button is pressed, and another if the right button is pressed.  Let's pick two frequencies in Hertz (vibrations per second).  I'll pick 440 Hertz (abbreviated Hz) and 1760 Hz (not quite random values, we'll go into that very soon!). Our code will make one of the sounds when the corresponding pushbutton is pressed on Circuit Playground:

// Adafruit Circuit Playground - Two tone sounds    Support Open Source, buy at Adafruit
//   2016-08-05 Version 1  by Mike Barela for Adafruit Industries

const int speaker = 5;       // The CP microcontroller pin for the speaker
const int leftButton = 4;    // The CP microcontroller pin for the left button
const int rightButton = 19;  // The CP microcontroller pin for the right button

void setup() {
  pinMode(speaker, OUTPUT);     // We will write out to the speaker
  pinMode(leftButton, INPUT);   // We'll read in from the buttons
  pinMode(rightButton,INPUT);
}
 
void loop() {
  if(digitalRead(leftButton)) {   // if reading the left button returns true
    makeTone(speaker,440,100);    // output a 440 Hz sound for a tenth of a second
  }
  else if(digitalRead(rightButton)) { // if reading the right button returns true
    makeTone(speaker,1760,100);   // output a 1760 Hz sound for a tenth of a second                    
  }
}
 
// the sound producing function (a brute force way to do it)
void makeTone (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds) {
  int x;   
  long delayAmount = (long)(1000000/frequencyInHertz);
  long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
  for (x=0; x<loopTime; x++) {        // the wave will be symetrical (same time high & low)
     digitalWrite(speakerPin,HIGH);   // Set the pin high
     delayMicroseconds(delayAmount);  // and make the tall part of the wave
     digitalWrite(speakerPin,LOW);    // switch the pin back to low
     delayMicroseconds(delayAmount);  // and make the bottom part of the wave
  }  
}

The program's main loop function reads the two Circuit Playground push buttons.  If the left one is pressed, the speaker outputs a sound at 440 Hz (a low tone) , if the right button is pressed, it outputs a sound at 1760 Hz (a higher tone). At the bottom of the program we can peek into the code making the tone via the makeTone function.  The loop turns on the speaker, waits, then turns it off. The speed of switching, slow or fast, makes the tone at the frequency at which the speaker pin is turned on and off.

Next is how to write the same code using the Circuit Playground library.

Last updated on 2016-08-14 at 03.39.58 AM Published on 2016-08-17 at 02.09.02 PM