Arduino: build speakers and output tones - how it works
You can also use the Arduino to build a speaker and have tones output. You can find out exactly how this works and which code you need in this practical tip.
Arduino: build speakers and output tones - how it works
To turn your Arduino into a speaker, you first have to distinguish between an active and a passive buzzer. An active buzzer is already equipped with an oscillating source that generates a sound as soon as a voltage is applied. However, you can only produce one tone with it.
- A passive buzzer is a little better. You can control the tone using pulse width modulation. Simply connect the buzzer to the ground and to a PWM pin (e.g. pin 9).
- Now you can easily output tones using the tone method. To do this, write »tone (9, 2000)«. Now a tone would be output on pin 9 with a frequency of 2, 000 Hz.
- If you want, you can also specify the duration in seconds with an additional parameter: »tone (9, 2000, 5)«. So the sound only lasts five seconds; otherwise it would be played forever.
- With the command »noTone (9);« you end all tones on a pin.
- Tip: By the way, you can play professional melodies with the Arduino. First import the sound library with the command »#include" pitches.h "«. Some variables have already been created in this library so that a frequency has been automatically assigned to a certain note, such as »NOTE_C3«.
In the next practical tip we will show you how to build a thermometer with the Arduino.