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);
    
Reply
  • 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);
    
Children
  • Hi

    I am using the following code with no luck at all.. The servo refuses to move. I am 100% certain the hardware connections are correct and the servo does work.. Any ideas ? Here is my code:

    BTW, I am using SDK 11.

    int init_servo2(void)
    {
    	ret_code_t err_code;
    
    	/* 2-channel PWM, 200Hz, output on DK LED pins. */
    	app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(20000L, SERVO_PIN);
    
    	/* 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);
    
    	uint32_t value;
    	while(true)
    	{
    		for (uint8_t i = 0; i < 40; ++i)
    		{
    			value = (i < 20) ? (i * 5) : (100 - (i - 20) * 5);
    
    			ready_flag = false;
    			/* Set the duty cycle - keep trying until PWM is ready... */
    			while (app_pwm_channel_duty_set(&PWM1, 0, 10) == NRF_ERROR_BUSY);
    
    			/* ... or wait for callback. */
    			while(!ready_flag);
    			APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 1, value));
    			nrf_delay_ms(125);
    		}
    	}
    
    }
    
  • 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);
         }
    
    }
    
    
    /** @} */
    
  • Did you change the SERVO_PIN to the pin that you are using? I used pin 4 in the code I posted. You also have a nRF52-DK? If you run the code only on the DK, does it work then?

  • I had originally written that it was not working. But now with closer inspection something is happening. I have to put my ear to the servo and I can hear it clicking.. It's not moving, but it's clicking.. So I think we are in the right direction.

    Any ideas how to get it to actually move ?

Related