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!