Sound and Piezo Buzzers
Sound and Piezo Buzzers
Quick Review: Sound Frequencies
- Sound is created by vibrations in air pressure (sound waves)
- Commonly we measure the amplitude (volume) and the frequency (perceived pitch)
Piezo Buzzer
Piezo Buzzer
- Uses magnetic coil to vibrate a metal disc to produce sound
- Control the pitch by a form of pulse width modulation (PWM)
Uses of piezo buzzers
- Buzzer are small and cost-effective
- Can be used for alarms, games, toys
- Can produce tones and simulate single notes
Limitations of Buzzer
- Frequency
- Human hearing is roughly 20 Hz to 20,000 KHz
- Hertz (Hz) is cycles per second
- Piezo buzzer operates up to 2.048 KHz
- Sound Quality
- Only generate tones through square waves
Connecting Buzzers
Connecting Buzzers
- Buzzers are polarized so look for the +
- Negative pin to gnd and positive pin to Argon output
Producing Sound
Syntax
tone(<<PIN>>, <<FREQUENCY>>, <<DURATION>>);
-
tone()
generates a square wave using PWM (50% duty cycle) -
pin
is connected the speaker -
frequency
is sound frequency in Hz (20Hz to 20KHz) -
duration
is the length of tone in milliseconds
Producing Sound
Example
tone(D6, 500, 1000); // play 500Hz tone for 1000ms (1 sec)
tone(D2, 2000, 3000); // play 2KHz tone for 3000 ms (3 sec)
tone(D2, 1000, 0); // play 1KHz tone (don't stop)
A Note on Argon and tone()
tone()
requires a pin that supports PWM- PWM pins are assigned to one of three groups
- Each group can have different PWM values (duty cycles), but must share the same frequency and resolution
- Pins D4, D5, D6, D8
- Pins A0, A1, A2, A3
- Pins D2, D3, A4, A5
Stopping Sound
Syntax
noTone(<<PIN>>);
- To stop a continuous tone or stop tone before duration is over
Optional: Controlling Volume
Optional: Controlling Volume
- All tones will be at the same volume since Argon can change only frequency (not amplitude)
- To control volume, connect a potentiometer between
negative
andgnd
- Potentiometer acts a current limit resistor to control volume
Playing Melodies
- Musical notes can represented as constants
- Add this file (pitches.h) to your
src
folder and then add#include "pitches.h"
to your sketch - You can then refer to music notes in the following manner
tone(D2, NOTE_C4, 1000); # play the note "middle C"
Finding Notes of Popular Songs
- Notes for some popular songs (note that the format for playing notes is different in this code, but you can still extract the notes and duration from these examples)
- MIDI to C converter allows you convert a MIDI file directly into
References
- Images created with Fritzing
- Pixabay
- Sparkfun
- Neuton Foo