Piezo buzzer FAIL (and fix)

A project log for Water Alert

hear it before you step in it

mcunerdmcu_nerd 05/10/2019 at 01:355 Comments

A few days back I played around with creating a siren sound on the Atmega328P, which I would later port to the Attiny85 and incorporate it in this project.  I simply hooked up a piezo directly to one of the I/O pins (with a series resistor.)  It worked just fine.  

Yesterday I assembled one of my new Water Alert boards that I got last week. I ported the siren code over the the Attiny85 and plugged the programmed Attiny85 and a piezo into my Water Alert board.

I heard absolutely nothing.

I checked my code, found something that needed to be changed and tried again; nothing.  I swapped the piezo for a larger one.  This time I heard it extremely faintly.  Some progress at least.  I then went down the rabbit hole trying to isolate the problem. I went from using a 2xAA battery back to a 3xAA pack; no improvement.  I hooked up the board to a 5V supply; no improvement. Could the issue be the cheapo "Shenzhen special" transistor on the board? I created a breadboard version of the peizo driver circuit and once again, no improvement.

I decided to try directly driving the piezo from the I/O pin. A considerable improvement occurred. Perhaps the issue was how I was trying to drive it.  I did a bit of googling and found the answer!  Apparently due to the capactive nature of a piezo buzzer, there needs to be a way for it to discharge when when the transistor is turned off.  I naively thought that I could drive it just like an LED.  The fact that it worked by driving it directly from an I/O pin further made me think it wouldn't be an issue.  The easy solution is to put a resistor across the piezo leads. I tested it, and that fixed it. I made a more permanent fix by bodging in a 150 ohm resistor across the piezo header pins on the board (the first time I've ever done an actual bodge on a board I designed.)

lesson learned: Test out circuits that I have not done before on a breadboard before ginning a board!  This is why I haven't posted the board files yet, in case there was a design issue.  I'll go back into KiCad and make the correction tomorrow.

I got a bit curious of why it worked fine when the piezo was directly hooked up to an I/O pin.  I remembered that many MCUs such as the Attiny85 and Atmega328P have internal ESD protection diodes, perhaps those diodes were what allowed the piezo to discharge, and thus work.

I setup my breadboard circuit again.  I tried a normal diode I had lying around, didn't work.  Was I wrong?  I thought about it for a few moments. Those internal ESD production diodes are likely fast switching.  I looked thru the horde and found a schottky diode. I put the diode reverse-biased across the piezo leads and it worked!


Ken Yap wrote 05/10/2019 at 04:46 point

Interesting. I thought those beepers had an onboard transistor to generate the oscillation. I've used one in my metronome project with a large capacitor for a bell-like decay.

Or are you generating the driving frequency yourself and the beeper is just a transducer? I suppose it would be like a capacitor then.

  Are you sure? yes | no

mcu_nerd wrote 05/10/2019 at 11:43 point

Yea it's just a transducer, it doesn't contain any oscillator circuity.  The reason I chose that kind was to allow me to generate my own tones.  It's not quite as loud as I want it to be even after I used a 0 ohm jumper in place of the series resistor.

  Are you sure? yes | no

Ken Yap wrote 05/10/2019 at 12:31 point

Maybe you could just subcontract the siren sound to a chip like this and a speaker. They are less than a buck.

  Are you sure? yes | no

mcu_nerd wrote 05/10/2019 at 13:12 point

Interesting, I'll have to check that out. Thanks.

  Are you sure? yes | no