Greetings, I have hooked up a piezo speaker from an old cell phone and loaded the code from http://www.arduino.cc/en/Tutorial/PlayMelody I have my PWM outputs taken by motors so I put the speaker on digital pin 3 and modified the code accordingly. It works the same. SO my questions: Why do they say I need to use the PWM outputs? (9,10 or 11) Will I damage the chip? Is there a max frequency for the other (not PWM) outputs? I could not find that info in the data sheet Thanks Kirk
The first version on that page will work on any pin. The second version which has volume control will require a PWM pin (and probably sound like hell).
I think it is safe to drive a piezo speaker directly from a pin. But there are some things you can do to make sure you are safe when driving a mystery load... - Put a 100 ohm resistor between the pin and the piezo. This will keep the current on a transition from exceeding the limits of the Arduino pin. - Between the resistor and the piezo, run a shotcky diode to +5v and ground, oriented so they don't normally conduct, but will dump current if the voltage tries to go over +5 or under ground. There are already diodes like these inside the AVR processor, but if you can dump most of the energy through the external diodes you will be better off. (That resistor from the first note will help ensure that the current dumps in the external diodes.) I say Schotkky diode because they are fast switching and low voltage drop. It doesn't need a lot of capacity, it will just be eating spikes.
I'm currently working with a 40kHz piezo transducer, I'm driving it from both leads... +5,0 to 0,+5 and am seeing spikes about 5v on the transitions if I leave out the diodes. That may be a result my double ended driving, but unless you have an oscilloscope to check for spikes, I'd put the diodes in.
Thanks for the info. I have a 96 Ohm resistor but I did not think about the inductance and thus did not add a diode. Kirk PS It seems wise that the tutorial should be updated. I am willing to help with this.