This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

BLE Nano/Nordic SDK: making a quick buzz from a piezo speaker

I am working on a project using a ble nano (nrf51822) using the Nordic SDK 8.1. I started building off the ble-app-hrs in the examples of the documentation so my code is pretty similar to the example.
What I am trying to do is to have a piezo speaker make a quick couple of buzzes when a bond is made. To do this I went into the bsp_led_indication() method in bsp.c and added the following code for the BSP_INDICATE_CONNECTED case:

 case BSP_INDICATE_CONNECTED: // This case is used when a connection is made
        printf("BSP_INDICATE_CONNECTED\n");

        COMPONENTS_OFF(COMPONENTS_MASK & ~BSP_LED_0_MASK & ~ALERT_LED_MASK);
        COMPONENTS_ON(BSP_LED_0_MASK);

        beepCount++;
        if (beepCount <= 6) {
            COMPONENTS_INVERT(BSP_BUZZER_MASK);
            app_timer_start(m_leds_timer_id, BSP_MS_TO_TICK(500), NULL);
        }

        m_stable_state = indicate;
        break;

please not that I updated the boards.h file to add the following methods:

` #define COMPONENTS_ON(components_mask) do { NRF_GPIO->OUTSET = (components_mask) & (COMPONENTS_MASK & COMPONENTS_INV_MASK);
NRF_GPIO->OUTCLR = (components_mask) & (COMPONENTS_MASK & ~COMPONENTS_INV_MASK); } while (0)

#define COMPONENTS_OFF(components_mask) do { NRF_GPIO->OUTCLR = (components_mask) & (COMPONENTS_MASK & COMPONENTS_INV_MASK);
NRF_GPIO->OUTSET = (components_mask) & (COMPONENTS_MASK & ~COMPONENTS_INV_MASK); } while (0)

#define COMPONENTS_IS_ON(components_mask) ((components_mask) & (NRF_GPIO->OUT ^ COMPONENTS_INV_MASK) )

#define COMPONENTS_INVERT(components_mask) do { uint32_t gpio_state = NRF_GPIO->OUT;
NRF_GPIO->OUTSET = ((components_mask) & ~gpio_state);
NRF_GPIO->OUTCLR = ((components_mask) & gpio_state); } while (0)`

The problem I have now is that the buzzer will buzz for inconsistent times. for example, some times when a bond is made the buzzer will ring for 2 or 3 seconds before turning off and back on again (even though the delay is set to 500 ms). Is there a better way I can create a buzzing sound than adding a timer that recalls the bsp_led_indication() method? Also is there an easy way I can change the tone or pitch of the buzzer to improve the sound quality (currently the buzzer just makes an awful screeching sound when it is activated... This may just be due to the quality of the piezo speaker I purchased (I got it from radio shack). Is there a better component to make a beep sound?)

  • @sigurd Alright I couldn't find the data sheet, but on the website I found the following info: "This 75dB Piezo electric buzzer operates on 6VDC, carrying a maximum 45mA current with a tame buzzer tone rating of 300-500Hz.". I assume that means that the frequency is between 300 and 500 hz. What values would I pass to APP_PWM_DEFAULT_CONFIG_1CH() to set this frequency? Also is it a problem if I am using a 6v buzzer with a 5v micro controller?

  • The period = 1/frequency, so for 500 hz, period = 1/500 = 0.002 s = 2000 us

    APP_PWM_DEFAULT_CONFIG_1CH(2000L,pin number)
    

    For 300 Hz you need to set the period to 3333L. I think you should be able to drive the buzzer with 3.3V/5V, but you may not get the maximum volume possible. You could also consider using a transistor, see this thread.

Related