1.. zephyr:code-sample:: servo-motor 2 :name: Servomotor 3 :relevant-api: pwm_interface 4 5 Drive a servomotor using the PWM API. 6 7Overview 8******** 9 10This is a sample app which drives a servomotor using the :ref:`PWM API <pwm_api>`. 11 12The sample rotates a servomotor back and forth in the 180 degree range with a 13PWM control signal. 14 15This app is targeted for servomotor ROB-09065. The corresponding PWM pulse 16widths for a 0 to 180 degree range are 700 to 2300 microseconds, respectively. 17Different servomotors may require different PWM pulse widths, and you may need 18to modify the source code if you are using a different servomotor. 19 20Requirements 21************ 22 23The sample requires a servomotor whose signal pin is connected to a pin driven 24by PWM. The servo must be defined in Devicetree using the ``pwm-servo`` 25compatible (part of the sample) and setting its node label to ``servo``. You 26will need to do something like this: 27 28.. code-block:: devicetree 29 30 / { 31 servo: servo { 32 compatible = "pwm-servo"; 33 pwms = <&pwm0 1 PWM_MSEC(20) PWM_POLARITY_NORMAL>; 34 min-pulse = <PWM_USEC(700)>; 35 max-pulse = <PWM_USEC(2500)>; 36 }; 37 }; 38 39Note that a commonly used period value is 20 ms. See 40:zephyr_file:`samples/basic/servo_motor/boards/bbc_microbit.overlay` for an 41example. 42 43Wiring 44****** 45 46BBC micro:bit 47============= 48 49You will need to connect the motor's red wire to external 5V, the black wire to 50ground and the white wire to the SCL pin, i.e. pin P19 on the edge connector. 51 52Building and Running 53******************** 54 55The sample has a devicetree overlay for the :ref:`bbc_microbit`. 56 57This sample can be built for multiple boards, in this example we will build it 58for the bbc_microbit board: 59 60.. zephyr-app-commands:: 61 :zephyr-app: samples/basic/servo_motor 62 :board: bbc_microbit 63 :goals: build flash 64 :compact: 65