Speed control of DC motor using pic microcontroller

This tutorial is about dc motor speed control with pic microcontroller using PWM method. There are many application of DC motors, where we need a variable speed of DC motor. For example DC motor have a applications in electrics cars, trucks and aircraft.These are three examples where we need variable speed of DC motor. Although now a days AC motors have more use in market for variable speed application over DC motor due to enhancement in power electronics and solid state electronics.But DC motors ares till used today in many applications for variable speed.

Types of DC motors

According to armature and field current circuit configuration there are five types of DC motor:

  • DC motor with separate excitation
  • DC motors with shunt
  • Series DC motors
  • Compound DC motors
  • DC motors with excitation from permanent magnet

The speed of separately excited DC motor and DC shunt motor  can be made variable by changing terminal voltage and by changing the armature resistance. The speed of DC series motor and permanent magnet DC motor can only be changed by changing armature terminal voltage.While Compound  DC motor have both characteristics of DC series motor and DC shunt motor.

So there is one thing common in all types of DC motors. Speed control by changing armature terminal voltage is same in all types of DC motors.Now to make this project you have to learn a method through which we can apply variable DC voltage to armature of any DC motor so that we can control speed of DC motor according to our requirement.

Methods to generate variable voltage:

  • Variable power supply
  • Pulse width modulation technique

In variable power supply there are some limitation but still speed of  very small power DC motors can be controlled by making a variable power supply with the help of  voltage regulators which are used to generate variable voltage with in a specified limit provided by manufacturer.There are many voltage regulators in market.But there will be issue of power rating of DC motor. Because power supplies which make with such voltage regulators have not too much high power handling capability and power supply is main source of power to DC motor. Therefore this method is not practically feasible and recommended.

Second method, pulse width modulation is the best method to control DC voltage applied to terminal of armature.In pulse width modulation we actually control switching duty cycle, which is the ratio of on time to the total time of switching.

Pulse width modulation

This is a method to control the output voltage with the of constant frequency switching and by adjusting on duration of switching and in other words by changing duty cycle of switching.

                                    Constant switching time period = on time + off time                                                                                 Duty Cycle = on time / on time + off time %

Duty cycle can not be greater than 1 or 100%. Because on time will always be less than total timer period of switching frequency .The relationship of input-output voltage and duty cycle is

                                                output voltage =  duty cycle * input voltage

Hence output voltage and duty cycle is directly related with each other but their output also depend on switching frequency of switch. What is a switch here.I will discuss it later.

Different Pulse width modulation having different duty cycles
Different Pulse width modulation having different duty cycles

In above picture there are four PWM having different duty cycle.So we need to change duty cycle according to requirement of output voltage.But output voltage can never be greater than input voltage. But now the question which into mind. how to generate PWM and how to use PWM for variable output voltage generation. Check this introduction to pulse width modulation tutorial for further details.

 PWM Generation Methods

There are two ways to generate pulse width modulation with variable duty cycle:

  1. Using analog electronics ( operational amplifier , comparator and saw tooth wave etc)
  2. Using digital electronics  ( microcontrollers and dedicated PWM controllers IC’s )

I will not discuss first method in this article. but in future I will write a complete post on Pulse width modulator using analog electronics. But those who don’t know about programming and micrcoontrollers can use analog electronics method for this purpose.

Circuit Diagram speed control of dc motor

To control DC motor speed with  PIC microcontroller , you should know how to generate PWM using PIC microcontroller and how to use analog channel of PIC micrcontrollerto to read  analog Voltage.If you don’t know about these topics. Kindly visit following link to get the understanding of PIC microcontrollers and PWM programming.

I have already mentioned there are two methods to make pulse width modulator.These are Analog Electronics method and digital Electronics method using Microcontrollers. There are dedicated PWM controller IC’s are also available in market like SG3525, UC3842, SG2525 and many others. These PWM controller IC’s also use analog electronics method. Because their internal circuitry consists of operational amplifiers , resistors and capacitors.I will write also write article on PWM controllers for those who hate programming and don’t know about use of Microcontrollers. But there are few issues with these ready to use PWM controller IC’s. Most important issue are:

  1. you will require many extra components to make them ready to use.
  2. Exact frequency is not achievable. 1-2% error
  3. There is also a frequency limitation which in some cases generate audible in motors.

But there is no such issues with pulse width modulators of digital electronics type.

Components List

  1. 7805 voltage regulator
  2. resistors
  3. PIC16F876 MICROCONTROLLER
  4. IRL1004 FET
  5. UF4007 DIODE
  6. 10MHZ CRYSTAL
  7. 1K Variable resistor
  8. 22pF capacitors

I have used PIC16F876 microcontroller to generate PWM and to change duty cycle by reading analog value of voltage across variable resistor. you can use any microcontroller you want. But logic will remain same.

Circuit diagram of speed control of DC motor
Circuit diagram of speed control of DC motor

In above circuit diagram 12v is used as a power source.7805 voltage regulator is used to give 5 volt to variable resistor and supply to microcontroller. There is no need to give supply 5 volt in Proteus to microcontroller. Because by default 5 volt  and ground has been supplied to microcontroller in Proteus.Variable resistor is used to change duty cycle of PWM. By moving the knob of variable resistor you can change the duty cycle which is directly related with speed of DC motor. So you can adjust the speed of DC motor by adjusting variable resistor knob according to your speed requirement.

Circuit diagram of speed control of DC motor
Circuit diagram of speed control of DC motor

Video lecture on dc motor speed control

The PWM frequency is about 8Khz which will not make any audible noise.You can adjust it according to your motor specifications. IRL1004 is used as a switch. IRL1004 FET require only 5 volt logic level driver to operate FET and negligible amount of current.Therefore there is no need of any driver to drive IRL1004 FET. It can handle more than one ampere current very easily as it is mentioned in its data sheet. Diode 1N4007 is used as a free wheeling  fast recovery diode.Because motor is a inductive load and its back emf may damage circuit.Free wheel diode will provide flow path to back emf current and avoid sparking across motor terminals.

Code for speed control of dc motor

long ADCValue=0;
long ADCValueOld=1;

#byte portA = 0X05
#byte portB = 0X06
#byte portC = 0X07

void main()
{
set_tris_A(0b00101011);
set_tris_B(0b00000001);
set_tris_C(0b00000000);

portA=0X00;
portB=0X00;
portC=0X00;

setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
setup_spi(FALSE);
setup_counters(RTCC_INTERNAL,WDT_288MS);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_timer_2(T2_DIV_BY_1,255,1);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_PWM);
enable_interrupts(global);

set_adc_channel(0);
delay_us(10);

while(1)
{
ADCValue = Read_ADC();
delay_ms(100); // monitor 10 times a second

if ( ADCValue != ADCValueOld )
{
set_pwm2_duty(ADCValue);
ADCValueOld = ADCValue;
}
}
}

If you want to learn programming of PWM and ADC (analog to digital converter) using PIC Microcontrollers. Check PIC mircontrollers tutorails.

PIC Microcontrollers Tutorials 

PIC microcontroller Projects 

If you want complete simulation and code of this project.Comment on this post with your email address and if you have any issue while making this project, feel free to comment on this post.

Future Recommendations :

  1. Feedback control mechanism to ensure constant speed
  2. Direction control of DC motor using H bridge
  3. Try to control more than one motors.
  4. Final year projects: Speed control of DC motor through wireless communication like GSM, GPRS, Bluetooth, ZigBee and many others wireless communication techniques.

I will also write on above articles soon. But to enhance your technical skills you should try it yourself.If you feel any problem.I will love to hear your problems and try to solve them.

67 thoughts on “Speed control of DC motor using pic microcontroller”

  1. Hi Bilal, can you please send me the full project simulation and the source code, very much interested.
    Thank you

  2. i love this tutorial and this particular topic was given to me design and construction of microcontroller based speed of a DC motor. so I just want to ask if I can get the complete write up and guide

  3. hello,
    i am very interrested in this project as i am working on the same topic, DC motor speed control with thyristors interfacing circuit.
    i would like to receive the project files for this application.
    here is my mail adress: [email protected]
    thank you for your help

  4. It was a great experience to see this tutorial , please email me the code of this project . I will be so grateful to you .

  5. Hi Bilal ,
    tutorial based on controlling dc motor speed woth pic was quiet interesting.
    can u please send a code as i need this concept for my mini project.

  6. Hello Sir,
    Thank you for this tutorial. I hope it will help me a lot to understand the speed control technique of dc motor. Sir I want to control the speed of 220V DC Shunt motor using same technique. So can you please me what all the changes I need to make in the circuit as well as the code for the microcontroller. Thank you so much in advance. Hope to hear soon from you.

  7. hello sir,i went to do a program for dc motor control by using ardiuno.so, please help me .how i can be done this program?

  8. Wow great tutorial!!!

    Do you mind to share and PM the code??? Currently I’m stuck at the code since there’s no input to AN0 from potentiometer. And my project got 2 potentiometer which is need to use AN0 and AN1, I’m using PIC16F877A

Leave a Comment

shares