DC motor and Fan speed control using pic 16f877 Microcontroller

Here is a simple project on how to control fan or dc motor speed uisng pic 16f877 microcontroller. There are numerous ways to control the speed of motor(or fan). Varing current,voltage and resistance etc. But when it comes to controlling the speed using microcontrollers. Then the PWM(Pulse width modulation) technique is used. In Pulse width modulation the digital signal high and low time is chaged. Actually it is made variable. So if the digital signal is high for 70% of the time and low for 30% of the time then the motor rotates more quickly because the signal is high for most of the time. Setting the pwm for different times can make you to control the motor speed.

DC motor and Fan speed control using pic 16f877 MicrocontrollerProject Requirements

  • PIC 16f877 microcontroller
  • L293d Motor driver Ic
  • DC Motor or small fan
  • Crystal 20MHz
  • Push Buttons
  • connecting wires
  • Bread board or PCB
  • Power Supply battery

Their are two pins dadicated for generating pwm output in pic 16f877 microcontroller. These two pins are of port c pin 1 and 2. They are named as CCP1 and CCP2 (CCP stands for Capture,compare and PWM). These two pins can be used in capture, compare and Pwm mode.They can also be used as digital I/O. We first have to configure them whether we want to use them in CCP mode or as digital I/O.

To Control and configure CCP1, CCP1 register is used. CCP1 is a 16-bit register which is further divided in to two 8-bit registers CCPR1H(Capture,compare,PWM register 1 high byte) and CCPR1L(Capture,compare,PWM register 1 LOW byte).These two registers are controlled by CCP1CON(Capture,compare,PWM 1 control) register.Alternatively CCP2CON is used to control the CCP2 module.
The PWM signal generated by PIC 16f877 provides 10-bit resolution. Which means it can calculate up to 2^10 = 1024. The 10 bit value is loaded in CCPR1L and CCP1CON<5:4> registers. Now Set the Period and duty cycle for the signal. The period is set in PR2 register. PR2 is an 8-bit register. Timer 2 runs from 0 upto the value loaded in PR2 register. Upon reaching the value it starts again from 0. When all the values are loaded in the registers then run the timer to generate the Pwm. Dont forget to set the timer prescaler. I set it to 4. 
DC motor and Fan speed control using pic 16f877 Microcontroller SchematicTo drive a dc motor or fan through the generated pwm wave you need to build a motor driver circuit. Microcontroller output voltage is very low and it can not drive huge loads like dc motors and fan etc. Make an H-Bridge circuit to drive motor or simply use L293d ic to drive the motor. L293d is a motor driver ic which can drive heavy loads. Full description of the ic with pin out is given in this tutorial (L293d PIN OUT AND WORKING). 

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter