Arduino Project: Using the tone() Function for Buzzer Music
The tone()
function in Arduino is a useful tool for playing notes on passive buzzers. By combining multiple calls to the tone()
function, we can create melodies and play songs.
To use the tone()
function, follow this syntax:
1 | tone(<PIN>, <FREQUENCY>) |
For example, to play the C7 note on a buzzer connected to pin 8, use the following code:
1 | #define NOTE_C7 2093 |
Please note that frequency is measured in hertz (Hz).
To stop playing the sound, you can use the noTone(<PIN>)
function. Here’s an example:
1 | tone(8, NOTE_C7) |
Alternatively, you can set the duration of the tone as a third parameter in the tone()
function. This way, you don’t need to call noTone()
separately:
1 | tone(8, NOTE_C7, 100) |
If you need the frequencies of other musical notes, you can refer to the list below. These values are taken from the official Arduino toneMelody
example:
1 | #define NOTE_B0 31 |
Using the tone()
function in Arduino opens up numerous possibilities for creating music with buzzers. Explore the different frequencies and combinations to bring your Arduino projects to life with sound!
tags: [“Arduino”, “tone function”, “buzzer music”, “melodies”, “songs”]