# Tiva Lab 08: PWM RC Servo Controller

## Overview

• Learn how to configure a PWM signal to control RC servo
• Learn how to calculate the LOAD value and the range of CMP value

# RC Servo

RC Servo is a type of geared of motor that are designed with limited rotation angles like 60°, 90°, 180°, and so on. It is great for beginners who want to make stuff move without building a motor controller with feedback and gear box. Originally used in remote-controlled cars and airplanes. It is controlled by sending PWM signal from microcontroller. The PWM signal tells the servo what position it should move to.

• Servo can be positioned from 0 to 180 degree
• An internal DC motor connected to a potentiometer
• High torque gearing
• Internal feedback circuitry to control motor position

#### Connector and Wire Colors

Most standard RC servos use a standard type of 3-pin plug. The connector is a female, 3-pin, 0.1" (2.54 mm) pitch header. The wiring color used on the servo is not always consistent, there are several color codes at play. But the pins are usually in the same order, just the colors are different.

• Red wire — Servo power (Vservo, battery positive terminal)
• Brown or Black wire — Gorund (GND, battery negative terminal)
• OrangeYellow , white, or blue wire — Servo control signal line (PWM signal)

Please check the datasheet or specs for your servo to determine the proper power supply voltage, and please take care to plug the servo into your device in the proper orientation. Plugging it in backwards could break the servo.

#### Control Signal

The third pin of the servo connector carries the control signal, used to tell the motor where to go. This control signal is a PWM signal. The frequency of PWM is in a range of 40 Hz to 200 Hz. Typically, the servo uses a 50 Hz (20 ms period) PWM signal. The servo position is not defined by the PWM duty cycle, but only by the pulse width. Since the position of the servo is determined by the width of the "on" pulse. The off time can be from 5 ms to 40 ms, without any impact on the position. Different servos may have different minimal and maximal pulse widths. Usually, the pulse width is in the range of 1 ms to 2 ms. The servo will rotate in the clockwise or counter clockwise direction. The direction of rotation of the digital servos can be changed by a specific programmer device. The neutral is defined to be the center of rotation. It is important to note that different servos will have different constraints on their rotation, but they all have a neutral position and that position is always a pulse width of 1.5 ms.

Figure 1: Typicall PWM Signal for the Most of Servos

When the power is turned off, the servo's rotor stays in its position. This means that, when power is turned on again, the controller may not know the rotor's angle. For this reason, it's common to return the servo to the netral position as soon as power becomes avaliable.

Due to the fact that the servo is appropriate for hobbyists, a detailed datasheet may not available. However, the limitiing values for signal width can be determined by the experiment.

Deadband width: Minimum pulse width (in microseconds) that the servo will respond to. determines the minimum move that can specify, 10 μs would be around 1.8º; 1 μs would be 0.18º.

# Tower Pro SG90 9G Micro Servo

RC Servo is a type of geared of motor that can rotate 90 or 180 degrees. It is great for beginners who want to make stuff move without building a motor controller with feedback and gear box. Originally used in remote-controlled cars and airplanes. It is controlled by sending PWM signal from microcontroller. The PWM signal tells the servo what position it should move to.

#### Connection

Most standard RC servos use a standard type of 3-pin plus. The connector is a female, 3-pin, 0.1" (2.54 mm) pitch header. The wiring color used on the servo is not always consistent, there are several color codes at play. But the pins are usually in the same order, just the colors are different.

• Red wire — Servo power (Vservo, battery positive terminal)
• Brown or Black wire — Gorund (GND, battery negative terminal)
• OrangeYellow , white, or blue wire — Servo control signal line (PWM signal)

SG90 servo can rotate approximately 180 degrees (90 in each direction).

#### Wired Connection

Position "0" degree ( 0.5 ms Pulse) is all the way to the left, "90" degree (1.5ms pulse) is middle, "180" degree (2.4ms pulse) is all the way to the right.

## Required Components List

1. Ti Tiva LaunchPad Board: TM4C123GXL or TM4C1294XL
2. Servo motor: Tower Pro micro servo SG90 or other compatible servo motors
4. F-F, M-F, M-M wires

## Circuit / Schematic Diagram

You need to define a GPIO pin from the Tiva launchPad as the PWM output and connect it with the RC Servo control pin. The power connected with the Servo must be provided from the power supply module. Do not connect the power source from your USB port.

## Procedure

The frequency of the PWM signal for RC Servo is 50 Hz, and PWM type is left-aligned PWM.

Check the datasheet, find out the range of PWM pulse widths for the RC Servo that are used in the lab:

PulseWidth0°(Left-Position) = _______ ms;   PulseWidth180°(Right-Position) = _______ ms

#### Calculations

In the first step, you have to know what is the system clock frequency. The default system clock frequency is:

• EK-TM4C123GXL LaunchPad: By default, the Keil C startup.s will change the system frequency to 50 MHz. You need to disable the Clock Configuration to change the clock to 16 MHz. Otherwise, you have to use 50 MHz as the system clock in the calculation when you calculate the frequency for the PWM Timer.
• EK-TM4C1294XL LaunchPad: The system clock is 16 MHz.

The clock frequency for PWM Timer is the system clock divided by the PWM divisor, which is defined in the SYSCTL_RCC register (for TMC123G) or PWM_CC register (for TM4C1294).

Calculate the count value for the PWM signal. This value will be set to the LOAD register, which is 16-bit length only. If the count value you calculated is over 65535 (= 216-1), you need to reduce the frequency of PWM Timer by increasing the value of the PWM Divisor and then recalculate the count value again. The frequency of the PWM output signal for RC Servo is 50 Hz (the period time is 20 ms), therefore the count value for the PWM Timer counting for 20 ms is the period value that is saved in the LOAD register.

Then, you need to calculate the CMP values for 0º (left-position) and 180º (right-position).

$CM{P_{0^\circ }} \ge CMP \ge CM{P_{180^\circ }}$

In your code, the value which is written into the CMP register can not exceed the range between CMP0 and CMP180, otherwise, the servo may be damaged, or can not work.

#### Configurations

Write down the following configuration information into your lab report.

## Lab Experiments

Write a firmware code to rotate the servo back and forth (increase the pulse width by 5% and decrease by 5%). An interval delay of around 100 ms is applied between the changing of the CMP value.

## Lab Questions

1. What is the range of the pulse width and CMP value of the servo you used in this lab?
2. Design a function, uint16_t DegreeToCmp(uint32_t degree), converts the degree of rotation (0 ~ 180) to a CMP value. Show your code on the lab report.
Hint: Using Linear Algebra equation to solve this problem. Check this web site: https://www.mathsisfun.com/algebra/line-equation-2points.html
3. Design a function, uint32_t DutyToCmp(uint8_t duty), converts the duty cycle (0 ~ 100) to a CMP value. Show your code on the lab report.