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

How can I fix the noise from the buzzer(PWM)?

Hello

I'm using a passive buzzer controlled by PWM.
When PWM is set and run, the buzzer will continue to output small noises.

This is probably a code problem. Below is my PWM code

#include "app_pwm.h" 


APP_PWM_INSTANCE(PWM1,1);                   // Create the instance "PWM1" using TIMER1  (name, NRF_DRV_TIMER_INSTANCE(num)).

void pwm_play_buzzer_duration(uint32_t duration) //button buzzer
{
    uint8_t i;
    uint32_t duty = 50;

    NRF_LOG_INFO("pwm_play_buzzer_duration =>");    
    ready_flag = false;

    //Set the duty cycle - keep trying until PWM is ready... 
    while (app_pwm_channel_duty_set(&PWM1, 0, duty) == NRF_ERROR_BUSY);

    // ... or wait for callback. 
    //while (!ready_flag);
    //APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 1, duty));
    nrf_delay_ms(duration);
    app_pwm_disable(&PWM1);
}


void pwm_ready_callback(uint32_t pwm_id)    // PWM callback function
{
    NRF_LOG_INFO("pwm_ready_callback =>");
    ready_flag = true;
}

/*
 init, Once executed
 */
//buzzer PWM initializing
void pwm_init(void) 
{
    ret_code_t err_code;

    /* 1-channel PWM, 200Hz, output on DK LED pins. */
    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(1500, BUZZER_PIN); //original 5000L
    
    /* Switch the polarity of the second channel. */
    pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
    
    /* Initialize and enable PWM. */
    err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
    APP_ERROR_CHECK(err_code);
    app_pwm_enable(&PWM1);

    printf("Start PWM\n");
}


/*
 pwm_change_frequency,Can be executed multiple times
*/
void pwm_change_frequency(uint16_t period_us) // ex) pwm_change_frequency(500);
{
    ret_code_t err_code;

    app_pwm_disable(&PWM1); //disable PWM for change PWM
    app_pwm_uninit(&PWM1); //uninitializing a PWM

    /* 1-channel PWM, 200Hz, output on DK LED pins. */
    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(period_us, BUZZER_PIN); //original 5000L
    
    /* Switch the polarity of the second channel. */
    pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
    
    /* Initialize and enable PWM. */
    err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
    APP_ERROR_CHECK(err_code);
    app_pwm_enable(&PWM1);
}


static void do_play_buzzer(void)
{
  pwm_play_buzzer_duration(200); //buzzer play time
  app_pwm_enable(&PWM1);
  //printf("Play buzzer\n");
}

What is the PWM noise related to?

Thank you.

  • Hi,

    Not sure I understand your question. If you have configured PWM to output a singla to the buzzer, and started it, would you not expect it to make sound?

    When PWM is set and run, the buzzer will continue to output small noises.

    In the end of pwm_play_buzzer_duration(), it looks like you tried to stop the PWM, but then it is enabled immediate afterwards inside  do_play_buzzer().

    Best regards,
    Jørgen

Related