Sound and Piezo Buzzers

Sound and Piezo Buzzers

Sound wave

Quick Review: Sound Frequencies

Sound wave

  • Sound is created by vibrations in air pressure (sound waves)
  • Commonly we measure the amplitude (volume) and the frequency (perceived pitch)

Piezo Buzzer

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

1570925594505

Connecting Buzzers

buzzer polarity buzzer polarity

  • Buzzers are polarized so look for the +
  • Negative pin to gnd and positive pin to Argon output

Producing Sound

Syntax

tone(<<PIN>>, <<FREQUENCY>>);		// will play tone indefinitely
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

A Note about Tone and Blocking

  • tone() is non-blocking (meaning it doesn’t cause delays or interfere with other Argon actions)
  • This means the Argon will not wait for one tone() to finish before playing the next. For example, in the code below, even though the first tone has a duration of 1 sec, the second tone will start immediately, meaning you will hear the second tone
tone(D6, 500, 1000);	
tone(D2, 2000, 3000);	
  • In order hear a tone for certain length of time, we either need to use a millis() timer or delay()

Optional: Controlling Volume

1570925716449

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 and gnd
  • 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"	

References

Updated: