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);
    
  • 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);
    		}
    	}
    
    }
    
Reply
  • 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);
    		}
    	}
    
    }
    
Children
No Data
Related