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

Example code for talking to a servo

Hi

I am looking for some example code that shows best how to talk to a servo.. If there isn't any, any ideas which example project comes the closest ?

Thanks

J

Parents
  • Hi,

    These servos are controlled by sending a Pulse width Modulation (PWM) signal. For the nRF51 the best starting point will be the PWM Library Example. It’s located in the folder

    <SDK_InstallFolder>\examples\peripheral\pwm_library 
    

    Try with a period of 20000L, and set the duty cycle to 5% and 10% for min and max servo position.

    Code snippets:

    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(20000L, PWM_PIN);
    
    
    
    
    while (app_pwm_channel_duty_set(&PWM1, 0, 10) == NRF_ERROR_BUSY);
    
  • Try something like this:

    #include <stdbool.h>
    #include <stdint.h>
    #include "nrf.h"
    #include "app_error.h"
    #include "bsp.h"
    #include "nrf_delay.h"
    #include "app_pwm.h"
    
    APP_PWM_INSTANCE(PWM1,1);                   // Create the instance "PWM1" using TIMER1.
    
    int main(void)
    {
        ret_code_t err_code;
        uint8_t SERVO_PIN = 4;
    
        /* 1-channel PWM, 50Hz, output on DK LED pins, 20ms period */
        app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(20000L, SERVO_PIN);
    
        /* Switch the polarity of the first channel. */
        pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;
    
        /* Initialize and enable PWM. */
        err_code = app_pwm_init(&PWM1,&pwm1_cfg,NULL);
        APP_ERROR_CHECK(err_code);
        app_pwm_enable(&PWM1);
    
        uint8_t servo_pos_max = 10;
        uint8_t servo_pos_min = 5;
        while (true)
        {
            /* Set the duty cycle - keep trying until PWM is ready... */
            while (app_pwm_channel_duty_set(&PWM1, 0, servo_pos_max) == NRF_ERROR_BUSY);
            nrf_delay_ms(500);
            while (app_pwm_channel_duty_set(&PWM1, 0, servo_pos_min) == NRF_ERROR_BUSY);
            nrf_delay_ms(500);
         }
    
    }
    
    
    /** @} */
    
Reply
  • Try something like this:

    #include <stdbool.h>
    #include <stdint.h>
    #include "nrf.h"
    #include "app_error.h"
    #include "bsp.h"
    #include "nrf_delay.h"
    #include "app_pwm.h"
    
    APP_PWM_INSTANCE(PWM1,1);                   // Create the instance "PWM1" using TIMER1.
    
    int main(void)
    {
        ret_code_t err_code;
        uint8_t SERVO_PIN = 4;
    
        /* 1-channel PWM, 50Hz, output on DK LED pins, 20ms period */
        app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(20000L, SERVO_PIN);
    
        /* Switch the polarity of the first channel. */
        pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;
    
        /* Initialize and enable PWM. */
        err_code = app_pwm_init(&PWM1,&pwm1_cfg,NULL);
        APP_ERROR_CHECK(err_code);
        app_pwm_enable(&PWM1);
    
        uint8_t servo_pos_max = 10;
        uint8_t servo_pos_min = 5;
        while (true)
        {
            /* Set the duty cycle - keep trying until PWM is ready... */
            while (app_pwm_channel_duty_set(&PWM1, 0, servo_pos_max) == NRF_ERROR_BUSY);
            nrf_delay_ms(500);
            while (app_pwm_channel_duty_set(&PWM1, 0, servo_pos_min) == NRF_ERROR_BUSY);
            nrf_delay_ms(500);
         }
    
    }
    
    
    /** @} */
    
Children
No Data
Related