Arduino hacking: differential drive for piezos and the Tone Library

My Christmas present this year was a bunch of Arduino gear, so I have spent some time playing with it. I bought a piezo to see what beeping noises I could make. To get accurate tones, it turns out that you need to use the hardware timers to generate the square waves. The Tone library does exactly that, with a nice interface to boot.

To learn my way around larger pieces of arduino code, I decided to change the tone library so that it optionally uses differential drive. This is how you get twice the voltage, and twice the volume, of a regular pin-to-ground piezo setup. By connecting both ends to different pins and making sure their outputs alternate, we can get 10V edges out of the arduino’s 5V pins. While it is well short of my piezo’s 30V range, it’s loud enough to hear it across the apartment (ie sufficiently loud to annoy your SOs).

Anyway, here’s the patched Tone.cpp file. When creating a Tone object, you have the option of passing an extra parameter which is the second pin. If you ignore it, the code won’t tie up any extra pins (but it will use more memory).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s