In my previous post, I wrote about how I got the current draw on my noisemaker down to a small enough amount that it should be able to run for months. (More to come about battery life when my experiment is finished.) I didn’t originally start with an ATtiny45/85 though, so I thought I would write a bit about what I started with, and how the design changed as I went.
I started out, as I usually do, with an Arduino Uno. What I wanted to do originally was use PWM to play sounds on a piezo speaker. Timer1 is the library I found that let me do that. With that, I was able to get some pretty annoying beeping sounds out of that little piezo speaker.
Since I wanted to plant 8-10 of these in the office, $35 each for an Uno was way too much. I wanted to be able to make them for less than $10 each. To do this, I would need to get this working with a bare AVR chip and no external oscillator. This would also make them smaller and easier to hide.
My first step toward that goal was to reproduce the same circuit on a breadboard using an ATmega328. Without the USB-serial converter from the Uno, I would need to program it through the ICSP pins using my Arduino-ISP programmer.
Once this was working (which was pretty quick since this wasn’t the first time I had done this sort of thing) I decided to dead-bug a prototype. I was apprehensive about soldering directly to the chip though, because I’d never done it before, and I didn’t have any spare chips around. I did, however, have a whole bunch of 28-pin DIP sockets around, so I popped the chip into one of those, and soldered everything to that. Thus was born my first prototype:
Unfortunately, this prototype still required some kind of power source. I had neglected to consider that when I constructed it. I decided, possibly hastily, to come back to that later. In hindsight, the power supply is something I should consider early on in the design, considering how much it affected the final design. I decided instead to concentrate on the fact that it was also still using a fairly expensive (only $2, but it adds up when you’re making 10) crystal oscillator, and a large and expensive ATmega328. I knew from a previous project that by setting fuses, I could get the AVR to run without the need of the oscillator, so I set the appropriate fuses, and got it beeping away annoyingly without needing the oscillator. To prove it was working, I constructed a 2nd prototype, without the oscillator, or the two 20pf capacitors needed for the oscillator. I also decided to forgo the 10uf capacitor between GND and Vcc, trusting that whatever battery I used would be able to supply sufficiently ripply-free power to make it unnecessary.
The 3rd prototype was when I decided to try shrinking it in size and cost by using an ATtiny45. This presented a bit of a challenge since they don’t have a 16-bit timer, for the PWM sounds, like the ATmega328. Instead of trying to get things working with an 8-bit timer (which I’m not sure has the resolution for playing back notes and such) I ended up rewriting it all to just bit-bang the sounds out. This worked great, but I still had the problem of the power source to work out.
Somewhere along the way, I don’t actually remember when, I did all the work to get the power consumption down to tiny amounts. I also discovered that once I turned off the brownout protection as part of that, I was able to run the whole thing from a CR2032 coin-cell battery. I included the coin-cell holder in the photo above for scale. It’s much bigger than both the ATtiny and the piezo buzzer combined, so that gave me an idea: Why not glue the IC and piezo to the back of the battery holder, and wire it all up like that. A bit of playing around with different arrangements led me to this:
The CR2032 is only 3V, but at the clock speed I was running the AVR, and with brownout protection set low (or off, in my case), it runs fine at 3V. The only downside is that the piezo isn’t as loud as it was at 4.5 or 5 volts.
I’m actually quite proud of how this turned out, other than the diode. The IC, piezo, and 4-pin header are all glued to the back of the battery holder. Pins 5,6,7 (MOSI, MISO, SCK) are bent over flat so they touch the pins on the 4-pin header. The Vcc pin is bent, carefully, around sideways so it touches the positive terminal of the battery holder. The negative pin of the piezo buzzer is touching the negative terminal of the battery holder.
One of the leads on the resistor from Vcc to pin 1 (reset) is bent around down the center of the IC and over to the 4th pin on the 4-pin header, to make those 4 pins easily accessible for programming. Another resistor (for purposes I’ll explain shortly) is run from pin 7 to the IC’s ground pin, and that resistor lead also carries on to connect to the negative terminal of the battery.
Finally, a diode connects the positive terminal of the piezo buzzer to pin 2 on the IC. The first two prototypes connected the piezo directly to the IC. When I was working on the 3rd prototype, the first one to use an ATtiny, I got it working on a breadboard first. While I was working on the code for bit-banging the sounds, I noticed that some frequencies would cause the AVR to reset. I kind of suspect that a capacitor across the +/- terminals might fix that, but for some reason I didn’t try that at the time. I didn’t realize then that a piezo speaker isn’t (I think?) an inductive load, like a regular speaker would be, so I thought that maybe I needed a diode to prevent reverse current spikes. I’m not entirely sure why it fixed the problem, but it certainly seems to have fixed it.