DC Motor Interfacing with 8051 Microcontroller

In this tutorial, we will learn about DC motor interfacing with an 8051 microcontroller. In many embedded system projects, we may need to control a DC motor using a microcontroller. It is not a good practice to connect the DC motor directly to the microcontroller, as it may damage the microcontroller. For this, we use a motor driver that helps us control the DC motor without causing any functional problems for the microcontroller. This article will demonstrate how to control the DC motor using an AT89C51 microcontroller.

DC Motor Driver Interfacing with 8051 Microcontroller

Now the question that comes to mind is: how can a DC motor affect the working of the microcontroller? This is because the maximum current that the 8051 microcontroller can sink is 15 mA at 5 V, whereas a DC motor requires much more current. It also needs higher voltages, such as 6 V, 12 V, 24 V, etc. (depending upon the type of motor used). One more thing to notice is that the back EMF produced by the motor may affect the working of the microcontroller, and reversing the direction can damage the controller. Due to these reasons, we can’t connect a DC Motor directly to a microcontroller.

To overcome these problems in the interfacing of the microcontroller and the DC motor, we connect a motor Driver IC between them. The motor driver is a little current amplifier. It takes a low-current signal and gives out a high-current signal, which can drive a motor. It can also control the direction of the motor. We can use any dual H-bridge IC, like L293D or L298.

The main differences between L293D and L298 are:

CharacteristicsL298L293D
Max. output current per channel2A0.6A
Peak max. output current per channel3A1A
Protection diodes across motorsUse externallyInternally available
Table 1 Difference between L298 and L293D motor driver IC


The L298 motor driver is already explained in a separate article. In this article, we will use L293D since it does not need external protection diodes.

H-Bridge Circuit

The name “H-Bridge” comes from the shape of the switching circuit, which controls the motion of the motor. It is also known as “Full Bridge”. This circuit has four switching elements. We can make H-bridges with the help of transistors, MOSFETs, etc. It will be cheap, but they will increase the size of the design and the circuit board, which is mostly not preferable, so we can use a small 16-pin IC for this purpose.

Working of H-bridge Circuit

As we can see in the figure, there are four switching elements named:

  • High side left
  • High side right
  • Low side right
  • Low side left

When these switches are turned on in pairs, the motor changes its direction accordingly. If we switch on “High side left” and “Low side right”, then the motor will rotate in a forward direction as current from the power supply flows through the motor coil and goes to ground through the switch on the low side (right). This is shown in the figure below.

H bridge for dc motor interfacing control

Similarly, when we switch on the low side (left) and the high side (right), the current flows in the opposite direction and the motor rotates in the opposite direction. This is the basic working of H-Bridge. We can also make a small truth table according to the switching of H-Bridge explained above.

High LeftHigh RightLow LeftLow RightDescription
OnOffOffOnMotor runs clockwise
OffOnOnOffMotor runs anti-clockwise
OnOnOffOffMotor stops or decelerates
OffOffOnOnMotor stops or decelerates
Table 2: Working of H-bridge circuit


So we have seen that by using simple switching elements, we can make our own H-Bridge. Another option is to use an IC-based H-bridge driver. Obviously, we will use Driver ICs; otherwise, we will have to use heat sinks for MOSFETs, etc.

L293D for DC Motor Interfacing with 8051 Microcontroller

The L293D motor driver provides bidirectional drive currents of up to 600 mA at voltages from 4.5 V to 36 V. It can drive inductive loads such as solenoids, relays, DC motors, and bipolar stepping motors. It contains internal protection diodes across the motor.

Pin Diagram

L293D for dc motor interfacing with 8051 microcontroller

Pin Description

L293D contains four half H-Bridge drivers. We can drive two DC Motors with a single driver.

  • The VSS pin provides input voltage to the L293D. For the 8051 interface, 5 V is given to it.
  • The motor supply is given to the VS pin of the L293D. It depends on the motor’s requirements.
  • EN1 enables the input pair 1 (IN1, IN2, for OUT1, OUT2), and EN2 enables the input pair 2 (IN3, IN4, for OUT3, OUT4). We connect EN to 5 V to enable the input.
  • We can control the direction of motor 1 through input-pin logic. IN1 and IN2 control motors connected to outputs OUT1 and OUT2.
  • We can control the direction of motor 2 through input-pin logic. IN3 and IN4 control motors connected to outputs OUT3 and OUT4.
  • All GND pins should be connected to ground.

Connections

  • We will use P2 of the 8051 microcontroller as the output port, and it will give inputs to the motor driver IC. Its lower four pins are connected to drive two DC motors.
  • We will use P0 as the input port, and by connecting two buttons to its lower two pins, we can manually start and stop the motors.
  • Motor 1 will connect between OUT1 and OUT2 of L293D.
  • Motor 2 will connect between OUT3 and OUT4 of L293D.
  • A 12 V battery provides the input to the VS for motors.
  • A 5 V battery provides the input to the VSS for the motor driver IC.

Circuit Components

Here we have listed all the components used in the simulation of this project

  • AT89C51 microcontroller.
  • 12 MHz Oscillator.
  • 12V DC battery.
  • 5V DC battery.
  • L293D motor driver.
  • DC motor – 2.
  • 2 Ceramic capacitors – 33pF.
  • 300Ω resistors – 2.
  • Push buttons – 2.
  • Connecting wires.

Proteus Schematic

In this section, we have provided the Proteus simulation schematic. The working of this schematic is explained below.

dc motor interfacing with 8051 microcontroller proteus simualtion

Working of DC Motor Interfacing with 8051 Microcontroller

After loading the program into the microcontroller, the motors will not start turning unless we press the buttons. According to the coding:

ButtonStateMotorFunction
1pressed1rotates in an anti-clockwise direction
1not-pressed1stops rotating
2pressed2rotates in anti-clockwise direction
2not-pressed2stops rotating
Table 3: Working of DC motor interfacing – 8051 microcontroller

Pin Status

Enabling pin 1, if we give logic as:

Pin 2Pin 7MotorFunction
101motor rotates clockwise
011motor rotates anti-clockwise
111motor stops
001motor stops
Table 4: Motor 1 working logic


Enabling pin 9, if we give logic as:

Pin 10Pin 15MotorFunction
102motor rotates clockwise
012motor rotates anti-clockwise
112motor stops
002motor stops
Table 5: Motor 2 working logic

Code of DC Motor Interfacing with 8051 Microcontroller

In this section, we have provided the code for the simulation, which is as follows:

#include <REGX51.h>

void delay(void);

sbit m1_pin1 = P2 ^ 0; // Port 2 Pin 0, motor 1
sbit m1_pin2 = P2 ^ 1; // Port 2 Pin 1, motor 1
sbit m2_pin1 = P2 ^ 2; // Port 2 Pin 2, motor 2
sbit m2_pin2 = P2 ^ 3; // Port 2 Pin 3, motor 2

sbit B1 = P0 ^ 0; // Port 0 Pin 0, button 1
sbit B2 = P0 ^ 1; // Port 0 Pin 1, button 2

void main()
{
  P2 = 0x00; //output port
  P0 = 0x03; //input port

  if (B1 == 1)
  {
    m1_pin1 = 1;
    m1_pin2 = 0; //Rotates Motor Clockwise

    delay();
    
  } else if (B1 == 0)
  {

    m1_pin1 = 0;
    m1_pin2 = 0; //Stops Motor

    delay();
  }
  if (B2 == 1)
  {
    m2_pin1 = 0;
    m2_pin2 = 1; //Rotates Motor Anticlockwise

    delay();
  } else if (B2 == 0)
  {
    m2_pin1 = 0;
    m2_pin2 = 0; //Stops Motor

    delay();
  }
}

void delay()
{
  int i;
  for (i = 0; i < 1000; i++)
  {
      
  }
}

Code Explanation

Here we will explain the workings of the code.

At first, we include the header <REGX51.h>. This will help us deal with the 8051 microcontroller’s registers. Next, we declare a delay function. After this, we set 4 lower pins of Port 2 as output to motor 1 and motor 2. Finally, we set the button pins using pin 0 and pin 1 of Port 0.

#include <REGX51.h>

void delay(void);

sbit m1_pin1 = P2 ^ 0; // Port 2 Pin 0, motor 1
sbit m1_pin2 = P2 ^ 1; // Port 2 Pin 1, motor 1
sbit m2_pin1 = P2 ^ 2; // Port 2 Pin 2, motor 2
sbit m2_pin2 = P2 ^ 3; // Port 2 Pin 3, motor 2

sbit B1 = P0 ^ 0; // Port 0 Pin 0, button 1
sbit B2 = P0 ^ 1; // Port 0 Pin 1, button 2

In the void main, we define Port 2 as an output port and Port 0 as an input port. Now we check for button 1. If we press button 1, pin 1 gets high and pin 2 gets low, which will cause the motor to rotate clockwise. In case we stop pressing the button, pins 1 and 2 get low, and motor 1 stops rotating.

void main()
{
  P2 = 0x00; //output port
  P0 = 0x03; //input port

  if (B1 == 1)
  {
    m1_pin1 = 1;
    m1_pin2 = 0; //Rotates Motor Clockwise

    delay();
    
  } else if (B1 == 0)
  {

    m1_pin1 = 0;
    m1_pin2 = 0; //Stops Motor

    delay();
  }

The part of the code is similar to motor 2. If we press button 2, then pin 1 gets low and pin 2 gets high in Port 2. This will cause motor 2 to rotate in an anti-clockwise direction. Now, if we stop pressing this button, motor 2 will stop rotating. This is because pins 1 and 2 of Port 2 become low.

if (B2 == 1)
  {
    m2_pin1 = 0;
    m2_pin2 = 1; //Rotates Motor Anticlockwise

    delay();
  } else if (B2 == 0)
  {
    m2_pin1 = 0;
    m2_pin2 = 0; //Stops Motor

    delay();
  }
}

Lastly, we have the delay function. This function keeps the microcontroller busy in a loop to add delay to our program. Here we set the microcontroller to go through 1000 iterations each time we call the delay function in our program.

void delay()
{
  int i;
  for (i = 0; i < 1000; i++)
  {
      
  }
}

Applications

  • This concept is used in robots to control the robot’s directions.
  • Used to control the speed of the DC motor.
  • It is used in applications where we need to drive high-voltage motors.

Conclusion

In this tutorial, we have discussed the following topics:

  • 8051 Interfacing with DC motor.
  • DC motor driver interfacing
  • H-bridge circuit and it’s working.
  • L293D  for DC Motor Interfacing with 8051 Microcontroller
  • Circuit Components and Proteus Schematic
  • 8051 Code and its Explanation

Related Articles

In case you liked this article and looking for more similar content, check out these links below:

This is all for this tutorial. Let us know in the comment section below if you face any difficulties or issues while following this tutorial.

3 thoughts on “DC Motor Interfacing with 8051 Microcontroller”

Leave a Comment