The toneAC library is now available.
Here are the advantages over the standard tone library:
- Nearly twice the volume (because it uses two out of phase pins in a push-pull manor)
- Much higher quality (no clicking)
- Capability of producing higher frequencies (even if running at a lower clock speed)
- Nearly 2k smaller compiled code
- Can set not only the frequency but also the tone volume (duty)
- Less stress on the speaker so it will last longer and sound better
I tested it on an Uno and Teensy 2.0. Making sure it works on the Mega and other boards would be helpful to know. Enjoy!
The official development forum has been created and can be found here.
Further discussion should probably go there instead of with this thread.
----- Original message -----
Concept is simple. I wanted to create an AC type output from two PWM pins using the same timer (timer 1) at a very high speed. The purpose is to create a higher sound pressure level output from a piezoelectric transducer than the standard pulsed-DC method used in the Tone library. Basically, instead of a pulsed-DC +5v and 0v (5 volt range) signal, driving two pins out of phase yields an effective +5v and -5v (10 volt range).
The tone library won't work as it doesn't do in/out of phase (also may have high frequency issues when the ATmega is running at low speed). The TimerOne library won't work because it also doesn't do in/out of phase PWM.
I couldn't find such a library, so I wrote a very fast port register level library that provides a perfect in-phase and out of phase PWM signal on two PWM pins. On an Arduino Uno you would tie the piezoelectric buzzer to pins 9 and 10 and using my library specify the frequency and you're done. Even if running the ATmega at 4 MHz like I was, this library can still create an ultrasonic frequency, and much louder than with the Tone library as it's driving both pins out of phase with each other. It always keeps the duty at 50% for the highest output.
Anyway, did I miss a library that does this? Or, is there interest in me turning it into a library for the community. It wouldn't be difficult, but there's no reason to bother if no one is interested. Think of it like a high-power and faster version of the Tone library but fixed to the assigned PWM pins for timer 1 (9 & 10 for Uno) (14 & 15 for Teensy 2.0) (11 & 12 for Mega).
By fast, typically PWM can be as fast as 62.5 kHz. But, because my library sets the upper limit, it can be as fast as 8 MHz (if my math is correct) at 16 MHz. As I wanted to do ultrasonic frequencies and I was running the ATmega at 4 MHz, the highest frequency Tone or TimerOne could produce was only 15,625 Hz. But, with my library, even at a 4 MHz clock speed I could generate a 40 kHz or even higher signal, and do it so two pins are out of phase at the same time for increased output volume.
So, did I waste my time or should I release it? Anyone got a catchy name to call it? I've created other libraries like the very popular NewPing and LCDBitmap, so you can trust it will be a high quality library.