Library 42- Control RC servo with STM32F4

Library 42 allows you to simple connect and interface RC servo motors with STM32F4. They are small, cheap motors, commonly used for RC (Radio Control), small scale robotics and more.

Servos are really simple to interface. They expect 50Hz signal on the input. According to the pulse length (duty cycle) they rotate between 0 and 180 degrees. If signal has 1ms pulse length, motor rotation will be 0 degrees and if signal has 2ms length, then it will have 180degrees rotation. Any pulse length between will set degrees linear to the value.

Equation for pulse length to get specific rotation is:

Pulse length (degrees) = (MAX – MIN) * degrees / 180 + MIN

where:

  • MAX: maximum pulse length for servo, 2000us
  • MIN: minimum pulse length for servo, 1000us

If you want to get rotation in degrees from know pulse length:

Degrees (pulse_length) = (pulse_length – MIN) * 180 / (MAX – MIN)

Library

Features

  • Control RC servo motors
  • Number of available servos depends on STM32F4xx chip you will use
    • More timers means more servos possible

Dependencies

  • CMSIS
    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
    • STM32F4xx TIM
  • TM
    • TM PWM
    • TM TIMER PROPERTIES
    • defines.h

Pinout

Pinout is the same, as in my PWM library. To get full table of pins, check it here.

MIN and MAX

By default, MIN and MAX pulses are set in my library. They are set to work with most servos, so between 1000us and 2000us. Some china servos (fake) are going also from 700us to 2300us, so you can test your servo for proper values. If you need to set your custom min and max pulses, then you can edit them. Add lines below in your defines.h file and edit them.

Functions and enumerations

Example

Example below produces:

  • 2 servos
    • One on pin PA5
    • Second on pin PB3
  • First servo rotates from 0° to 90° and then to 180° each 2 seconds
  • Second servo rotates from 180° to 90° and then to 0° each 2 seconds

Project is available on Github, download library below.

tilz0R

Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...