Small DC Motor
DC motor converts electrical energy in the form of Direct Current into mechanical energy.
- In the case of the motor, the mechanical energy produced is in the form of a rotational movement of the motor shaft.
- The direction of rotation of the shaft of the motor can be reversed by reversing the direction of Direct Current through the motor.
- The motor can be rotated at a certain speed by applying a fixed voltage to it. If the voltage varies, the speed of the motor varies.
- Thus, the DC motor speed can be controlled by applying varying DC voltage; whereas the direction of rotation of the motor can be changed by reversing the direction of current through it.
- For applying varying voltage, we can make use of the PWM technique.
- For reversing the current, we can make use of an H-Bridge circuit or motor driver ICs that employ the H-Bridge technique or any other mechanisms.
For more information about DC motors and how to use them, H-Bridge circuit configuration, and PWM technique, refer to the topic DC Motors in the sensors and modules section.
DC Motor Connection to 8051 using Motor Driver
Let’s interface the DC motor with the AT89S52 microcontroller and control the DC motor speed by using the Speed Increment Switch and Speed Decrement Switch connected to the Microcontroller port and direction by using Direction Switch.
We are going to use the L293D motor driver IC to control the DC motor movement in both directions. It has an in-built H-bridge motor drive.
- As shown in the above figure we have connected two toggle switches on the P1.0 and P1.1 pin of the AT89S52 microcontroller to change the speed of the DC motor by 10%.
- One toggle switch at pin P1.2 controls the motor's rotating direction.
- P1.6 and P1.7 pins are used as output direction control pins. It provides control to motor1 input pins of the L293D motor driver which rotate the motor clockwise and anticlockwise by changing their terminal polarity.
- And Speed of the DC Motor is varied through PWM Out pin P2.0.
- Here we are using the timer of AT89S52 to generate PWM.
DC Motor Controlling Code using 8051
#include <reg52.h>
#include <intrins.h>
/* Define value to be loaded in timer for PWM period of 20 milli second */
#define PWM_Period 0xB7FE
sbit PWM_Out_Pin = P2^0; /* PWM Out Pin for speed control */
sbit Speed_Inc = P1^0; /* Speed Increment switch pin */
sbit Speed_Dec = P1^1; /* Speed Decrement switch pin */
sbit Change_Dir = P1^2; /* Rotation direction change switch pin */
sbit M1_Pin1 = P1^6; /* Motor Pin 1 */
sbit M1_Pin2 = P1^7; /* Motor Pin 2 */
unsigned int ON_Period, OFF_Period, DutyCycle, Speed;
/* Function to provide delay of 1ms at 11.0592 MHz */
void delay(unsigned int count)
{
int i,j;
for(i=0; i<count; i++)
for(j=0; j<112; j++);
}
void Timer_init()
{
TMOD = 0x01; /* Timer0 mode1 */
TH0 = (PWM_Period >> 8);/* 20ms timer value */
TL0 = PWM_Period;
TR0 = 1; /* Start timer0 */
}
/* Timer0 interrupt service routine (ISR) */
void Timer0_ISR() interrupt 1
{
PWM_Out_Pin = !PWM_Out_Pin;
if(PWM_Out_Pin)
{
TH0 = (ON_Period >> 8);
TL0 = ON_Period;
}
else
{
TH0 = (OFF_Period >> 8);
TL0 = OFF_Period;
}
}
/* Calculate ON & OFF period from PWM period & duty cycle */
void Set_DutyCycle_To(float duty_cycle)
{
float period = 65535 - PWM_Period;
ON_Period = ((period/100.0) * duty_cycle);
OFF_Period = (period - ON_Period);
ON_Period = 65535 - ON_Period;
OFF_Period = 65535 - OFF_Period;
}
/* Initially Motor Speed & Duty cycle is zero and in either direction */
void Motor_Init()
{
Speed = 0;
M1_Pin1 = 1;
M1_Pin2 = 0;
Set_DutyCycle_To(Speed);
}
int main()
{
EA = 1; /* Enable global interrupt */
ET0 = 1; /* Enable timer0 interrupt */
Timer_init();
Motor_Init();
while(1)
{
/* Increment Duty cycle i.e. speed by 10% for Speed_Inc Switch */
if(Speed_Inc == 1)
{
if(Speed < 100)
Speed += 10;
Set_DutyCycle_To(Speed);
while(Speed_Inc == 1);
delay(200);
}
/* Decrement Duty cycle i.e. speed by 10% for Speed_Dec Switch */
if(Speed_Dec == 1)
{
if(Speed > 0)
Speed -= 10;
Set_DutyCycle_To(Speed);
while(Speed_Dec == 1);
delay(200);
}
/* Change rotation direction for Change_Dir Switch */
if(Change_Dir == 1)
{
M1_Pin1 = !M1_Pin1;
M1_Pin2 = !M1_Pin2;
while(Change_Dir == 1);
delay(200);
}
}
}
DC Motor Control using 8051 and motor Driver Video
Components Used |
||
---|---|---|
L293D Driver L293D Driver |
X 1 | |
8051 AT89c51 8051 AT89c51 |
X 1 | |
Mini Push Button Switch - 5-6mm Mini Push Button Switch - 5-6mm |
X 3 |