PWM: Servo motor


This is a sample app which drives a servo motor using PWM.

This app is targeted for servo motor ROB-09065. With the PWM control signal, the servo motor can rotate to any angle between 0 and 180 degrees. The corresponding PWM pulse width is between 700 micro seconds and 2300 micro seconds. The motor is programmed to rotate back and forth in the 180 degree range.

Since different servo motors may require different PWM pulse width, you may need to modify the pulse width in the app if you are using a different servo motor.


BBC micro:bit

You will need to connect the motor’s red wire to external 5V, the black wire to ground and the white wire to pad 0 on the edge connector.

Building and Running

This sample can be built for multiple boards, in this example we will build it for the bbc_microbit board:

Using west:

west build -b bbc_microbit samples/basic/servo_motor
west flash

Using CMake and ninja:

cmake -B build -GNinja -DBOARD=bbc_microbit samples/basic/servo_motor
ninja -C build
ninja -C build flash