Share this

Tutorial on connecting a servo motor to a PIC microcontroller

2026-04-06 05:15:01 · · #1

This board belongs to the MSP430 Value Series category, which allows us to program all MSP430 series microcontrollers.

Servo motors and pulse width modulators:

Before going into detail, we should first understand servo motors.

A servo motor is a combination of a DC motor, a position control system, and gears. Servo systems have many applications in the modern world, and therefore come in various shapes and sizes. In this tutorial, we will use the SG90 servo motor, which is a popular and inexpensive type. The SG90 is a 180-degree servo. Therefore, using this servo, we can position the axis from 0 to 180 degrees.

A servo motor has three main wires: one for positive voltage, another for ground, and the last for position setting. The red wire connects to the power supply, the brown wire connects to ground, and the yellow (or white) wire connects to the signal.

Each servo motor operates at a different PWM frequency (the most common frequency used in this tutorial is 50Hz), so please obtain the motor's datasheet to check which PWM cycle the servo motor is operating at.

The frequency of the PWM (Pulse Width Modulation) signal may vary depending on the type of servo motor. The important factor here is the duty cycle of the PWM signal. Based on this load distribution, the control electronics adjust the shaft.

As shown in the diagram below, to move the axis to 9 o'clock, the opening must be 1/18.ie. In an 18ms signal, there is a 1ms on-time and a 17ms off-time.

<

For an axis to move to the 12 o'clock position, the signal's on-time must be 1.5ms and the off-time should be 16.5ms. This ratio is decoded in the servo by the control system and the position is adjusted accordingly. The PWM here is generated using an MSP430.

Required materials:

MSP430

SG90 servo

male-to-female line

Circuit diagram and instructions:

In the MSP430, we have predefined libraries, and the PWM functions are already written in these libraries, so we don't need to worry about the PWM values. You only need to input the angle to rotate the axis, and the rest is handled by these libraries and the microcontroller.

Here, we use pin 6, or P1.4, which is the PWM pin of the MSP430. However, you can use any PIN. There is no need to use the PWM pin for the servo, as all PWM functions are written into the library itself.

The header file used to control the servo is "servo.h".

We will use the Energia IDE to write our code. The code is simple and easy to understand. It's the same as the Arduino version and can be found in the "Examples" menu.

#include

Servo sg90servo; // create servo object to control a servo

int angle = 0; // variable to store the servo position

void setup()

{

Sg90servo.attach(4); // attaches the servo on pin 4 to the servo object

}

void loop()

{

for(angle = 0; angle< 180; angle++) // goes from 0 degrees to 180 degrees

// in steps of 1 degree

Sg90servo.write(angle); // tell servo to go to position in variable 'angle'

delay(20); // waits 20ms for the servo to reach the position

}

for(angle = 180;angle>=1; angle--) // goes from 180 degrees to 0 degrees

{

Sg90servo.write(angle); // tell servo to go to position in variable 'angle'

delay(20); // waits 20ms for the servo to reach the position

}

}


Read next

CATDOLL 108CM Bebe

Height: 108cm Weight: 14.5kg Shoulder Width: 26cm Bust/Waist/Hip: 51/47/59cm Oral Depth: 3-5cm Vaginal Depth: 3-13cm An...

Articles 2026-02-22
CATDOLL 140CM Qing TPE

CATDOLL 140CM Qing TPE

Articles
2026-02-22
CATDOLL 102CM Ling Anime Doll

CATDOLL 102CM Ling Anime Doll

Articles
2026-02-22
CATDOLL EQ (Sleepy Q) 108CM

CATDOLL EQ (Sleepy Q) 108CM

Articles
2026-02-22