1884 views|0 replies

6593

Posts

0

Resources
The OP
 

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; // 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
}
}
This post is from Microcontroller MCU
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list