Servo Motor Control With PIC Microcontroller – 16Bit PWM PT3

Previous Tutorial Previous Tutorial Tutorial 32 Next Tutorial Next Tutorial
Servo Motor Control With PIC Microcontrollers | 16Bit PWM
Intermediate Level ★★☆☆☆

 

Servo Motor Control Tutorial – PART 3

Servo Motor Tutorial PT3 Thumbnail

 

In this Article / Tutorial, we’ll discuss how to control servo motor with PIC microcontroller using 16-Bit High-Resolution PWM signals. This depends on a technique proposed in an earlier article that utilizes the CCP Compaer+Timer module in order to create a high-resolution PWM signal @ any desired frequency.

This technique will dramatically increase our control precision over the servo motor angular position as we’ll see in a couple of LABs hereafter in the article.

Without further ado, let’s get started!


   Required Components For This Tutorial   

 

Qty. Component Name Buy On Amazon.com
1 PIC18F2550 Add
1 BreadBoard Add
1 Micro Servo Motor (Metal Gear) Add
1 LED Add    Add
1 Resistors Kit Add    Add
1 Capacitors Kit Add    Add
1 Jumper Wires Pack Add    Add
1 LM7805 Voltage Regulator (5v) Add
1 Crystal Oscillator Add
1 PICkit2 or 3 Programmer Add
2 9v Battery or DC Power Supply Add    Add    Add

My DSO Siglent-SDS1104 (on Amazon.com)

My Function Generator Fy-6900 (on Amazon.com)

*Affiliate Links Disclosure: links will make me a small commission at no additional cost to you*

The Prototyping Board Setup

Prototyping Board - Embedded Systems Tutorials With PIC MCUs

 


   How To Control Servo Motors?   

 

The way servo motors are working is simply by comparing a reference voltage to the actual angular shaft position using a potentiometer attached to the gearbox. The reference voltage can be controlled by sending a 50Hz PWM (pulse-width-modulated) signal to the servo motor. Which in turn changes the reference voltage and the control circuitry steers the motor in the right direction until it reaches the exact required angle position and it keeps holding it while the PWM signal is not changing.

The diagram down below indicates the behavior of the servo motor’s shaft due to the change of the PWM pulse width. Typically the full sweep (0->180°) is achieved by sweeping the pulse width from (1ms to 2ms).

How To Control Servo Motor With PIC Microcontroller

Different servo motors have different ranges for the pulse width of the 50Hz PWM signal which corresponds to the 0° position and 180° maximum angular position. So, it’s important to double-check the datasheet for the specific servo motor you are using.

The range for the PWM pulse width for my servo motors (MG90S) is 0.6ms to 2.4ms for angular motion from 0° to 180°. And that’s what we’ll be using.

 Note That:

 0.6ms of 20ms period is a 3% duty cycle (0° Position), 

 1.4ms of 20ms period is a 7% duty cycle (90° Position), 

 2.4ms of 20ms period is a 12% duty cycle (180 ° Position). 

 


   Servo Motor Control With PIC Microcontrollers   

 

There are different ways to generate the 50Hz PWM signal required by the servo motor using a microcontroller. Speaking about PIC microcontroller, the first thing that should pop-up in your mind is the CCP PWM hardware module inside the microcontroller itself. But it turns out to be a little bit tricky business to get that right. We’ve discussed the reasons for this in the previous tutorial and put it to the test.

 

What Are Some Design Techniques & Solutions To Control Servo Motors?

 

1 CCP PWM Hardware Module & Lowering Down Fosc using LP internal RC oscillator

(This solution was implemented in the previous tutorial PART-1).

2 Software PWM Generation Using Timer or Compare Match Interrupts

(This is the solution was implemented in the previous tutorial PART-2).

3 Using PCPWM module in Microchip PIC Microcontrollers

The Power Control PWM module from Microchip is available in a number of microcontrollers dedicated to motors and power control applications. Embedded systems designers for these sorts of applications are more familiar with this module and the dsPIC part with sophisticated PWM generation hardware.

The PCPWM hardware module is controlled by a total of 22 registers. And it has a larger time base registers and a very wide range of Prescaler values that makes your task much easier. The output channels may be up to 6 or 8.

4 Using NCO (Numerically Controlled Oscillator) Module

The NCO module from Microchip is available in too many microcontrollers especially the 8-Bit parts. This intelligent hardware module is capable of LINEARLY sweep over a wide range of frequencies and it can reach a resolution of 1/Fosc. I just can’t emphasize more how powerful and elegant this module is! It’s definitely a huge topic to be discussed in the future.

And it can potentially be the solution for a problem like a servo control. And let me show you how to do this but in another article!

PCBgogo Ad

5 The CCP Capture + Timer1 = 16-Bit PWM!

(This is the technique we’ll be implementing today in this tutorial).

Theoretically, the 16-Bit Timer1 module can be combined with the CCP Capture module and use the CCPR1 16-Bit register pair to work as a Duty Cycle controller. We’ll end up having a 16-Bit of resolution PWM signal with a wide band of frequency control!

This means, we can effectively push the CCP PWM beyond its fundamental limits in terms of 10-Bit resolution, so we end up with a x64 times more levels of resolution in the duty cycle which will potentially increase our control ability over the servo motor’s angle!

Read The Article For High-Resolution (16-Bit) PWM Generation With Capture+Timer Technique

 

 

Well, I think there still be other solutions like using external I2C servo drivers and maybe combining CLC with timers. The list goes on, that’s enough for this section. Other solutions will be discussed and hopefully implemented in future articles. If you’re interested in that, please drop me a comment! I’d really like to know about this!

 


   Servo Motor Control With High-Resolution PWM   

 

Prerequisites

1 Timer Modules Tutorial

2 Timer Preloading

3 CCP Capture+Timer 16-Bit PWM Article

4 ADC Tutorial

5 Servo Motor Control PART-1

6 Servo Motor Control PART-2 (the previous tutorial)

How To Generate Servo Motor Control Signal?

Using the CCP Compare+Timer technique for generating a high-resolution PWM is not so difficult. We only need to find out the timer preload value using the formula down below. Assuming Fosc=48MHz, Prescaler=4, Desired FPWM=50Hz. Therefore, we can solve for TMR1 (the value to be preloaded)

High Resolution PWM 16bit Frequency Equation

50 = 48000000 / 4 x 4 x (65536-TMR1)

TMR1 = 5536 (the preload value)

Now, it’s time to figure out how to get the desired duty cycle as well. The duty cycle in order to be in the range of 3% up to 12% for a full range rotation (0° up to 180°), we need to write the convenient data to the CCPR1 register pair.

By simple calculations & mapping, it turns out that the CCPR1 values which correspond to duty cycle ranging from 3% up to 12% falls within the range (7335 for 3% DC up to 12735 for 12% DC). And any value in between can be easily found by mapping.

Mapping Duty Cycle To CCPR1

Now, we can set the frequency of the PWM to 50Hz and move the duty cycle in the range from 3% up to 12% so we’re almost ready to test out this technique on a servo motor. I’ll create a couple of LABs down below to highlight the strength of this technique.

 


   Servo Motor Sweep 16Bit PWM – LAB6   

 

Lab Name High-Resolution 16-Bit PWM – Servo Motor Sweep
Lab Number 43
Lab Level Intermediate
Lab Objectives Learn how to generate software PWM signals @ 50Hz and control the position of a servo motor.

 

       1. Coding       

 

Open the MPLAB IDE and create a new project name it “Servo_16Bit_PWM”. If you have some issues doing so, you can always refer to the previous tutorial using the link below.

Create New Project With MPLAB IDE

Set the configuration bits to match the generic setting which we’ve stated earlier. And if you also find troubles creating this file, you can always refer to the previous tutorial using the link below.

Now, open the main.c file and let’s start developing the firmware for our project.

The full code listing for this lab is as follows

 

Project Files Download

 

       2. Simulation       

 

Servo Motor Sweep 16Bit PWM Simulation

 

       3. Prototyping       

 

Connect the Brown wire to GND and the Red to Vdd and the Orange to RC2 (CCP1 PWM Pin). And turn on the power supply. The 50Hz PWM signal is captured by my DSO (digital storage oscilloscope).

Here is a short demo video for the running test for this LAB

 

Play Video On YouTube

 


   Servo Motor Manual Knob – LAB7   

 

Lab Name Software PWM – Servo Motor Knob
Lab Number 44
Lab Level Intermediate
Lab Objectives 16-Bit PWM generation technique to generate PWM signals @ 50Hz and control the position of a servo motor & a pot connected to the ADC.

 

In this LAB, we’ll replicate the Arduino servo example that uses the ADC and potentiometer to manually control the position of the servo motor using the analog input. We’ll read the AN1 channel, then the AD_RES result will get mapped to fit in the range of the servo PWM duty cycle 3% to 12%.

 

       1. Coding       

 

Open the MPLAB IDE and create a new project name it “Servo_16Bit_PWM_Knob”. If you have some issues doing so, you can always refer to the previous tutorial using the link below.

Create New Project With MPLAB IDE

Set the configuration bits to match the generic setting which we’ve stated earlier. And if you also find troubles creating this file, you can always refer to the previous tutorial using the link below.

Now, open the main.c file and let’s start developing the firmware for our project.

The full code listing for this LAB is as follows

 

Download Project Files

 

       2. Simulation       

 

 

       3. Prototyping       

 

Play Video On YouTube

 


   Concluding Remarks   

 

  1  

The technique used in this article is advantageous in many applications but it can also be expensive in terms of hardware for some other situations. But all in all, it’s definitely worth it when you need such high resolution.

  2  

You can use the same timer module with multiple CCP modules in order to add some more servo motors and keep them in sync (as long as they’re using the same timer module as a base).

  3  

There still some other methods and techniques to control servo motors. Each one comes at a cost and depending on your situation, you should make the choice!

Therefore, PART3 is the last part of servo motor control. And if you still have any questions, then please leave me a comment down below and I’ll consider updating these articles to clarify any ambiguity.

  4  

Don’t forget to SHARE these articles/tutorials. And let me know in the comments what do you think? Do you have any other ideas? maybe questions or problems?

I’ll be always here for help!

Regards ^^

Khaled M.

 

 

 

 

Previous Tutorial Previous Tutorial Tutorial 32 Next Tutorial Next Tutorial
Share This Page With Your Network!

Khaled Magdy

I'm an embedded systems engineer doing both Software & Hardware. I'm an EE guy who studied Computer Engineering, But I'm also passionate about Computer Science. I love reading, writing, creating projects and Technical training. A reader by day a writer by night, it's my lifestyle. You can view my profile or follow me via contacts.

You may also like...

Leave a Reply

%d bloggers like this: