I recently received some electronics from China, and I got two small buzzers, because I thought, that I might use them, and their cost was like nothing. One looks like this:
It turns out, that they are not like a "normal" buzzer. When I/O is low, the PCB consume 100 mA and you get no sound. It is like you can turn on a pull in the membrane when I/O is low. Then I tried to supply I/O with a PWM signal, and then you got the sound from the PWM frequency, that in my case was 490 Hz.
So I guess, that you can control it a bit like a class D amplifier.
I like to be able to make some different short tone signals to provide user information. I use an Arduino Nano V3.
Have you seen some libraries that might make it easier to control such a device?
This function is non-blocking, which means that even if you provide the duration parameter the sketch execution will continue immediately even if the tone hasn’t finished playing.
Does it mean, that somehow the "system" with perhaps interrupts take care of stopping the tone?
Yes, I guess it might be possible to make some correction to the duty cycle. It will however also cause the sound to include more harmonics, and the sound becomes not so "nice".
I guess, that if you need to provide a "nice" sound with less harmonics, it can be hard to avoid blocking code.
You can reduce volume by reducing duty cycle of the applied signal to the input (active low).
I tried to apply a PWM signal to a 32 kHz carrier frequency. So the pulse width was modulated by a 500 Hz signal. This way did not provide any 500 Hz audio at all. I guess this result has to do with some nature of the piezoelectric audio device.
In theory, if you want to generate a 500Hz tone then you modulate the 32kHz PWM signal with the 500Hz signal and change the duty cycle of the 32kHz signal in order to adjust the amplitude.
Seems that way. As I said (and to save @jim-p the bother of looking for a library that has a couple extra features in case he was curious), it is just a library that provides an alternative way to generate tones with an Arduino.
Will it work with your module? I don't know.
I just like to comment, that the tone function do work with this cheap buzzer module, but there is an issue with power consumption. This buzzer PCB is active low on the I/O pin, so when input is low the module load current is 110 mA from 5 V power supply. When the tone function finish a tone, that pin ends low. Therefore the module will get hot and dissipate a power loss of 550 mW, when not used.
I noticed a few previous Arduino links about this module:
As the one link stated, setting the output pin HIGH solves the problem but only if you stop the tone using noTone(). Makes the duration parameter useless.