PWM - Play Music by buzzer
Example
Introduction
A sound is composed of volume, tone and timbre. Volume is determined by the amplitude of the sound wave. Tone is determined by the frequency of the sound wave. Timbre is determined by the waveform of the sound wave.
In this example, we use PWM to control the buzzer to emit sound with desired tone. As PWM outputs square waves, if we wish to emit tone C4 (frequency=262Hz), we must use PWM to output square waves with wavelength 1/262 = 3.8ms:
We use PWM to output sound waves with different frequency, so as to play music through the buzzer.Connect the buzzer to the PWM output pin shown in the following diagrams.
Procedure
AMB21 / AMB22 wiring diagram:
AMB23 wiring diagram:
BW16 wiring diagram:
BW16 type C wiring diagram:
AW-CU488 Thing Plus wiring diagram:
AMB25 wiring diagram:
Open the example code in “Examples” -> “AmebaAnalog” -> “TonePlayMelody”
Compile and upload to Ameba, press the reset button. Then you can hear the buzzer playing music.
Code Reference
https://www.arduino.cc/en/Reference/Tone
https://www.arduino.cc/en/Reference/NoTone