Home / Home made Projects / Microcontroller Projects / Speed and direction control of DC motor using microcontroller

Speed and direction control of DC motor using microcontroller

This article is related to the speed and direction control of DC motor using microcontroller. The concept of PWM is given along with its implementation in microcontroller. Test bed, schematic diagram and simulation results are explained with the help of labelled diagram.

Introduction

Controlling speed of DC motor is always been the topic of interest for electrical engineers. The most common and effective technique of controlling speed of DC motor is using Pulse Width Modulation. To understand how this technique works, the concept behind the speed control of DC motors must be revised. For this purpose please refer to the article various methods of speed control of DC motors

Pulse Width Modulation

It is a technique with the help of which effect of varying analogue signal is achieved via digital signal. A continuous pulse of square waves is created which switch between on and off. This on-off signal can supply voltage between 0V and maximum value by varying the duration signal on time versus the time signal is off. The duration for which the signal is on (high) is called width of pulse. So to get varying output signal value, the pulse with is varied.

Duty Cycle

Duty cycle is the terminology associated with PWM.  When the signal is high, we call this “on time”. To describe the amount of “on time” , we use the concept of duty cycle. Duty cycle is measured in percentage. The percentage duty cycle is the ratio of signal on time to the total time period of the signal. This period is the inverse of the frequency of the waveform.

If a digital signal spends half of the time on and the other half off, we would say the digital signal has a duty cycle of 50% .

If the signal remains high for more time than duty cycle will be more than  50%. In the same way, if signal remains low for more time duty cycle will be less than 50%. Following graph shows three described cases

 

Duty cycle

If the duty cycle is 100% it means setting a signal on high value continuously.  0% duty cycle would be the same as grounding the signal.

So by changing the duty cycle, the output voltage can be changed and varying voltage can vary the speed of DC motor.

Interfacing DC motor with microcontroller

It is not possible to connect dc motor to a microcontroller directly. A microcontroller has a very low output current. On the other hand motor draws much higher current therefore motor cannot be run directly from microcontroller. Also high currents will damag microcontroller.

To interface DC motor with microcontroller, an intermediate IC L293D  dual H bridge IC can be used. It has the capacity to control two motors simultaneously. The output current is as high as 600mA with peak current up to 1.2A.  To provide extra protection form back emf of the motor, diodes are connected at the output of the IC which prevents reverse current flow.

For complete detail of interfacing, follow the article interfacing DC motor with microcontroller

Circuit Diagram 

The following components are used in the speed and direction control of DC motor using microcontroller.

  • PIC18F4220
  • Variable resistor
  • DC motor
  • Push button
  • L293D

The connection diagram is shown in the following figure.

connection diagram for speed and direction control of dc motor using microcontroller

The variable resistor is used to change the duty cycle of the PWM signal thus speed of dc will vary with it. To reverse the direction of rotation, a digital switch is connect at pin 38. It will be explained later how variable resistor changes the duty cycle and what is its relation with the external voltage.

Microcontroller is connected to DC motor through L293D IC which provides high current to motor as well as protects microcontroller from back emf.

Other ICs can also be used for this purpose whose details can be found in DC motor ICs

ISIS Simulation

The proposed circuit is simulated in ISIS Proteus for testing purpose.

When 30% of the total resistance is inserted in the circuit, PWM waves generated my microcontroller is shown.output PWM 1

It can be seen that the duty cycle is low as a result the motor attains less speed as shown in fig.

simulation1

It can be seen that motor only attains speed of 137 RPM while it can attain 450 RPM if full voltage is applied. Negative sign showed the direction of rotation. Now to change the direction of rotation and speed, push button is pressed and value of is variable resistor is increased.

PWM 2

It can be seen from the above figure that duty cycle has been increased. To see its effect on the motor speed consider the following figure.

simulation 2

The motor direction is reversed as shown by + ive sign. The speed has been increased. So it is clear from simulation results that motor speed and direction can easily be changed through microcontroller.

Program

The program is written in C language and compiled in MIKRO C compiler.

#define output1 PORTD.F0    //assigning names to pins which is to be used in motor control.
#define output2 PORTD.F1
#define directionM1 PORTB.F5    //assigning pin for taking input to change direction of motor
unsigned int dutycycleM1;     // initializing variable for duty cycle
unsigned adcResM1;        // initializing variable for storing analogue value to set duty cycle
void main() {
TRISC=0X00;    // making port C as an output port
TRISD=0X00;    // making port D as an output port
TRISB=0XFF;      // making port B as in input port
TRISA=0XFF;    // making port A as in input port
ADC_Init();        // initializing Analogue to digital converter
PWM1_Init(500);  // setting PWM frequency. 500Hz frequency is set
while (1){
adcResM1 = ADC_Read(0);     // reading analogue value from channel 0. 0 stands for channel number
dutycycleM1=0.25* adcResM1;  //  calculating duty cycle from analogue value
PWM1_Set_Duty(dutycycleM1);   // setting calculated duty cycle for PWM
if (directionM1==1)     // check if button is pressed. If yes rotate in clockwise direction
{output1=1 ;            // required output to rotate clockwise
output2=0;}
else                   // if push button is not pressed rotate in anticlockwise direction
{output1=0 ;           // required output to rotate anticlockwise
output2=1; }
PWM1_Start();        // start geneating PWM signal
delay_ms(10);       // wait until check conditions again.
}}

Relation between analogue input voltage and duty cycle

The following command is used to set duty cycle of PWM.

PWM1_Set_Duty(value= 0-255)

To get maximum duty cycle, value of 255 is given while for minimum duty cycle, 0 is given in the command. So the value which stands for maximum duty cycle is 255. On the other hand, maximum analogue input voltage that can be given to microcontroller is 5V.  Using 10bit A/D converter, corresponding digital value is equal to 1024.

To find linear relationship between two quantities the easiest method is that

Max of1st quantity = Max of 2nd quantity

255 (PWM max duty cycle variable value) = 1024(A/D)

1 =1024/255 = 4

Or 0.25 (PWM variable value) = 1 (output of A/D)

So if the output of A/D converter is Z, the equivalent value of PWM duty cycle variable will be 0.25x Z

It means that ever be the value of A/D converter, it will be multiplied by 0.25 and the value will be set as duty cycle variable value. This correctness of this relation is verified in simulation

If you have any question regarding speed and direction control of DC motor using microcontroller project please ask in comments. Don’t forget to share it with your friends.

About Syed Noman ud din

Syed Noman ud din is an Electrical Engineer and working in Industry from last 3 years. He writes technical articles for electrical and electronic engineers. He has also published several research publications in renowned international journals.

Check Also

Power Factor measurement using Microcontroller

The article will help the readers to use microcontroller for power factor measurement using a …

Leave a Reply

Your email address will not be published. Required fields are marked *

*