Psoc 4 : How To Control A Servo

About the project

This is a tutorial about controlling a Servo with PSoC by generating PWM, adjusting Frequency and Duty Cycle!

Project info

Difficulty: Moderate

Platforms: Cypress

Estimated time: 1 hour

License: GNU General Public License, version 3 or later (GPL3+)

Items used in this project

Hardware components

Servo Mount - Servo Plate B Servo Mount - Servo Plate B Micro Servo SG90 Tower Pro x 1
Jumper Wire Kit - 140pcs Jumper Wire Kit - 140pcs x 1

Story

 Controlling a Servo with Arduino is damn easy ! There are libraries to do things, you don't need to know what's going on with the control signal that commands the servo. There is an Arduino tutorial for those who might need

Arduino Servo Tutorial

But how to do that with PSoC 4 ?

According to SG90 Micro Servo (Tower Pro) datasheet, you have to make PWM signal like this to control the Servo -

Servo control pulse

Configuring the PWM module

The answer is TCPWM component, but wait ! How do I make a 50 Hz signal (20 mS) and vary the Duty cycle between 5% (1mS) to 10 % (2mS) ?

Setting up PWM Component

  • By Changing the Value of Period from "1 to 65535", we can vary PWM Frequency.
  • By Changing the Value of Compare from "1 to Period Value", we can vary  Duty Cycle.
  • By Changing the Value of fclk ( Clock signal to PWM component ), we can vary  the Frequency range.
  • The larger the fclk, the higher is the max frequency .

Configuring Frequency and Duty

Formula

Formulas for this Configuration of PWM (Center Aligned i.e. symmetric PWM) -

Period = Fclk / (2 X Desired Frequency )

Compare = Period - ( Desired Duty X Period )

Calculating Frequency and Duty Cycle

Here, Fclk for the PWM Component is chosen 480 kHz,

We need a PWM signal of 50 Hz ( Our Desired Freq )

So, Period = 480000 / ( 2 X 50 ) = 4800

and Compare (min) = 4800 - ( 10% X 4800 ) = 4320 

       Compare (max) = 4800 - ( 5% X 4800 ) = 4560

Varying the Compare value between 4320 to 4560, we can control the servo because it will generate the desired signal required to do the job !

PWM0_WriteCompare(value)  API ( or Function) is used to change the Duty Cycle from C code, which controls the position of the servo !

Visualizing Output Waveform

Important : PWM Pin must be Checked as Hardware Connection !

Pin Driven by Internal H/W

Otherwise , there will be no output !

Servo in Action

Sorry for the crappy quality video. Don't have a better camera right now!

Servo Motion

Schematics, diagrams and documents

Servo connections to PSoC

Code

Main C

Controlling Duty Cycle on the Fly

PSoC Codes and Design Files

PSoC 4 : 4245 AXI/ ARM M0/ Cy8C049 Kit !

Download

Credits

Leave your feedback...