You are on page 1of 8

Microprocessors and Microcontrollers Lab Lab#11 Date: 07/12/2021

Microprocessors and Microcontrollers Lab


Lab # 11
Title: Design a system to control the angle of servo motor with PIC16F877A.

Components:
 Breadboard
 LED
 Resistors kit.
 Capacitors Kit.
 Jumper Wires Pack.
 LM7805 Voltage Regulator (5v).
 Crystal Oscillator.
 PICkit2 or 3 Programmer.
 9v Battery or DC Power Supply
Software:
 MPLABX IDE
 Proteus
Introduction:

. Servo Motor .
A servomotor is a rotary actuator or linear actuator that allows for precise control of
angular or linear position, velocity and acceleration. It consists of a suitable motor
coupled to a sensor for position feedback. It also requires a relatively sophisticated
controller, often a dedicated module designed specifically for use with servomotors.
Servomotors are not a specific class of motor, although the term servomotor is often used
to refer to a motor suitable for use in a closed-loop control system.

Saif Tariq UW-19-MTS-BSC-003


Microprocessors and Microcontrollers Lab Lab#11 Date: 07/12/2021

Types of servo Motor


1. DC Servo Motor:
Generally, this motor has a separate DC source in the winding and the armature winding field.
The control can be archived either by controlling the armature current or field current. Field
control comprises some benefits over armature control. Similarly, armature control comprises
some benefits over field control. Based on the uses the control should be functional to the DC
servo motor. This motor offers very precise and also quickly react to start or stop commands
due to the low armature inductive reactance. These motors are used in related equipment’s
and computerized mathematically controlled machines.

2. AC Servo Motor:
This motor includes encoder which is used with controllers for giving closed loop control and
also feedback. This motor can be employed to high accuracy and also controlled exactly as
required for the applications. Often these motors have advanced designs of easiness or better
bearings and some simple designs also use higher voltages in order to achieve greater torque.
AC motor applications, mainly involve in robotics, automation, CNC machinery, etc.

Servo Motor Working


Saif Tariq UW-19-MTS-BSC-003
Microprocessors and Microcontrollers Lab Lab#11 Date: 07/12/2021

An exclusive design of this motor is suggested in controlling applications like the robotics.
Basically, they are used to change the speed control at high torques and correct positioning.
The required parts for this motor are highly developed controller and motor position
sensor.These motors can be classified according the motor controlled by servomechanism.

Applications of Servo Motor:


1. This motor is used to activate movements in robotics for giving the arm to its exact angle.
2. This motor is used to start, move and also stop the conveyor belts carrying the product along
with many stages. For example, bottling, packaging and product labeling,
3. This motor can be used in the camera to set a lens of the camera to enhance the focus of
images
4. This motor is built into the camera to correct a lens of the camera to improve out of focus
images.
5. This motor can be used to control the robotic vehicle by controlling robot wheels,speed,
generating plenty torque to move and also start and stop the robot.

Servo motor use


In modern cars, servo motors are used to control its speed. When stepping on the gas pedal, it
sends electrical signals to the car's computer.The computer then processes that information and
sends a signal to the servo attached to the throttle to adjust the engine speed.

Saif Tariq UW-19-MTS-BSC-003


Microprocessors and Microcontrollers Lab Lab#11 Date: 07/12/2021

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

Saif Tariq UW-19-MTS-BSC-003


Microprocessors and Microcontrollers Lab Lab#11 Date: 07/12/2021

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 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 (0deg position)
 1.4ms of 20ms period is a 7% duty cycle (90deg position)
 2.4ms of 20ms period is a 12% duty cycle (180deg 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.

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

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.

Saif Tariq UW-19-MTS-BSC-003


Microprocessors and Microcontrollers Lab Lab#11 Date: 07/12/2021

Code:
#include <xc.h>

#include "config.h"

#include <stdint.h>

#include "Servo.h" 

uint16_t AD_RES;

void ADC_Init(); 

void main(void) {

  ADC_Init();

  Servo_Init();

  while(1)

  {

  }

  return;

void __interrupt() ISR()

  if(TMR1IF)

  {

    AD_RES = (ADRESH<<2)+(ADRESL>>6);

    CCPR1 = 5.2786*AD_RES + 7335;

    CCP1CON = 0x00;

    PWM1 = 1;

    CCP1CON = 0x09;

    TMR1IF = 0;

    GO_nDONE = 1;

    TMR1 = PreLoad; // Timer1 Preloading

Saif Tariq UW-19-MTS-BSC-003


Microprocessors and Microcontrollers Lab Lab#11 Date: 07/12/2021

  }

void ADC_Init()

  ADCON1 = 0x0D; // Ref V+ is VDD and Configure pin as analog pin

  ADCON2 = 0x3E; // Left Justified, 20Tad and Fosc/64.

  ADCON0 = 0x05; // Select AN1

  ADRESH=0;

  ADRESL=0;

  __delay_us(30);

  GO_nDONE = 1;

Saif Tariq UW-19-MTS-BSC-003


Microprocessors and Microcontrollers Lab Lab#11 Date: 07/12/2021

Hardware:

Conclusion:
The purpose of this lab is to learn about the angle of a servo motor with pic16f877a, To control
the working of a servo motor when interfaced with PIC16F877A microcontroller. It has a very
simple circuit diagram.  The control wire of the servo motor is directly connected to the RB0 pin
of the microcontroller. This pin will provide the required angular displacement of the motor. A
microcontroller generates a PWM signal using a timer, and subsequent configurations can be
modified to rotate the arm of a servo, a specific class of motor often used to create closed-loop
control systems in industrial robots, CNC machinery, and automated manufacturing, We
successfully implemented our code onto MP-lab for the for angle control of servomotor.

.END.

Saif Tariq UW-19-MTS-BSC-003

You might also like