MCU 101: Pulse Width Modulation for Servos

Showing results for 
Search instead for 
Did you mean: 

MCU 101: Pulse Width Modulation for Servos

No ratings

MCU 101: Pulse Width Modulation for Servos


Servo_TearDown 005.jpg

Servos are specialized dc motors geared to produced high-torques and set at specific angles vs rotating continously. The ability to position the servo at a specific angle over and over makes them ideal for robotics, radio controlled car and other various applications. A typical servo will have range of motion from 180-270 degrees. Most modern servos have a three wire interface, red (V+), black (ground), and white (control). To control a servo you must send it a variable length commands (pulse) in 20ms increments. This type of control is called Pulse Width Modulation.

Pulse Width Modulation is a square wave with a set period. By changing the width controlling the proportion of on versus off time, you can obtain a digital ratio from 0-100%. That ratio of on versus off time is called the duty cycle. A microcontroller generates a PWM signal using a timer. The time from the beginning of one sequence to the next is called the period.

The main timer registers include: Counter, Modulo, Count Initialization Value, Channel Value, FTM Status & Control, and Channel Status & Control. The Counter will count up from the Count Initialization Value and reset after reaching Modulo. One tradeoff of the design is the Modulo value we set. It represents the count value of a full duty cycle and also the resolution of our servo control. Setting a higher Modulo value allows for more precise variation in the servo, i.e. more accurate steering. The downside is that a higher value requires more time per cycle.

It is necessary to configure a timer module for the drive motor separate from the servo because they each require different clock frequencies.

Another tradeoff of pulse width modulation is whether it is edge-aligned or center-aligned. Edge-aligned PWM, where the channel is cleared at counter overflow and set at channel match, is simpler to implement in hardware. Center-aligned PWM, where the counter counts up and down, is more difficult to implement but does not give as much noise interference when the channel matches.

Servos have 3 wires coming out of them:

Ground: Black, Brown
Power: Red
PWM Signal: White, Yellow, Orange

Spec Sheet for Servo used in Freescale Cup


Creating the PWM Signal

Much of what is needed to create this signal is discussed in the Motor Control tutorial. Click here to review how to configure a PWM signal on your microcontroller.

The same microcontroller configuration utilized to drive a motor can be modified slightly to rotate the arm of a servo. Since the Servo and motor require different clock frequencies, it is necessary to configure a timer module for the servo separate from the motor. Freescale Cup participants will configure the timer modules to output signals that control a steering Servo via varying the Duty Cycle of a PWM signal.

Microcontroller Reference Manual: Timer Information

You will find high level information about Timer usage in several different areas of a reference manual. See the reference-manual article for more general information.

  • Relevant Chapters:
    • Introduction: Timer modules - lists the memory map and register definitions for the GPIO
    • System Modules: System Integration Modules (SIM) - provides system control and chip configuration registers
    • Chip Configuration: Timers
    • Signal Multiplexing: Port control and interrupts

Methods of controlling steering angles

Construct a look-up table

One way of controlling the steering angles is to construct a look-up table. The input of the look-up table can be the shift distance(in pixels) from the center, and the output could be the steering angles. The look-up table can be put into an excel file. So when you want to use it, just copy and paste the table into your code file.
Here is an example of how to construct a look-up table.

1. Set up basic parameters of your car: height of camera(h), angle of camera(theta), velocity of car(v), servo delay(s)..
2. Draw a graph to help you develop a function between your input parameters and your output steering angles
3. Put all paraments into excel. So if you want to change any parameters in the future it will be very convenient.
4. Copy and paste look-up table into code file

Note: depending on how you define your parameters, the look-up table may not work as well as you expected. Experiments show that the look-up table works well when the shift distance is small( small turns) and the car tends to go off track when the shift distance is big(sharp turns).

Poportional Control (P Control)

You can map your servo angle based directly on your line location. Take the derivative of the camera signal and use the derivative peaks as the edges of the line. Take the location of each peak and subtract them from each other to get the line width. Taking the min line peak plus line width will give you the location of the line. Now take that location and map it to your servo. We made Camera.Lock = loc and using this we made Motor.ServoAngle = Camera.Lock»1; This made our line location map directly to our servo and it seemed to work well for us.

Additional Theory Training Resources

Freescale Motor Control Tutorial

Freescale Lecture 1: Introduction and Motor Basics
Freescale Lecture 2: Pulse Width Modulaiton

Freescale Lecture 3: Control Design

Freesacle Lecture 4: Speed and Position

Freescale Lecture 5: MPC5607B Overview

Labels (1)
Version history
Revision #:
1 of 1
Last update:
‎07-12-2012 09:12 AM
Updated by: