The RedBot kit includes a "buzzer" — a small speaker that can produce simple sounds. The speaker can only play one tone (sound) at a time, but you can create different sounds or sound patterns.
The speaker can be used to provide alerts or feedback to people interacting with your robot.
You can also use the speaker to play a song one note at a time.
To use the speaker in your robot app, you will need to:
Declare a gloabl variable to store the speaker's pin number
Set the pin mode for the speaker
Use the tone()
method to produce a sound
You'll need to create a global variable to store the pin number of the speaker, which is normally connected to pin 9. Add this code statement before the setup()
function:
You'll need to set the pin mode for the speaker. Add this code statement within the setup()
function:
The tone()
method is used to produce a sound of a specific frequency using the speaker.
The frequency should be an integer value (whole number) between 20-20000 Hertz:
Lower values (lower frequencies) produce a lower pitched sound (more bass).
Higher values (higher frequencies) produce a higher pitched sound (more treble).
To produce a sound that most people can easily hear, use a frequency value between 50 and 8000 Hertz. Try using a value of 3000, and then decide whether you want the sound to have a higher or lower pitch.
Typically, you will only want to play a tone for a certain duration of time and then turn it off.
For example, the following code will use the speaker to play a tone with a frequency of 2000 Hertz for 0.5 seconds and then turn it off:
Notice that the noTone()
method was used to turn the speaker off. Otherwise, the tone would keep playing continuously.
Alternatively, you can include the duration of the sound (in milliseconds) when using the tone()
method. In this case, the tone will automatically turn off once the duration is over:
VOLUME: There is NOT a way to change the volume of the tone. However, you will notice that certain frequencies will naturally seem louder to your ears.