Home > Control Circuits >Microcontroller > How to use a microcontroller to control a servo motor?

How to use a microcontroller to control a servo motor?

Source: InternetPublisher:睡不醒的小壮 Keywords: Microcontroller servo motor Updated: 2025/02/07

The servo motor is a commonly used motor. In this article, we will learn how to use a microcontroller to control the servo motor. The case uses the PIC16F877A microcontroller as an interface connection and control example.

What is a servo motor?

A servo motor is a special motor that runs according to given instructions. It provides precise angular control, which means that unlike other motors that rotate as soon as power is applied, the servo motor only rotates to a certain extent or until the next rotation is required, before which the motor will stop and wait for the next instruction to perform further actions. Therefore, the servo motor needs to be controlled by the servo circuit to rotate as required, and its angular rotation and final movement are determined by the position feedback, and the input of its control line also determines the required position of the output shaft.

Servo motor circuit diagram of servo motor and PIC16F877A interface:

20220401182021.jpg

It has a very simple circuit diagram. The control wires of the servo motor are directly connected to the RB0 pin of the microcontroller. This pin will provide the required angular displacement of the motor. In this project, let's assume that we are using a servo motor whose angular rotation is limited to 0°-180°. We can control the rotation of the motor with maximum accuracy to achieve the desired angle by using pulses of varying width.

A pulse is provided to the servo motor after every 20 milliseconds (20000 microseconds). The angular position of the motor is determined by the length of this pulse. Angular positions 0°, 90°, and 180° are demonstrated in the code.

Below is the C code for PIC microcontroller interface to control the servo motor. We write the following servo motor code to interface with PIC in the compiler:

void Rotation0() //0 degrees

{

unsigned int i;

for(i=0;i<50;i++)

{

PORTB.F0 = 1;

Delay_us(800); //800us pulse

PORTB.F0 = 0;

Delay_us(19200);

}

}

void Rotation90() //90 degrees

{

unsigned int i;

for(i=0;i<50;i++)

{

PORTB.F0 = 1;

Delay_us(1500); //1500us pulse

PORTB.F0 = 0;

Delay_us(18500);

}

}

void Rotation180() //180 degrees

{

unsigned int i;

for(i=0;i<50;i++)

{

PORTB.F0 = 1;

Delay_us(2200); //2200us pulse

PORTB.F0 = 0;

Delay_us(17800);

}

}

void main()

{

TRISB = 0; // PORTB is used as output port

do

{

Rotation0(); //0 degrees

Delay_ms(2000);

Rotation90(); //90 degrees

Delay_ms(2000);

Rotation180(); //180 degrees

}while(1);

}

The individual functions for the motor angle rotation of 0°, 90° and 180° have been declared at the beginning of the code. In this tutorial, we have not used the actual pulse width modulation feature of the PIC16F877A to generate the pulses. Instead, the pulses are generated with the help of programmed delays. The delay duration for a particular angle is equal to the pulse length required to rotate the motor to the corresponding angle. That is, for an angle of 0°, the pulse width is about 800ms, so a delay of 800ms is introduced when the PORT pin RB0 is set high. Similarly, a pulse of 1500ms is required for a rotation of 90° and 2200ms for an angle of 180°.

In the main program, PORTB is set as an output port and all three functions are called with a 2000ms delay between them. This program rotates the motor in a certain pattern, say 0° – 90° – 180° – 0°, etc. This will continue to run in an infinite looping “do-while” loop until the program is aborted.

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号