toneAC v1.2 - Twice the volume, higher quality, higher frequency, etc.

Piezo elements are like capacitors, that is a problem for higher frequencies. For ultrasonic sound it is even very bad to use a square wave, because of the peak currents. So there should be a resistor of 220 ohm (or 100 ohm ?) to protect the outputs of the Arduino. Please add a warning about that to your library.

Tested with normal 8 ohm speaker with extra 120 ohm resistor:

  • Arduino Uno, pin 9,10 : good.
  • Arduino Mega 2560, pin 11,12 : good
  • Arduino Leonardo, pin 9,10 : good
  • ATmega8 16MHz, pin 9,10 : good

My conclusion:

  • No nasty click at start and stop : Excellent. A very small click can be heard.
  • Extra loud : Good
  • Small, fast code : Excellent
  • Flexibility : Not so good, fixed pins.
  • Volume control : Good, I needed that. Working roughly, not a linear scale.
  • Frequency range : The lowest frequency is not low enough for a slow blinking two-pin dual led.

If I select a ultrasonic frequency, I don't hear it, but a very small click can be heard when the sound is started and stopped.
Perhaps setting the pins floating (set as inputs) if not used could reduce that. But that will make the code larger.
The normal Arduino tone() library doesn't work for all frequencies with the ATmega8. I didn't test that for your toneAC library very well, but it sounds okay.

Its a very good library. Thank you.

PS: Do you know the Mozzi library ? http://sensorium.github.com/Mozzi/