Author Topic: Piezo buzzer, tone library, and PWM  (Read 5334 times)

micahwedemeyer

  • Newbie
  • *
  • Posts: 6
Piezo buzzer, tone library, and PWM
« on: July 19, 2016, 11:41:30 am »
I'm having a bit of trouble getting the Arduino tone library (https://www.arduino.cc/en/Reference/Tone) to work, or even the basic Oak buzzer (http://digistump.com/wiki/oak/tutorials/buzzer) example. I'm still confused by things like 3.3v vs 5v and so forth, so I'm probably making a very basic mistake.

First, I'm powering the Oak with the Vin and GND in the center 3 pins, using a 5V regulated power supply. So, if I understand correctly, all the output pins should be 5v.

I have it directly connected to this speaker https://www.sparkfun.com/products/7950?_ga=1.53800815.658135470.1462117205 from sparkfun.

First, I tried it with a real Arduino Uno and it worked fine. When I hook it up to the Oak and use the tone library or the buzzer example, I hear some faint clicking from the speaker.

On a whim, I tried sending a PWM signal to the buzzer, and that gave a clear tone.

So, I can make noise with the PWM signal, which is fine, but I'm wondering if someone can tell me what I'm doing wrong. Wrong hardware? (Not a true piezo?) Wrong voltage?

Also, since it does work with PWM, does anyone know of a library like tone() that makes it easy to set frequency/duration but using actual PWM? Or a tutorial that explains how to calculate audio frequency from PWM?

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Piezo buzzer, tone library, and PWM
« Reply #1 on: July 19, 2016, 06:09:23 pm »
In a nutshell, you have microcontrollers (and electronics parts) that either work at 5v or a 3.3v (or 1.8v, or 12v, etc, etc). If you were to use water as a comparison, voltage is the width of the pipe.

A lot of the Arduino stuff is 5v, as the original Arduinos like the Duemilanove, Uno and Mega were all five volt parts. Later ones like the Arduino Due are 3.3v parts. This has the unfortunate side effect if you use a circuit meant for a 5v Arduino Uno with a 3.3v Arduino Due, the magic smoke is released (in most cases, it is destroyed and stops working).

The ESP8266 microcontroller inside the Oak is also a 3.3v part. Even though you can put in up to around 10v at the VIN pin, that voltage will be dropped down to 3.3v as that is what the ESP8266 needs. You then have another issue, which is how much current (with water, the pressure) the Oak can provide on each pin. An Arduino Uno can provide 40ma @ 5v, whereas it appears the Oak may only be able to provide 12ma @ 3.3v (so way less than 1/4 of the amound of power the Uno can provide on a GPIO pin).

So what may be happening is that although the buzzer you are using is able to work down at 3v (3-5v in the datasheet), since it needs up to 35 mA, it just isn't getting enough power to buzz. You may need to use a transistor and resistor as shown on page 2 of the datasheet.

Even with your Arduino Uno, I wouldn't be connecting it directly to the Uno - that particular one is a magnetic component, as such, will a) act as a microphone given loud enough sound, meaning it will push power into the Uno when it's not expecting it, and b) resists the voltage pushed into it, meaning it kicks back (EMF) and can kill an Arduino Uno pin given the right circumstances (the same goes for relays). At least put a diode across the buzzer to stop the EMF backfeed, and a resistor (something around 100 ohm may do) to stop the buzzer trying pull too much current from the Arduino pin.

Having said all of this, I haven't tried putting a buzzer across my Oak, so I will give that a try sometime in the next few days and see what happens ;) An issue on the Arduino ESP8266 github page may be of interest also, as some people were trying to get their buzzers to work also, but that may be unrelated.

emardee

  • Full Member
  • ***
  • Posts: 135
Re: Piezo buzzer, tone library, and PWM
« Reply #2 on: July 20, 2016, 03:22:52 am »
Had you seen this issue on Oak Github?


Suggests known problems with Tone() library on Oak and looks like Erik was waiting for upstream bug fixes before rolling it out to Oak. Might be worth leaving a comment on that github issue as a reminder to Erik, and with your experiences too.

micahwedemeyer

  • Newbie
  • *
  • Posts: 6
Re: Piezo buzzer, tone library, and PWM
« Reply #3 on: July 20, 2016, 08:34:24 am »
Thanks for the tips, especially the note about protecting the Oak from the buzzer. I'll definitely keep that in mind.