Do some library exist for special buzzer

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?

Thank you. Yes, it was something like that I was looking for.

However, with this tone function I cannot see a way to adjust sound amplitude. It seems to use 50 % duty cycle all the time.

This module hasn't controls to adjust the amplitude. The only way to change the volume is to increase and decrease a module power voltage.

Thanks for information.

I noticed this text in reference:

  • 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?

That is correct.
I don't know of a library that allows you to adjust frequency and amplitude. However it is not impossible to do

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.

I would imagine so.

It's a cheap buzzer, what do you expect.

1 Like

I tried two things:

  1. You can reduce volume by reducing duty cycle of the applied signal to the input (active low).

  2. 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.

ToneAC does but uses two Arduino pins
3490992041-toneAC3

Syntax
toneAC( frequency [, volume [, length [, background ]]] ) - Play a note.

source:
https://bitbucket.org/teckel12/arduino-toneac/wiki/Home#!connection-example

Yes, but it do not seem to work. This is the code I tried:

//Tries to make some nice tone generator with timer 2 and repeated 10 kHz call
const byte FrqDivFrom10k = 20;    //10 kHz divided by 20 makes 500 Hz modulation
const byte ShiftAmplitude = 1;
const byte SineTable[] PROGMEM = {
  128,167,203,231,249,255,249,231,203,167,128,89,53,25,7,1,7,25,53,89 };

byte toneState = 0;
byte tonePWM = 255;

void setupNiceTone() {
  TCCR2B = (TCCR2B & 0B11110000) | (0B00000001); //Sets prescaler to 1 (32 kHz) and WGM22 to 0.
  TCCR2A = 0B10000001;  //phase correct. Table 18-4. OC2B disconnected
  OCR2A = 254;
  pinMode(AnalogOutBuzzerPin, OUTPUT);
  };

void niceToneUpdate() {   //supposed to be called at 10 kHz.
  toneState = (toneState+1) % FrqDivFrom10k;
  tonePWM = pgm_read_byte_near(SineTable + toneState);
  tonePWM = tonePWM >> ShiftAmplitude;
  OCR2A = 255 - tonePWM;
  };

This is code is to be used with a real speaker with magnet - right?

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.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.