Push-pull piezo speaker on Attiny85

Hi, I would like to drive a piezo buzzer with two pins from an Attiny85 to make the sound louder and cleaner. Is there a library or sample code that I can use?

This question is very similar, but it's from 8 years ago, and I was wondering if anyone has made/published anything since then. Unfortunately the topic is locked and I can't comment on it.

This is the piezo speaker that I am currently using:

It is connected to PB4 of Attiny85 and ground. 3v circuit, 1MHz internal clock. Programmed with Arduino as ISP, ATTinycore, tone() from Arduino IDE. Is there anything that I should change to get the cleanest sound from what I currently have?

I am aware that larger piezos will give better sound, and will look into that in the future.

Thanks in advance for your help.

The ToneAC uses hardware timers and therefor it can only be used on certain boards: https://bitbucket.org/teckel12/arduino-toneac/wiki/Home.

Is the tone() not clean ? If you change the frequency, there might be a audible hickup.
That's a small piezo indeed ! A bigger piezo with a good box around it will be much louder.
A good box around a piezo or normal loudspeaker can make a sound ten times louder.

Did you know that a piezo requires a resistor ? The piezo acts as a capacitor and to reduce the current peaks, a resistor of about 220Ω is required. That will make the sound less loud.

Circuits for loud sounds use a loudspeaker with a amplifier or a piezo with a LC oscillicating circuit (the piezo itself is the capacitor). When driving a piezo with a square wave and a high frequency, a lot of energy is wasted.

Thanks for your fast reply!

The main problem with the sound is that the tone is very sharp, possibly because of overtones, even at lower frequencies like 900Hz, and more so at higher frequencies. I only need it to buzz at one frequency, so the clicking from changing frequencies is okay. Running it at higher frequencies like 1600Hz is uncomfortable to hear and rather annoying. The buzzer is turned on in response to user input, approximately every 3-10 seconds.

I didn't know that a piezo needed a resistor, so I should connect one in series between the piezo and ground?

Yes I'll definitely be looking into larger piezos, perhaps 12 or 20mm ones? Unfortunately space is very limited for the intended application, which is a handheld device.

Yes, a series resistor. Often the resistor is at the Arduino pin and the piezo or speaker at the GND pin.

How loud a piezo is, depends on how it is attached in the device.

I thought that a piezo was supposed to be annoying :wink:
The Arduino outputs a square wave. Perhaps your piezo device has a lot of internal reflections and more high frequencies.

1 Like

If you want the piezo to sound nice, use sine wave AC to drive it, not square waves you are current using.