ad space

Controlling Servo Motors with PIC16F877A: Applications in Robotics

 

Servo motors play a crucial role in robotics due to their ability to control angular movement with precision. From robotic arms to remote-controlled vehicles, servos offer the exactness needed for controlled, smooth movements. In this blog post, we'll explore how to control servo motors using the PIC16F877A microcontroller and how it can be applied in various robotic applications.

Why Use PIC16F877A for Servo Motor Control?

The PIC16F877A microcontroller is a popular choice for many electronics projects due to its versatility and ease of programming. It supports PWM (Pulse Width Modulation), a feature crucial for controlling the position of servo motors. This makes it perfect for robotics projects where precise control over angles is necessary.

Understanding Servo Motor Basics

A servo motor has a shaft that can be positioned at specific angles, typically between 0° and 180°. The position is controlled by sending a PWM signal, where the width of the pulse determines the angle of the shaft. A typical servo motor operates based on:

  • PWM Duty Cycle: Controls the position of the motor's shaft.
  • Control Signal: A pulse of 1ms moves the servo to 0°, a pulse of 1.5ms positions it at 90°, and a pulse of 2ms moves it to 180°.

Components Needed

To begin controlling a servo motor with a PIC16F877A, you’ll need the following components:

  • PIC16F877A microcontroller
  • Servo motor
  • Power supply (5V for the servo motor)
  • Crystal oscillator (20 MHz)
  • Breadboard and jumper wires
  • Microcontroller programming board (e.g., PICkit 3)
  • Capacitors and resistors for circuit stabilization

Circuit Setup

To control a servo, you’ll use one of the PWM pins of the PIC16F877A. For this demonstration, we’ll connect the PWM pin (CCP1) to the control pin of the servo motor.

  1. Connect the servo motor to the power supply. The red wire goes to 5V, the black wire to ground, and the yellow (control) wire to the CCP1 pin (Pin 17) of the PIC16F877A.
  2. Set up a 16 MHz crystal oscillator between the OSC1 and OSC2 pins of the PIC16F877A.
  3. Ensure proper grounding for all components.

Circuit Diagram

The following circuit diagram shows how to connect hobby servo motor to PIC16F877A microcontroller.

servo motor pic16f877a circuit diagram

 See also servo motor with arduino and Servo Motor control using Simulink and Arduino for details how PWM signal controls a servo motor.

Writing the Code

In this section, we’ll focus on generating the PWM signal to control the servo. Here’s a simple code to rotate the servo to specific angles using PWM with PIC16F877A:

#include <xc.h>

// Configuration bits
#pragma config FOSC = HS // High-speed oscillator
#pragma config WDTE = OFF // Watchdog Timer disabled
#pragma config PWRTE = ON // Power-up Timer enabled
#pragma config BOREN = ON // Brown-out Reset enabled
#pragma config LVP = OFF // Low Voltage Programming disabled
#pragma config CPD = OFF // Data EEPROM Memory Code Protection disabled
#pragma config WRT = OFF // Flash Program Memory Write Protection disabled
#pragma config CP = OFF // Flash Program Memory Code Protection disabled

#define _XTAL_FREQ 16000000 // Define the crystal oscillator frequency

void PWM_Init(){
PR2 = 249; // Set Timer2 period register
CCP1CON = 0x0C; // Configure CCP1 module for PWM mode
T2CON = 0x01; // Timer2 prescaler = 4
TMR2 = 0; // Clear Timer2
TMR2ON = 1; // Enable Timer2
while(TMR2IF == 0); // Wait until Timer2 overflows
TMR2IF = 0; // Clear overflow flag
}

void Set_Duty(unsigned int duty){
// Split the duty cycle value to upper 8 bits and lower 2 bits
CCPR1L = (duty >> 2) & 0xFF; // CCPR1L gets the upper 8 bits of duty
CCP1CON = (CCP1CON & 0xCF) | ((duty & 0x03) << 4); // CCP1CON lower 2 bits DC1B
}


void main(){
TRISC2 = 0; // Set CCP1 pin (RC2) as output
PWM_Init(); // Initialize PWM

while(1){
Set_Duty(62); // Set duty cycle for 0 degree (~1ms pulse)
__delay_ms(500); // Wait for 1 second

Set_Duty(125); // Set duty cycle for 90 degrees (~1.5ms pulse)
__delay_ms(500); // Wait for 1 second

Set_Duty(188); // Set duty cycle for 180 degrees (~2ms pulse)
__delay_ms(500); // Wait for 1 second
}
}

Code Explanation

  • PWM_Init(): Initializes the PWM by configuring the CCP1 module of the PIC16F877A.
  • Set_Duty(): Sets the PWM duty cycle, which directly controls the angle of the servo motor.
  • Main Loop: In the loop, the servo motor rotates to 0°, 90°, and 180° by setting corresponding duty cycles.

Applications of Servo Motors in Robotics

Controlling servo motors is a key element in many robotic systems. Let’s explore some common applications where servo motors play a vital role:

1. Robotic Arms

Servo motors provide precise control over the movement of robotic arms, allowing them to perform complex tasks such as picking, placing, and rotating objects with accuracy.

2. Robotic Vehicles

In mobile robotics, servos are often used for steering and controlling mechanical arms. Whether it's an autonomous vehicle or a drone, servos are integral in controlling movement.

3. Humanoid Robots

Servo motors are essential for joint movement in humanoid robots. They control the movement of arms, legs, and even facial expressions, mimicking human actions.

4. Pan-Tilt Camera Systems

Servos are widely used in pan-tilt mechanisms for cameras. They enable precise control of the camera angle, which is important for surveillance robots or autonomous vision systems.

Conclusion

Servo motors, when controlled using the PIC16F877A, offer precise movement and are ideal for a variety of robotic applications. By utilizing the PWM capabilities of the PIC16F877A, you can control servos to achieve smooth and accurate motion in robotic systems. Whether you're building a robotic arm, an autonomous vehicle, or a camera system, learning to control servo motors with this microcontroller will significantly enhance your projects.

Further Reading:

 

Post a Comment

Previous Post Next Post