Tiva Lab 008: PWM RC Servo Controller

Overview

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

Required Reading Material

Overview

RC Servo

servo samples s

RC Servo is a type of gear motor that is 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 a gearbox. Originally used in remote-controlled cars and airplanes. It is controlled by sending a PWM signal from the microcontroller. The PWM signal tells the servo what position it should move to.

  • The 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.

servo conns s

  • Red wire — Servo power (Vservo, battery positive terminal)
  • Brown or Black wire — Ground (GND, battery negative terminal)
  • Orange, Yellow, 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 backward 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.

pwm 1ms 2msFigure 1: Typical PWM Signal for the Most of RC 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 neutral position as soon as power becomes available.

Due to the fact that the servo is appropriate for hobbyists, a detailed datasheet may not available. However, the limiting 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

sg90 s

RC Servo is a type of geared 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 a gearbox. Originally used in remote-controlled cars and airplanes. It is controlled by sending a PWM signal from the 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, and just the colors are different.

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

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

Wired Connection

sg90 wires s

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


Calculations

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

\({f_{PWM,Servo}} = 50Hz\)

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

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

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).

\({f_{PWMTimer}} = \frac{{SysClk}}{{PWMDivsor}}\)

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 the PWM Timer by increasing the value of the PWM Divisor and then recalculating 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.

\(LOAD = Coun{t_{PWM,50Hz}} = \frac{{20ms}}{{{T_{PWMTimer}}}} = \frac{{{f_{PWMTimer}}}}{{50Hz}} \le 65535\)

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

\(Dut{y_{0^\circ }} = \frac{{PulseWidt{h_{0^\circ }}}}{{20ms}}\)

\(CM{P_{0^\circ }} = LOAD \times (1 - Dut{y_{0^\circ }})\)

\(Dut{y_{180^\circ }} = \frac{{PulseWidt{h_{180^\circ }}}}{{20ms}}\)

\(CM{P_{180^\circ }} = LOAD \times (1 - Dut{y_{180^\circ }})\)

\(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.

Required Components List

  1. Ti Tiva LaunchPad Board: TM4C123GXL or TM4C1294XL
  2. Servo motor: Tower Pro micro servo SG90 or other compatible servo motors
  3. Breadboard, Power adapter, and voltage regulator
  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 breadboard power module. Do not connect the power source from the Tiva board.

Procedure

  1. Create a new folder under the EE3450 folder and name it Lab08_PWMServo.
  2. Launch the Keil μVisio and create a new project. Save the project to the project folder you just created in the previous step and set the project name to Lab08_PWMServo.
  3. Add the Common and ezTivaLIB folder to the include paths which is under the "Options for Target" setting.
  4. Add ezTiva LIB (ez123GLIB.lib or ez1294LIB.lib) into your project, and increase the stack and heap size under the "startup_TM4cXXX.s (Startup)" setting.

Configurations

DevicePort.Pin Signal TypePCTLDirectionDrive Mode

Example Source Code

Lab Experiments

Questions

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


© 2024 Air Supply Information Center (Air Supply BBS)