The speaker included in the Photon kit can be used to produce tones or play simple music (note by note).
The speaker is a polarized part, meaning it has to be connected in a certain way to work properly. Specifically, the speaker has a positive pin and a negative pin. These can be identified by markings printed on the bottom of the speaker next to each pin.
Line up the speaker pins different numbered rows on the breadboard (be sure to remember which row has the positive pin), and push down to insert the speaker into the breadboard. Then plug jumper wires into the two rows with the speaker pins and connect them to the Photon.
IMPORTANT: The speaker's positive pin must connect to an I/O pin that supports pulse-width modulation (PWM) output. Only the following pins on the Photon can act as PWM outputs: D0, D1, D2, D3, A4, A5, WKP, RX, TX.
Experiment 5 in the online SparkFun Photon Experiment Guide shows how to connect the speaker. Here is the connection diagram from Experiment 5:
The speaker does not require any special code library.
In the global variables, you should declare which pin is being used as the speaker pin. The example below declares a variable called "speakerPin" (but you could use a different variable name).
Within the setup()
function, you have to include a statement to set the pin mode for the speaker pin variable:
Code for producing tones with the speaker could be placed within the setup()
function (runs only once), within the loop()
function, or within a custom function.
The speaker uses the tone()
function to produce a sound of a specific frequency for a specific duration of time.
The speaker can produce tones ranging in frequency from 20Hz (very low pitch) to 20KHz (very high pitch), which covers the full range of sounds that humans can hear.
NOTE: There is no built-in function to change the volume of the sound. However, you will notice that certain frequencies (in the mid-range) will naturally seem louder.
The tone()
function requires 3 values: speaker pin number, frequency, and duration. These values can be listed in the function as integer numbers or as integer variables.
The frequency should be an integer value ranging from 20-20000 (20Hz to 20KHz). Lower numbers will have a lower pitch (more bass). High numbers will have a higher pitch (more treble).
Note: The range of sounds that humans can hear naturally diminishes with age (starting as early as 18). As people get older, they become less able to hear high-frequency sounds. Everyone (regardless of age) should be able to hear sounds up to a frequency of 8000Hz. However, sounds above this frequency may not be heard by certain adults depending on their age range (so keep this in mind when building a product that uses sound).
The duration should be an integer value representing the number of milliseconds to play the tone (1000 milliseconds = 1 second). The Photon device will play the tone for the designated duration and then automatically turn the speaker off again.
Note: Using a duration of 0 (zero) will cause the speaker to produce a continuous tone.
You can use the notone()
function to turn the speaker off. This is not normally needed since a tone will automatically stop playing after the designated duration.
However, if you are playing a continuous tone (duration = 0) or simply want to turn off the speaker, use the notone()
function.
Try out this example app that plays a song. Do you recognize the song?
If you can figure out the notes and beats to a song, you can change the code below for songNotes
and songBeats
to play that song. Be sure to update the songLength
value, and adjust the tempo
value if necessary.
Did you figure out the song? 😎
Speaker
Photon Pin
Positive (+) pin
any I/O pin with PWM
Negative (-) pin
GND