DC motor Speed control using pic microcontroller

This tutorial is about dc motor speed control with a pic microcontroller using the PWM method. There are many applications of DC motors, where we need a variable speed of DC motor. For example, it has applications in electric cars, trucks, and aircraft. These are three examples where we need variable speed. Although nowadays AC motors have more use in the market for variable speed applications over DC motor due to enhancement in power electronics and solid-state electronics. But,  direct current rotational machines are still used today in many applications for variable speed requirements.

Types of DC motors

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

• DC motor with separate excitation
•  Shunt Type
• Series
• Compound ( Series+shunt)
•  With excitation from a 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 the DC series motor and permanent magnet DC motor can only be changed by changing the armature terminal voltage. Conversely, the Compound  DC motor has both characteristics of DC series motor and DC shunt motor.

How to Control DC Motor Speed?

First of all,  there is one features common in all types of DC motors. The rate of acceleration depends on the magnitude of the armature terminal voltage. Therefore, We can use this common feature to control speed. Speed control by changing armature terminal voltage is the same in all types of DC motors.  In short, we can apply the same method to all direct current rotational machines.

At the start, to make this project possible, we have to learn a method through which we can apply variable DC voltage to the armature of any DC motor. Thereafter, we can control the speed of DC motor according to our requirements.

Methods to generate a variable armature voltage

We can generate variable dc voltage by two methods. Firstly, we will discuss the variable power supply method. After that, we will briefly go through the pulse width modulation technique.

Variable Power Supply Method

In variable power supply technique, there are some limitations. But we can still use it for small power DC motors.  It can be controlled by making a variable power supply with the help of voltage regulators. They are capable to generate variable voltage within a specified limit provided by the manufacturer. There are many voltage regulators in the market. But there will be an issue of the current rating of DC motor. Because power supplies that make with such voltage regulators have not too much high power handling capability. A power supply is the main source of power to DC motor. Therefore this method is not practically feasible and recommended. This technique is costly also.

Pulse Width Modulation Technique

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

PWM for DC Motor Speed Control

PWM Introduction

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

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

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

`output voltage =  duty cycle * input voltage`

Hence the output voltage and duty cycle is directly related to each other. However,  their output also depends on switching frequency of switch. What is a switch here. I will discuss it later.

In the above picture, there are four PWM having different duty cycles. Therefore,  we need to change the duty cycle according to the requirement of the output voltage. But output voltage can never be greater than the input voltage.  At last, if you have still trouble in grasping these concepts, read this complete guide:

PWM Generation Methods

But now the question which must come into mind. How to generate PWM and how to use PWM for variable output voltage generation?  At the present time, there are two ways to generate pulse width modulation with variable duty cycle:

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

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

Dc Motor Speed Control  using pic microcontroller

To control DC motor speed with  PIC microcontroller, we should know about these two features:

• how to generate PWM using PIC microcontroller
• how to use the analog channel of PIC microcontroller to read analog voltage.

If you don’t know about these topics. Kindly visit the following link to get the understanding of PIC microcontrollers and PWM programming.

As we mentioned earlier,  there are two methods to make a pulse width modulator. These are the analog Electronics method and the digital Electronics method using Microcontrollers.

Dedicated PWM controller ICs for Speed control

There are dedicated PWM controller IC’s are also available in the market like SG3525, UC3842, SG2525, and many others. These PWM controller IC’s also use analog electronics methods. Because their internal circuitry consists of operational amplifiers, resistors, and capacitors. I will write also write an article on PWM controllers for those who hate programming and don’t know about the use of Microcontrollers. But there are few issues with these ready to use PWM controller IC’s. A most important issue is:

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

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

DC Motor Speed Control Circuit diagram

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

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

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

Video Lecture Demonstration

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 requires the only 5-volt logic level driver to operate FET and a negligible amount of current. Therefore there is no need for any driver to drive IRL1004 FET. It can handle more than one-ampere current very easily as it is mentioned in its datasheet. Diode 1N4007 is used as a freewheeling fast recovery diode. Because the motor is an inductive load and its back emf may damage the circuit. Freewheel 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;

#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_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);

delay_us(10);

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

{
}
}
}```

If you want to learn to program of PWM and ADC (analog to digital converter) using PIC Microcontrollers. Check PIC microcontroller tutorials.

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 motor.
4. Final year projects: Speed control of DC motor through wireless communication like GSM, GPRS, Bluetooth, ZigBee, and many other wireless communication techniques.

81 thoughts on “DC motor Speed control using pic microcontroller”

1. I like your tutorial, please could you send me the code?

2. I am very much interested about your project. Please send me this project details & also source code. I am new in microcontroller.

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

4. Please provide me the simulation file of proteus & source code with hex code.

5. Please i need the simulation file & the code .

• Hi i am Daniel. I am studying mechatronics final year and i looking for project ideas.

6. i am very much intrested in this project. kindlysendme the code and simulations plz.. its urgent.. i will be thankful.

• plz how can I use this to control brushless through the 30-60amp.
plz friend don’t leave me a shame I’am waiting “facebook:Bashir Abdullah AK” & “twitter:Bashir Abdullah AK@Engr_BAAK”
thanks!!!!

7. I want to know how to make it on proteus 8

8. 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

9. pls. I want the complete stimulation and code on this project help me to send it to my email akin4luv@ymail.com

10. pls I need the simulation file and the code help me pls my email

11. 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: fz.elasri@gmail.com

12. Pls i want the complete code for this project. email me on wolex771@GMAIL

13. I want the complete stimulation and code on this project please help me by sending it to my email

14. Hi…I am interested in your project…please I’d like to see the codes…my email is lavick3@gmail.com Thanks

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

16. 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.

17. 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.

18. Hello Sir,
I would like to know if the code is available for Download.
Thanks you 🙂

19. Good post. please send the complete simulation and the codes via my e-mail: sijtade@gmail.com

20. can i get the codes……..thanks sir

21. hello Please give the full code and simulation
Thank you

22. hello Please give the full code and simulation
Thank you

23. Hi,Thanks for your tutorial ,these are very helpful for power electronics students.Would you please provide me full code and simulation through this email bhuiyanmdhabib@gmail.com

Thanks
Habib

24. 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?

25. 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

26. hello, C code and circuit diagram can you send?

27. it was nice tutorial

28. Please, I want pic microcontroller software to control the speed of treadmill motor at constant speed

• Hi
First of all, you need to figure out what kind of motor is used in a treadmill.

29. plz sent me complete file of this project on my email

30. I warmly welcome!
I am in the process of making PWM control of an electric motor for a 600W boat. I will ask for a code to program the PIC
Thank you

31. Hello, thanks for your interesting project. Please send me the full file and code of your project.

32. Kindly send me the whole simulation and code of this project

33. Hello, please send me the whole simulation and code files of this project.

34. Please provide me the simulation file of proteus & source code with hex code.