Play 3 Tones to a Speaker
 

Step 1 - Build the Project

Play 3 Tones to Speaker Hookup: 1 speaker on pin A5.

Topics Covered:

 

There is a 'tone' command built into the coding software that makes playing tones on the speaker very easy! The command works by entering the pin that you want to send the tone to, then the frequency of the tone, measured in hertz.

Step 2 - Upload the Code

/* * Send 3 rising tones to a speaker */ void setup(){ pinMode(A5,OUTPUT); //Set up your speaker on pin A5 & the other pin in GND //Send a tone of 250 hertz, then 450, then 850, then stop tone(A5,250); delay(1000); tone(A5,450); delay(1000); tone(A5,850); delay(1000); noTone(A5); } void loop(){ } // Move the tone code to the loop & see what happens! // (c) 2017 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

Step 3 - Read the Walkthrough

A tone will continue to play forever if you don’t use the command noTone to stop it. All of the code here exists in the setup() section, so it will happen only one time. After setting the speaker pin as an output, it’s ready to accept current to create a tone.

Next is defining the hertz of the tones you want to create. You can look up the frequencies of many notes on the internet and try to play songs with hertz. Here we play a 250 hertz tone for 1 second, then a 450 hertz tone, then an 850 hertz tone for one second and stop all of the tones.