Skip to content

PWM-Controlled Servo Motor Example

A servo motor is a type of rotary actuator that allows precise control of angular position. It is commonly used in robotics, RC vehicles, and automation systems. The motor has an internal servomechanism that provides positional feedback so that the servo may know what position its shaft is in. A servo motor operates by receiving a PWM (Pulse Width Modulation) signal(period and duty-cycle) which determines its rotational position.

Important

For this demonstration, we use the TowerPro 9g SG90 servo motor. This selection is solely for demonstration purposes; we do not endorse or provide purchase information for this specific module. Users may use similar servo motors but must understand their specifications and adjust the source code accordingly.

Specification of TowerPro MicroServo 9g SG90

Modulation: Analog
Torque: 4.8V: 25.0 oz-in (1.80 kg-cm)
Speed: 4.8V: 0.10 sec/60°
Rotational Range: 180°
Pulse Cycle: ca. 20 ms
Pulse Width: 500-2400 µs

Hardware Connection

To connect the SG90 servo motor to your board, please follow these wiring instructions:

SG90 Servo Pin Signal Board Pin
Orange PWM pin-32
Red VDD (5V) pin-2
Brown GND pin-14

Ensure the connections are secure before proceeding to the software setup.

Running the Example

Clone the Example Code

git clone https://github.com/kneron/kneopi-examples.git
cd kneopi-examples
cd peripherals/C/pwm_servo_control

Build the Code

bash build.sh

#Or manually compile it with GCC
# gcc -o servo_control servo_control.c

Run the Example

./servo_control

Upon running the program, the servo will rotate from 0 degrees to 180 degrees in steps, then return to 0 degrees.

Additional Notes

  • If you are using a different servo motor, refer to its datasheet and adjust the PWM signal parameters accordingly.
  • Ensure that your board provides sufficient power for the servo motor; an external power source may be required for high-power servos.