Interfacing Servo Motor with Atmega32 Microcontroller

A Servo Motor is a type of DC Motor that incorporates error sensing negative feedback to precisely regulate the angular position of its shaft. Unlike standard DC Motors, servo motors do not rotate continuously but instead perform controlled angular rotations, such as 0-90° or 0-180°. While Stepper Motors can also achieve accurate angular rotations, Servo Motors are favored for applications involving angular motion, such as robotic arms. This preference arises from the simplicity of controlling Servo Motors, the absence of additional drivers required as with stepper motors, and the capability for exclusive angular motion.

The operation of a Hobby Servo Motor is straightforward, requiring just three wires. Two of these wires, typically Red and Black, supply power, while the third wire delivers control signals. Control signals are generated using Pulse Width Modulated (PWM) waves, and the angular position is determined by the width of the pulse received at the control input. In this tutorial, we are employing a servo motor with a rotation range from 0-180°, and the angular position can be managed by adjusting the pulse width within the range of 1ms to 2ms.

Please be aware that you should consult the datasheet of your specific Servo Motor before implementing the program provided in this tutorial, as the angular range and control pulse width may vary among different servo motors.

Controlling Angular Position of Servo Motor using Pulse Width Modulation

Circuit Diagram

An 8 MHz crystal is employed to supply the necessary clock signal for the Atmega32 Microcontroller, with the operation of the crystal being stabilized through the use of 22pF capacitors. Additionally, a 10KΩ resistor and a 10μF capacitor are utilized to deliver the necessary Power-On Reset (POR) to the microcontroller. The control of the servo motor is linked to the initial pin of PORTC (RC0), which has been designated as an output pin within the program.

Program

#ifndef F_CPU
#define F_CPU 8000000UL // 8 MHz clock speed
#endif

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
DDRC = 0x01; //Makes RC0 output pin
PORTC = 0x00;
while(1)
{
//Rotate Motor to 0 degree
PORTC = 0x01;
_delay_us(1000);
PORTC = 0x00;

_delay_ms(2000);

//Rotate Motor to 90 degree
PORTC = 0x01;
_delay_us(1500);
PORTC = 0x00;

_delay_ms(2000);

//Rotate Motor to 180 degree
PORTC = 0x01;
_delay_us(2000);
PORTC = 0x00;

_delay_ms(2000);
}
}

Source: Interfacing Servo Motor with Atmega32 Microcontroller


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top