servo motor interfacing with 8051 using keil compiler

servo motor interfacing WITH 8051 MICROCONTROLLER: This article is about interfacing of servo motor with 8051 microcontroller. You will learn how to interface and control this motor using 8051 microcontroller. Servo motors are used in robotics, embedded systems and industries because they are very precise and reliable. They are used to operate remote control toy cars, airplanes or robots. Their motion can be controlled by rotating them in particular angle. Servo motor can be controlled by PWM signal. In this article we will interface servo motor with 8051 microcontroller and control its speed.  I will recommend you to read a article on how to used keil compiler and how to use I/O ports of 8051 microcontroller before learning about interfacing servo motor with 8051.

CONSTRUCTION of servo motor

A DC servo motor consists of:

  • DC motor
  • Potentiometer (variable resistor)
  • Gear assembly
  • Control circuit

It consists of a closed loop system. Positive feedback is given to control the motion and position of the shaft. Feedback signal is generated by comparing output signal and reference input signal. Now, this feedback signal will act as input signal to control device. This signal will remain present as long as there remains a difference between reference input signal and output signal. So we have to maintain output of this system at desired value in presence of noises.

PRINCIPLE of interfacing servo motor

Servo motor is controlled by PWM (Pulse with Modulation) technique in which we control the angle of rotation through the duration of pulse applied to its Control pin.

It has three connection wires for:

  • Positive supply V+
  • Negative supply or Ground V-
  • Control signalservo motor interfacing

First two wires are used for giving power to motor. The control signal is connected with microcontroller to feed PWM signals. Gear and potentiometer control the DC motor in it. Servo motor can turn to 90, 180 degree in either direction and can go up to 360 degrees. Turning angle depends on manufacturing. The servo motor expects a pulse after every 20ms. Length of pulse determines how far the motor turns. The shaft angle exceeds with increase of pulse width. The duration of that logic 1 pulse can be from 1ms to 2ms.

  • 1ms duration pulse can rotate servo motor to 0 degree
  • 5ms duration pulse can rotate it to 90 degree
  • 2ms duration pulse can rotate it to 180 degree

So we can say that pulse duration between 1ms to 2ms can rotate Servo motor to any angle that is between 0 and 180 degree.servo motor angle control


We will control servo motor with AT89C51 microcontroller. Servo motor red and black wires are connected to battery and ground respectively. Its control wire is connected with any port pin of microcontroller.


CONNECTIONS diagram of servo motor interfacing with 8051 microcontroller

  • P2 of 8051 microcontroller is used as output port. Its lower one pin is used which is connected with control pin of servo to give logic input to servo motor.
  • P0 of 8051 microcontroller is used as output port. Its lower three pins are connected with push buttons so that we can manually start the motor.
  • 5V battery is used to give input to servo motor.

PROTEUS SIMULATION of servo motor interfacing with 8051 microcontrollerservo motor interfacing with 8051 microcontroller

WORKING of servo motor interfacing with 8051 microcontroller

When we start the simulation, motor will not rotate until any button is pressed. When 1st push button is pressed, servo motor will rotate up to 90 degree. When I press 2nd push button, it can rotate up to 180 degree and stops. And when 3rd push button is pressed, it will rotate up to 270 degrees.

CODE of servo motor interfacing with 8051 microcontroller

#include <REGX51.H>

void Delay_servo(unsigned int);

sbit control_pin=P2^0;

sbit B1=P0^0;

sbit B2=P0^1;

sbit B3=P0^2;

void main()


P0=0x07;                                            // input port

control_pin=0;                                                 // output pin




             {                                              //Turn to 90 degree





else if(B2==1)

            {                                               //Turn to 180 degree





else if(B3==1)

            {                                               //Turn to 270 degree







void Delay_servo(unsigned int d)


  TMOD &=0xF0;                    // Clear 4bit field for Timer0

  TMOD|=0x01;                                   // Set timer0 in mode1, 16bit

  TH0=0xFF - (d>>8)&0xFF;              // Load delay vales Timer 0 + Bitwise right shift[c][d]a >> b

  TL0=0xFF- d&0xFF;              

  ET0=1;                                              //Enable timer0 interrupts

  EA=0;                                                // Global Interrupt

  TR0=1;                                              // Start timer 0

  while(TF0==0);                                 // Wait for overflow

  TR0=0;                                  //Stop timer0


  TF0=0;                                              // Clear Flag



  • Conveyor Belts: In conveyor belts each moment of the belt is controlled by servo motors i.e. to start, stop and move the products. E.g. packaging, labeling and bottling.
  • Robotics: Almost each junction of a robot contains a servo motor, which moves the robotic arm at an accurate angle.
  • Camera Auto Focus: In cameras there is a built in servo motor which is highly accurate and is used to correct the camera lens to adjust the out of focus pictures.
  • Robotic Vehicle: These vehicles are mostly used by military organizations for bomb detonations, the moment of the wheels are controlled by the servo motor, it generates sufficient torque to smoothly control the movement as well as speed of the vehicle.
  • Solar Tracking System: In automatic solar panels, servo motors are used to change the angle of the solar plates with the movement of the sun so that the solar panels can continuously face the sun and we can get the maximum power from the system.
  • Metal Fabrication Machines: For the precise cutting, molding, lathes, grinding, pressing, punching, bending and centering in metal fabrication, servo motors are considered as the backbone.
  • Antenna Positioning: For the vertical and horizontal movement of the antennas and telescopes, servo motors are used to get the accurate position and angle.
  • Woodworking/CNC: In CNC machines, servo motors are used to cut and shape the wood products and also to drill holes and auguring which are necessary for assembling process.
  • Textiles: In textile industry servo motors are used to control the weaving machine, spinning, knitting machines and looms e.g. caps, socks, gloves, carpets and fabrics are produced by textile industries.
  • Printing Presses/Printers: To start and stop the printer head and at the same time to move the paper to draw graphics, pictures and text accurately servo motors are used. Examples are photographs, newspapers, magazines and documents.
  • Automatic Door Openers: In malls, hospitals and commercial buildings servo motors control the automatic movement of the doors by getting input signal from whether the pressure plates or by IR

5 thoughts on “servo motor interfacing with 8051 using keil compiler”

  1. Good article and helpful post

    But I have one query, Can you explain how you have obtain
    1218 for 90 Degree
    1470 for 180 Degree
    1720 for 270 Degree


Leave a Comment