Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering

DC motor and Fan speed control using Pic16f877 Microcontroller using PWM(Pulse width modulation) Technique

By EG Projects

Here is a simple project on how to control fan or dc motor speed with microchip pic16f877 microcontroller. There are numerous ways to control the speed of motor(or fan). Varying current, voltage and resistance etc. But when it comes to controlling the speed using microcontrollers. Then the PWM (Pulse width modulation) technique is most effective one. Pulse width modulation reduces the direct current/voltage pulse duration, which ultimately reduces the output current and voltage at output. In pulse width modulation the digital signal high and low time period is varied to output a desired voltage. Varying the dc voltage in pulses is know as duty cycle.  A typical pulse width modulation diagram is show below with different duty cycles.
Varying duty cycle signals are shown in the pic on the right side. High pulse is generally termed as duty cycle. Original signal high pulse is 65% and low 35%. Thus original signal duty cycle is 65%. The work is only done in duty cycle period. Varying duty cycle signals are shown below the original. Duty cycle of easy signal is different then others. 70%, 30% and 90% respectively.
PWM(Pulse width Modulation) waves

PWM(Pulse width Modulation) waves

Pic microcontroller speed control project requirements

  • Pic16f877 microcontroller
  • L293d h-bridge 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 dedicated for generating pwm output in pic16f877 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 (input/output). We have to configure some registers to achieve this.

Registers associated with CCP1 & CCP2 Module.
PIC16f877 CCP module Hirerachy

PIC16f877 CCP module Hirerachy

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.
CCP1CON and CCP2CON register bits

CCP1CON and CCP2CON register bits

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. 
Note: Only Timer 2 can be used for PWM generation in Pic16f877.
Period and duty cycle wave forms

Period and duty cycle wave forms

To 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). 
The circuit diagram of the project is given below. Six Push buttons are attached with port A of the pic16f877 microcontroller. These push buttons are used to change the speed of the motor. Their are six speed levels press these push buttons to change the speed of fan or motor. CCP1 pin is attached to L293d pin no 10. The motor rotates in anti clock wise direction when power is turn on. You can drive the motor in clock wise direction, just connect CCP1 with pin 15 of L293d and make pin 10 ground. 20 Mhz crystal is used in the project. Using a crystal of low frequency can also effect the motor speed.
Speed control of DC motor with Pic 16f877 microcontroller and l293d motor driver ic - Circuit Diagram

Speed control of DC motor with Pic 16f877 microcontroller and l293d motor driver ic – Circuit Diagram

The code part is simple. It is written in c language using Mplab and HTC(high tech c compiler). First include the header file htc.h. This header file is necessary to be included if you are using htc compiler. Then the crystal frequency is defined which is 20 Mhz. Then individual pins are defined for each button. In the main function ADCON1=0x06 instruction initializes all pins of port A as digital. This instruction is very important. Port A of pic16f877 is multiplexed. It can be used as digital I/O as well as Analog input pins. ADCON1 register is used to configure port A to be used as ADC or digital I/O. Loading 0x06 in ADCON1 makes port A all pins as digital I/O. Now after initializing port A pins as digital I/O you need to initialize them as digital input or output. TRISA register is used to initialize them as digital input or output. Loading TRISA=0b11111 will initialize all pins as digital input. TRISC2 register is initializing port c pin 2(CCP1) as output.PR2 register is used to set timer 2 period. I loaded 0xFF in it, which means timer increments uptill 255 and then back starts from 0.

In rundutycycle() function pwm signal in generated. The function of each instruction is given with it. The status of switches s1 to s6 are also checked in this function and duty cycle is varied according to the switches status. T2CON is timer 2 control register. To set its bit functions please refer to the data sheet of pic 16f877 microcontroller. 

Download the project code, hex file(code) and simulation. Folder includes all the project files. The code is compiled using htc(high tech c compiler) and mplab . Simulation is made in proteaus 8.0.Please give us your feed back on the project.

DC motor speed control with Pic 16f877


Filed Under: Microcontroller Projects, PIC Microcontroller

 

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • PS2 Keyboard To Store Text In SD Card Using Arduino Circuit Setup On Breadboard
    How To Use PS2 Keyboard To Store Text In SD Card Using Arduino- (Part 42/49)
  • Wireless Path Tracking System Using Mouse, XBee And Arduino Circuit Setup On Breadboard
    How To Make A Wireless Path Tracking System Using Mouse, XBee And Arduino- (Part 43/49)
  • How to Make a Wireless Keyboard Using Xbee with Arduino- (Part 44/49)
  • Making Phone Call From GSM Module Using Arduino Circuit Setup On Breadboard
    How to Make Phonecall From GSM Module Using Arduino- (Part 45/49)
  • How to Make a Call using Keyboard, GSM Module and Arduino
    How To Make A Call Using Keyboard, GSM Module And Arduino- (Part 46/49)
  • Receiving SMS Using GSM Module With Arduino Prototype
    How to Receive SMS Using GSM Module with Arduino- (Part 47/49)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • Renesas delivers intelligent sensor solutions for IoT applications
  • Microchip Technology releases AVR-IoT Cellular Mini Development Board
  • Qualcomm acquires Cellwize to accelerate 5G adoption and spur infrastructure innovation
  • MediaTek’s chipset offers high-performance option for 5G smartphones
  • Nexperia’s new level translators support legacy and future mobile SIM cards

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr bluetooth dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • Fixing hold violations in Innovus without inserting delay on setup timing paths
  • SDC constraints for MUXed clock input + clock divider + MUXed clock output
  • DC Motor Speed Control
  • D Flip Flop frequency divider
  • FPGA LVDS with separate clock

RSS Electro-Tech-Online.com Discussions

  • intro to PI
  • Are Cross-wind compensation and Road crown compensation functions inputs to LKA function?
  • we are facing an issue with one-wire communication by using DS2485
  • RF modules which can handle high number of bytes per second
  • Component identification.
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering