PinMode help and the Buzzer of hell!

Hi All…

I made the “clever” decision to wire a buzzer’s positive pin in a PCB straight to VCC, and the ground PIN straight to A0 of a Photon.

Here’s the issue. If I set the PIN to INPUT or not set anything at all in the pin mode, it will silence the BUZZER.

I am guessing this is because the pin is floating. However if I set it as OUTPUT, it will BUZZ No matter what I write to the PIN.

Dumb me for setting this configuration, I was trying to be clever and not to pull more current than necessary from the Photon unnecessarily and never thought about it much after a drew it.

Is there a way to set a PIN Write to Floating (High-Low-Floating) to silence the buzzer without having to configure the pin mode differently (INPUT)?

Any suggestion would be greatly welcomed!

Nope!

But what’s wrong with altering the pinMode()?

On the other hand, are you sure you want 5V -> buzzer -> 3V3 A pin?


Not quite. The actual reason is that the pin is set to high impedance which allows (nearly) no current to flow.
But in OUTPUT you do allow current to flow (either 5V -> buzzer -> GND or 5V -> buzzer -> 3V3).

It clearly was a mistake. What my clever mind thought was 5V -> Buzzer -> GND or 5V -> Buzzer -> Float.

If I use as a hack the PinMode(Input) I guess this sets as you stated the pin in high impedance. If every time I want to buzz, I do :
->PinMode Output
->DigitalWrite(Low)

And When I want to stop I do :
-> PinMode Input

Can this be a feasible solution? Or can I hurt something?

I didn’t want to use the PinMode, because I don’t know how much overhead it uses and it seemed wrong. But I guess is negligible and since we are starting from a bad design or a flaw, maybe its not that important :smiley: and a feasible solution.

Just Curious… Is there A way to set the Pin to go to High Z instead of switching from Input to Output?

Thanks much for the help!

That’s fine, you don’t even have to digitalWrite() each time, since the previous state will be reestablished anyhow.

Theoretically, yes - by use of a “remap” function for the GPIO which also requires highZ, but the direct way via INPUT is the best.
If you want to cut the overhead and don’t need the sanity checks of pinMode() you can use some closer to bare metal functions exposed by the HAL.
But unless required for extreme speed (which I’d not see when using a buzzer) I’d advise against it.

@frlobo, what is the expected current draw from that buzzer? And may I ask why you would need high speed I/O to a buzzer (vs a speaker)?

1 Like

I picked a buzzer for simplicity of not having to add amplifier of the speaker. (Am I missing something obvious?)

I wanted the speed to produce some RTTL sounds like simple ring tones, however I don’t know if with this configuration of pins its even possible anymore.

Thanks for all the help

Il get back to you on the current draw but I don’t expected to be much. It’s a small buzzer …

@frlobo, a buzzer “buzzes” and nothing else. This is the fundamental difference between a buzzer and a speaker. A buzzer is a fixed tone annunciator which you turn on or off. No PWM, no RTTL, just buzz on or buzz off ( :flushed:). A speaker needs to be driven with a signal of fixed frequency and amplitude if it is to do the same thing as the buzzer. However, the speaker can also be driven with PWM or RTTL or a DAC to create a rich audio output.

I hate to say it but your choice of a buzzer and how you hooked it up was, well let’s just say, less than desirable (ok, absolutely not desirable!). Furthermore, you have nothing to limit current! I recommend you remove it entirely and rethink what you are trying to do. Even with a speaker, the 20ma max from the GPIO pin is not very loud. So an amplifier my be in order.

What are the piezo elements you ship with your original spark core kits. Are they buzzers? Or speakers?

If it makes a continous sound when only supplied with DC it’s definetly a buzzer and you won’t have any real control over the sound/frequency it produces.

@frlobo, the kit contains a buzzer:

Add an audible feedback to your project with this buzzer. The longer lead is positive and the shorter is negative. You will need a transistor to drive it.
Note: The sound gets annoying after a while. Use it sparingly!
Operating Voltage: 4.0 to 7.0 V DC
Oscillation Frequency: 2.3KHz
Current: 30mA
Sound Pressure: 85dB

Notice that it draws 30ma. Without a current limiting resistor it will most likely damage your Core/Photon GPIO pin. I suggest you take it out of your circuit.

Thank you peakay. I am working on the updated schematic. Can you suggest the best way to include a buzzer in my circuit? I want to do it right.

But for this circuit where the positive goes to the VCC and negative to the Photon, will this damage my circuit with 30mA? (By switching the high z mode on/off)?

Just curious for a temporary solution if feasible and in the future to make the right design.

I have to say that I have used this piezo buzzer and have been able to modulate an RTTL sound in the past!! Maybe wrongly and as a side effect. But it worked… good to know that this is not the right way to do it.

Thanks for the tips… This is an extremely great community!

[quote=“frlobo, post:11, topic:14338”]
But for this circuit where the positive goes to the VCC and negative to the Photon, will this damage my circuit with 30mA?
[/quote]According to the specs, current source and sink is 25 mA max. So you don’t get any advantage using the pin as current sink.

I suggest using a transistor to connect the buzzer. You will control it using LOW / HIGH signal (keeping the pin as output), similar as controlling a LED.

See this link - it talks about 12V buzzer, but you can use it with your setup.
https://startingelectronics.org/beginners/circuits/arduino-buzzer/

1 Like

@peekay123 I thought it was possible to get a buzzer to play a specific tone? I was under the impression I’d done that on a previous (arduino) project.

More likely that was a piezo speaker where the input can be modulated. A regular speaker is also simple - just drive it with a transistor for improved volume.

2 Likes

I don’t know if what shipped in the original builders kit in the Spark Core was a piezo or a buzzer. But I did got it to play RTTL tones. If i put a 1 to the output the device beeps… if I modulated, I can on a limited basis get it to play some tones.

Easiest way to tell is touch it to a battery. If it clicks, it doesn’t have a tone generator built in.

Thats a good idea :smile:

In the case of the buzzer that came with the kit… Id buzzes… And I can make tones with it!! Maybe as a side effect though!

Yeah, it probably produces the right frequency as long as it’s faster than the oscillating frequency. One time I was driving an LED with a variable PWM and worked fine until I got it down to about 2Hz and lower. It took hours to figure out it was a blinker LED. :smile:

I’d agree with @CuriousTech.

@frlobo, I think you need to picture two signals and their sum signal: The native frequency of the buzzer (e.g. 2.3kHz) and your GPIO signal which “modulates” the base (carrier) frequency will produce something very different from either of them but still carrying the characteristics of both.

Usually carrier frequencies should be at least 40kHz to avoid/reduce audible distortions but with e.g. 2.3kHz your GPIO signal will always be distorted in a very noticable way.
So if you intended to play a tune, it might sound similar but never good.
On the other hand, if you intend to emulate something like a MFDT signal that should be recognized by a machine, you’re at a loss with this buzzer.

2 Likes

Actually, I just needed a buzzer… So everything else was a side effect…

I am waiting for a new pcb with the fixes suggested here including the transistor for the buzzer…

Thanks for the help!

1 Like