I quite often use a DC piezo buzzer for the sound element of my circuits I find these buzzers give a much louder sound level than can be obtained simply by using the tone() command to drive a standard piezo element.
In some circumstances I find the sound level too loud, so experimentally I tried using PWM to see if I could alter the sound level. My initial tests were somewhat discouraging, I found I could alter the sound volume but the tone output seemed distorted.
I wondered if I could get a less distorted sound by altering the PWM frequency, so using the Arduino PWM frequency cheat sheet [1] I worked my way through the possible pin and frequency combinations eventually settling on the frequency of 31372.55Hz which is available on both pins 9 & 10 (timer1) and 3 & 11 (timer 2) I found this frequency to give the cleanest sound.
Hardware description:The circuit consists of an Arduino UNO and a DC piezo sounder element.
The ground end of the DC piezo sounder is connected to ground, the positive to PWM pin 11 which provides the drive signal.
Software overview:99% of the code consists is the Fade sketch which you can find in the Arduino IDE under File->Examples->01.Basics->Fade.
I altered the PWM pin and added in code to set the PWM frequency. I renamed some of the variables for clarity.
Failure:Buoyed by my success I attempted to control the brightness of a 5 volt flashing LED. I tried a variety of PWM frequency’s and altered the step delay to 3 seconds thinking there may be a capacitor that needed charging within the device. I had no success in getting this to work.
Addendum:If you want to use PWM to drive a standard piezo element I found that maximum volume could be achieved by altering the PWM frequency to one that closely matches the resonant frequency of the piezo element.
References:
[1] https://playground.arduino.cc/Main/TimerPWMCheatsheet (Arduino PWM cheat sheet, checked 27/March/2020)
Comments