Buzzer RAK18001

Hello! I decided to join the community as I have been successfully using Wisblock modules for several months now.
I would also like to apologize for my poor command of English.
And personally thank Bernd Giesecke for the extremely interesting Wisblock API & Wisblock Sensor project. Thanks Bernd!
I currently use Wisblock modules for my work, I do several projects at once.
I would like to share my experience.

I have noticed that it is very often difficult to work with the Buzzer RAK18001 module on the forum (for example, here buzzer-rak18001-weak-audio-output/10014 )

I use this module effectively and can say that it works well. However, there is an unfortunate inaccuracy in the description of the module, which I hope I noticed correctly.

This is the reason for the “bad sound” and “overloads”: this module should be controlled using the PWM library. Otherwise, perhaps the module will heat up, there will be a quiet sound (you need to choose “resonance” according to the datasheet, so that there is a loud sound).

Below I give an example of code that I successfully use with the rewritten Wisblock Sensor libraries and programs (thanks beegee!)
As you can see, I included the library in the code for nRF52-based boards usinghardware-based PWM with Adafruit_nRF52_Arduino core,
written by Khoi Hoang

Of course, any PWM library can be used.

If you control the buzzer with PWM (don’t use tone! This is wrong!), put the sound into resonance, then there will be no quiet sound, no reboot due to high current consumption, and there will be no heating of the buzzer.

Since I’m writing FreeRTOS, posting sample code for the Wisblock Sensor here.
I hope that my advice will be useful to someone, regards!

/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/

#define  BUZZ_DEBUG 1

#if  BUZZ_DEBUG == 1
#include "app.h"
#endif

#if RAK_BUZZ > 1

// soundAlert(1245, 20, 30, 5);

/*
u16 GL_BuzzerAllNotes[] = {
 261, 277, 294, 311, 329, 349, 370, 392, 415, 440, 466, 494,
 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988,
 1046, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1976,
 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951,
 4186, 4434, 4699, 4978, 5274, 5588, 5920, 6272, 6645, 7040, 7459, 7902};
*/
/*
u32 HappyBirthday[] = {
 262, 262, 294, 262, 349, 330, 262,
 262, 294, 262, 392, 349, 262, 262,
 523, 440, 349, 330, 294, 466, 466,
 440, 349, 392, 349};

for(i = 0; i < sizeof(HappyBirthday) / sizeof(u32); i++)
{
 buzzerConfig( HappyBirthday[i] ... );
 delay(500);
 buzzerConfig( BUZZER_VOLUME_MUTE ... );
}
*/


 // C8 - 5th octave "Do"
#define BUZZER_DEFAULT_FREQ (4186)
 // 20ms
#define BUZZER_DEFAULT_DURATION (60)


#define BUZZER_VOLUME_MAX (10)


#define BUZZER_VOLUME_MUTE (0)


/****************************************************************************************************************************
 PWM_Basic.ino

 For nRF52-based boards usinghardware-based PWM with Adafruit_nRF52_Arduino core
 Written by Khoi Hoang and rewritten Renice =)

 Built by Khoi Hoang https://github.com/khoih-prog/nRF52_PWM
 Licensed under MIT license
*****************************************************************************************************************************/

#define _PWM_LOGLEVEL_ 0

// Select false to use PWM
#define USING_TIMER false	// true

#define BOARD_NAME 10

#define BUZZER_QUEUE_LEN 10

#define BUZZER_CONTROL WB_IO1

#if BUZZ_DEBUG == 1
#include "app.h"
#include "nRF52_PWM.h"
// Creates pwm instance
nRF52_PWM *PWM_Instance;
#endif

#define BUZZER_CONTROL WB_IO1

/* Queue structure */

TaskHandle_t BuzzerHandle;
QueueHandle_t xQueueBuzz = NULL;

struct BuzzMessage {
    uint16_t freq;
    uint16_t volume;
    uint16_t duration;
    uint16_t pause;
    uint16_t counter;
} xBuzzerParameters;

void buzzerConfig(float frequencyT, float dutyCycleT)
{
    if (dutyCycleT > BUZZER_VOLUME_MAX)
	dutyCycleT = BUZZER_VOLUME_MAX;
#if BUZZ_DEBUG == 1
    PWM_Instance->setPWM(BUZZER_CONTROL, frequencyT, dutyCycleT);
#endif
}

/* The main task for sound */
void vTaskBuzzer(void *pvParameters)
{
    struct BuzzMessage xParameters;	

    for (;;)
      {
	  if (xQueueBuzz != NULL)
	    {
		if (xQueueReceive
		    (xQueueBuzz, &(xParameters),
		     (TickType_t) 10) == pdPASS)
		  {
		      for (int i = 0; i <= xParameters.counter; i++)
			{
			    buzzerConfig(xParameters.freq,
					 xParameters.volume);
			    vTaskDelay(xParameters.duration);
			    buzzerConfig(1, BUZZER_VOLUME_MUTE);
			    vTaskDelay(xParameters.pause);
			}
		  }
	    }
	  vTaskDelay(100);
      }
}

bool init_buzzer()
{
#if BUZZ_DEBUG == 1
    pinMode(BUZZER_CONTROL, OUTPUT);
    digitalWrite(BUZZER_CONTROL, LOW);
#endif
    xQueueBuzz = xQueueCreate(BUZZER_QUEUE_LEN, sizeof(xBuzzerParameters));

    if (xQueueBuzz == NULL)
      {
	// return false;
      }

    if (!xTaskCreate
	(vTaskBuzzer, "Buzzer", 400, NULL, tskIDLE_PRIORITY,
	 &BuzzerHandle))
      {
	// return false;
      }

    // assigns PWM frequency of 1.0 KHz and a duty cycle of 0%
#if BUZZ_DEBUG == 1
    float frequencyBuzz = 1000.0f;
    float dutyCycleBuzz = 0.0f;
    PWM_Instance =
	new nRF52_PWM(BUZZER_CONTROL, frequencyBuzz, dutyCycleBuzz);
    if ((!PWM_Instance) || !PWM_Instance->isPWMEnabled())
      {
	// return false;
      }
#endif
return true;
}

/* Setting up the sound notification system */
void soundAlert(uint16_t freqT, uint16_t volumeT, uint16_t durationT,
		uint16_t pauseT, uint16_t counterT)
{
    xBuzzerParameters.volume = volumeT;
    xBuzzerParameters.duration = durationT;
    xBuzzerParameters.pause = pauseT;
    xBuzzerParameters.freq = freqT;
    xBuzzerParameters.counter = counterT;
    xQueueSend(xQueueBuzz, (void *) &xBuzzerParameters, (TickType_t) 0);
}

#endif
1 Like

Welcome to the forum @renice123

And many thanks for sharing your RAK18001 code. Will try this definitely to get a louder buzzer.