How to control the servo with MSP430G2 LaunchPad
[Copy link]
A servo motor consists of a DC motor, a position control system, and a rotation mechanism. Servo motors have many applications in the modern world, so they come in different shapes and sizes. The SG90 servo motor we will use in this article is one of the most popular and cheapest motors. The SG90 is a 180 degree servo. So with this servo we can position the shaft from 0-180 degrees.
Servo motors have mainly three wires, one for power, another for ground and the last one for position setting. The red wire is connected to power, the brown wire is connected to ground and the yellow wire (or white) is connected to the signal.
Each servo motor runs on a different PWM frequency (the most common frequency used in this tutorial is 50HZ), so get the datasheet of the motor to check at which PWM cycle your servo motor operates.
The frequency of the PWM (Pulse Width Modulation) signal can vary depending on the type of servo motor. What is important here is the DUTY RATIO of the PWM signal. Based on this DUTY RATION, the control electronics adjust the axis.
As shown in the diagram below, for the axis to move to 9o clock, the turn on power must be 1/18.ie. 1ms ON time and 17ms OFF time in a 18ms signal.
The high level t occupies the entire period T (20ms)
|
Angle of servo rotation
|
0.5ms
|
0 degrees
|
1ms
|
45 degrees
|
1.5ms
|
90 degrees
|
2ms
|
135 degrees
|
2.5ms
|
180 degrees |
In MSP430 we have predefined libraries and PWM functions already written in these libraries so we don't have to worry about the PWM values. You just put the angle by which you want to rotate the shaft and then it is operated by these libraries and the microcontroller.
Here, we are using PIN 6 i.e. P1.4 which is the PWM pin of MSP430. But you can use any pin number. It is not required to use PWM pin for servo because all PWM functions are written in the library itself.
The header file used to control the servo is "servo.h".
We will use Energia IDE to write our code. The code is simple and easy to understand. It is the same as Arduino and can be found in the examples menu. The complete code is given below, you can edit the code as per your requirement and upload it to your MSP430 development board.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
twenty one
|
#include <Servo.h>
Servo sg90servo;
int angle = 0;
void setup()
{
Sg90servo.attach(4);
}
void loop()
{
for (angle = 0; angle< 180; angle++)
{
Sg90servo.write(angle);
delay(20);
}
for (angle = 180;angle>=1; angle--)
{
Sg90servo.write(angle);
delay(20);
}
}
|
|