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:

tone(<PIN>, <FREQUENCY>)

For example, to play the C7 note on a buzzer connected to pin 8, use the following code:

#define NOTE_C7 2093
tone(8, NOTE_C7)

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:

tone(8, NOTE_C7)
delay(100)
noTone(8)

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:

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:

#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
...
#define NOTE_DS8 4978

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!