View Course Path

Servo Motor Interfacing with 8051 – Simple tutorial

Putting the theory that we have learned so far in this 8051 course, we’ll try to apply it by interfacing a servo motor with the 8051. Let’s talk in detail about the servo motor and then discuss the actual interfacing with circuit diagrams and the code to control it all.

Components Required

Sr No. Component Required Description Nos.
1 8051 Microcontroller AT89S51/ AT89C51/Any other compatible variants 1
2 Servo Motor (DC) 5-6V 1
3 DC Power Supply 5V (1.2A) 2
4 Oscillator Crystal 12MHz 1
5 Capacitor (1) 22pF 2
6 Capacitor (2) 10µF 1
7 Resistor/ Pot 10kΩ 1

What is a servo motor?

If you are looking for a motor with precise position control for industrial and commercial applications, then the servo motor might be right up your alley. Servo motors are most commonly used in closed-loop systems where the precision of the position of the shaft is a must required quality. For example, for metal cutting and forming machines or even an Antenna Positioning system consists of one or more servo motors.

A closed-loop system is a control system where the output of the system depends on the input as well as the output (in the form of feedback). The feedback is usually through a sensor. In the case of the servo motor, it’s a positional transducer that converts angular position to an electrical signal and ensures accurate outputs. We’ll see how the feedback works in the case of the servo motor below. Let’s take a more conventional example to understand feedback in a closed-loop. When you set the desired temperature, the sensor in your AC senses the ambient temperature and accordingly sends a signal to the control system to switch on the cooling system if the temperature goes above the desired temperature or to switch it off if it goes below (or equal).

Servo Motor

Let’s begin by understanding how a servo motor works from a mechanical and electrical point of view.

How does a servo motor work?

The servo motor is a combination of the motor, a shaft, a gear assembly, an amplifier, and an encoder or a resolver. It is a self-contained electrical device that rotates a machine or parts of a machine with great precision and high efficiency. By controlling the position and speed of the shaft, we can control the angle of the shaft with respect to the stationary parts.

The servo consists of a normal motor with a positional sensor governed by a control circuit. The positional sensor is a transducer that converts angular position to electrical pulses. These pulses are sent through an amplifier. The particular signal sent is called the actuating signal. Since the operation is in a closed-loop, these actuating signals provide input to the feedback mechanism to control the rotational or linear speed and position of the shaft.

Servo motors are classified into different types:

  • Based on their current type: AC or DC.
  • Based on the type of Commutation used: Brushed or Brushless.
  • On the basis of the motor’s rotating field, the rotor, whether the rotation is Synchronous or Asynchronous.

How do we control the servo motor electrically using our 8051 microcontroller?

It’s simple, the ports of 8051 microcontrollers provide the PWM signals to the servo motor. Depending on the width of these pulses, the shaft is rotated to certain angles ranging from 0 to 180 degrees (theoretically), which can be extended up to 210 degrees depending on the manufacturer. Although practically, it might not rotate to such extremes (180 degrees) due to the inertial barrier.

What are the components of a servo motor?

Servo parts

Broadly, a servo motor consists of:

  1. Electric Motor: A motor is a device that converts electrical energy into mechanical energy; it’s a rotatory element. A user can choose from a variety of servo motors available depending upon the need, for example,
    • Brushless DC servomotor (Good for use if you are looking for high-resolution encoders and precision gearheads.)
    • Positional rotation servo motor (Good for use if you’re looking for servo-valves, radio-controlled cars, robotic arm, and many other applications)
  2. Control system:  A closed-loop system that controls the position of the shaft depending on the control signal.
  3. Drive System: It consists of an arrangement of gears in such a way that one can increase or decrease the speed and torque of the motor.
  4. Potentiometer: It is connected to the central shaft, and helps the control system to monitor the angle in which the motor’s shaft is positioned.

Using the 8051 for PWM

Servo motors work on the Pulse Width Modulation (PWM) technique, where the width or duration of the pulse signal decides the angle of rotation or the angular position of the shaft.

Here we’re using a DC motor, which is controlled electrically with variable resistors or a potentiometer and mechanically by gear arrangement, which converts the force exerted by the motor into electrical torque.

To start with the frequency, every device operates at f = 50Hz frequency, and it implies a time period of T = 20ms. We can control the exact angular position by varying the pulse between 1ms and 2ms.

Pulse Width Angular Position of the shaft
1 ms 0 degrees
1.5 ms 90 degrees
2 ms 180 degrees

Angle corresponding to Pulse width

The control mechanism of a servo motor

Control mechanism of Servo Motor

  • The control signal (PWM pulse) can be either digital or analog.
  • Here we’re using 8051 microcontroller to generate the PWM pulses.
  • The feedback corresponding to the present position is obtained from the positional sensor.
  • The position sensor here can be a potentiometer that produces a voltage corresponding to the angle of the motor shaft.
  • The feedback voltage (obtained by the current position of the motor) is compared with the desired voltage (related to the desired position of the motor), and an error voltage is generated.
  • This error voltage (either positive or negative, depending on the desired position) is amplified and given to the armature of the motor to obtain the desired rotational angle at all times.

How to select a suitable Servo Motor for an 8051 project?

Servo motor can be divided into two parts:

  1. Standard/Limited Motion servo motor: The motor rotates from 0 to a maximum of 180 degrees. It can be used in applications like a robotic arm whose purpose is to pick and drop objects or wing flap control of an airplane, etc.
  2. Continuous motion servo motor: Here, the motor rotates continuously, and the PWM pulses control the speed and torque instead of angle and position of the motor.

Torque is an essential parameter of the servo motor. The servo motors we linked to in the components required section should suffice for most general applications. They provide a torque of 2.5kg/cm. Meaning, they can lift a weight of 2.5kg when it lies at a distance of 1 cm. Likewise, when we place the substance at 1/2cm, it can pull a load of 5kg and so on.

No matter what kind of servo motor we’re using, the interfacing procedure remains almost the same.

Pins of a servo motor

A servo motor consists of 3 pins, namely live, ground, and a control pin.

  • The live wire is represented by Red wire.
  • The ground wire is represented by Brown wire.
  • The control wire is represented by Blue or Orange wire.

Here’s a diagram for your reference.

Servo-Motor-Wires

Step-by-step connections

  1. Step 1: If you’re using Proteus or and other simulation software or even hardware, select the AT89C51 or AT89S51 microcontroller or any other compatible variant. (The AT89C51 is an 8-bit microcontroller from the Atmel family which works with the 8051 architecture.)
  2. Step 2: Connect a 12 MHz oscillator between pin 18 and 19.
  3. Step 3: Connect two capacitors of 22pF, with one terminal on either side of the oscillator and the other terminal to ground, as shown below.
  4. Step 4: Set Pin 31, i.e., EA pin to HIGH by connecting it to the +5V DC source.
  5. Step 5: Now, to make the RESET circuit, connect Pin 9 (RST) to +5V through a capacitor of 10µF and connect the same pin to +0V (GND) through a 10kΩ resistor or a potentiometer.
  6. Step 6: Now connect the wire in the following way:
    1. Live wire (RED) to +5V source
    2. Ground wire (BROWN) to the ground (GND)
    3. Control wire (BLUE ) to Port 2.0 (i.e., pin 21)

Circuit diagram to interface a servo motor with 8051

Simulation of servo interfaced with 8051

That’s it. We’re now done with the connections, now let’s go to the PWM logic.

PWM Logic to generate pulses

  1. To generate a pulse of 1ms = 1000µs corresponding to 0 degrees.
    • We’re using timer 0 in mode 1, i.e., 16-bit mode. TH0 and TL0 are set in such a way that we get 1000us delay. Check out our post on timers and counters in 8051 to revise the required concepts.
    • The timer counts up from whatever value we give, if the initial value is 0000 then timer moves from 0000 to FFFF and an extra transition from FFFF to 0000 before the flag TF0 is set. Therefore we get a delay of FFFF + 1 = (65536)D µs.
    • Now (1000)D = (03E8)H.
    • Therefore to get (1000)D us, we have to select the initial level
      (65536 – 1000 +1)D = (64537)D = (FC19)H
      TH0 = FCH
      TL0 = 19H
  2. To generate a pulse of 1.5ms = 1500µs corresponding to 90 degrees.
    • (65536 – 1500 + 1 ) = (64037)D = (FA25)H
      TH0 = FAH
      TLO = 25H
  3. To generate a pulse of 2ms = 2000µs corresponding to 180 degrees.
    • (65536 – 2000 + 1) = (63537)D = (F831)H
      TH0 = F8H
      TL0 = 31H
  4. To generate a delay of 1s corresponding to the wait time between these positions.
    • We use the DJNZ command with nested loop creating a delay of 100µs * 100µs * 100µs = 1s

Assembly language program to interface servo motor with 8051

ORG 00H //Start the program
MAIN:
MOV TMOD, #01H ;using Timer 0 in Mode 1
LCALL zero_degrees ;Function to move to position = 0 deg
LCALL delay ;Function to create a delay of 1 sec

LCALL ninety_degrees ;Function to move to position = 90 deg
LCALL delay ;Function to create a delay of 1 sec

LCALL one_eighty_degrees ;Function to move to position = 180 deg
LCALL delay ;Function to create a delay of 1 sec

SJMP MAIN ;to repeat the loop until manually stopped
RET

zero_degrees: //To create a pulse of 1ms
MOV TH0, #0FCH //(FFFF - FC19 + 1)H = (03E7)H 
MOV TL0, #19H //equal TO (1000)D = 1ms
SETB P2.0 ;Make P2.0 HIGH
SETB TR0 ;Start the timer 0
WAIT1:JNB TF0, WAIT1 ;Wait till the TF0 flag is set 
CLR P2.0 ;Make P2.0 LOW 
CLR TF0 ;Clear the flag manually
CLR TR0 ;Stop the timer 0
RET

ninety_degrees: //To create a pulse of 1.5ms
MOV TH0, #0FAH //(FFFF - FA25 + 1)H = (05DB)H 
MOV TL0, #25H //equal to (1500)D = 1.5ms
SETB P2.0 ;Make P2.0 HIGH
SETB TR0 ;Start the timer 0
WAIT2:JNB TF0, WAIT2 ;Wait till the TF0 flag is set 
CLR P2.0 ;Make P2.0 LOW 
CLR TF0 ;Clear the flag manually
CLR TR0 ;Stop the timer 0
RET 

one_eighty_degrees: //To create a pulse of 2ms 
MOV TH0, #0F8H //(FFFF - F831 + 1)H = (07CF)H 
MOV TL0, #31H //equal to (2000)D = 1.5ms
SETB P2.0 ;Make P2.0 HIGH
SETB TR0 ;Start the timer 0
WAIT3:JNB TF0, WAIT3 ;Wait till the TF0 flag is set
CLR P2.0 ;Make P2.0 LOW 
CLR TF0 ;Clear the flag manually
CLR TR0 ;Stop the timer 0
RET 

delay: //To create a delay of 1sec 
MOV R4,#64H ;100us * 100us * 100us = 1s
LOOP1:MOV R3,#64H
LOOP2:MOV R2,#64H
LOOP3:DJNZ R2,LOOP3
DJNZ R3,LOOP2
DJNZ R4,LOOP1
RET

C program to interface servo motor with 8051

#include<reg52.h>
#include<stdio.h>
#include <intrins.h>

sbit servo_pin = P2^0;
void delay(unsigned int);
void servo_delay(unsigned int);
void main()
{
  servo_pin = 0;
  do
  {
   //Rotate to 0 degree
   servo_pin = 1;
   servo_delay(50);
   servo_pin = 0;
	 
   delay(1000);

   //Rotate to 90 degree
   servo_pin=1;
   servo_delay(82);
   servo_pin=0;
	 
   delay(1000);

   //Rotate to 180 degree
   servo_pin=1;
   servo_delay(110);
   servo_pin=0;
	 
   delay(1000);
 }while(1);
}

void delay(unsigned int ms)
{
  unsigned long int us = ms*1000;
  while(us--)
  {
    _nop_();
  }		
}

void servo_delay(unsigned int us)
{
  while(us--)
  {
    _nop_();
  }
}

Application of servo motor

  • CNC machinery or automated manufacturing
  • Radio-controlled cars
  • Labeling applications
  • Packing system with random timing function
  • Used in airplanes
  • Self Balancing Robots

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Top