Friday, 16 August 2013

Digitally controlled DC fan using PWM (Code + Schematic)

This post will provide a practical example of how to control DC fan speed digitally. The speed of Fan is controlled trough PWM (Pulse Width Modulation) by varying the duty cycle. I am using 16Bit PIC microcontroller but same code will work on all PIC except the PWM module functions.

You may download code and schematic from the download section at the bottom of this post. I am using MPLAB X IDE, C30 Compiler and Schematic is in PDF. This code is written in C Language and will work on PIC24, DsPIC33 and DsPIC30 (16bit microcontroller) by slightly changing the code.

In this post I supposed that you know following if not then please read them before proceeding further in this post.

1)      How to interface LCD with PIC (Click here).
2)      How to generate PWM using PIC (Click here).
3)      How to interface Push button with PIC (Click here).

Experimental Setup and circuit:
As I am using DsPIC33F custom I/O board and a LCD. LCD is connected with PortE through Level translators as LCD has working voltage of 5.0VDC while PIC is running on 3.3VDC. You may use this single NMOS based Level translators. PWM is generated through OC1 (Output compare 1). I connected the fan’s positive terminal with 12VDC and negative with collector of NPN transistor BD135 (12W transistor). Base of this transistor is connected with OC1 through a resistor of 2kΩ. I also connected two push buttons with RD14 and RD15. These are for increasing and decreasing Duty cycle of PWM signal. In the following figure 1. You have seen that when a switch is connected to 3.3VDC the current flows through the DC fan. This means that in ON (High) condition fan will rotate. And when you connect this switch to Ground the NPN transistor is OFF and hence there will be no current flowing through the transistor. Arrows are showing the flow of current. This simple animation demonstrating the effect of High and LOW signals to NPN and fan. So this is the science now this ON, OFF sequence is controlled through PWM Duty cycle larger will be the high signal more will be the speed of fan. So by increasing ON width of the signal we will increase the speed and vice versa.

Figure 1. Fan control.

In the following figure 2. An animations shows how LCD is updated when we increase and decrease the PWM and ultimately the speed of the FAN.

Figure 2. LCD Updates.
In the following figure 3. Here are some pictures of how PWM’s duty cycle changes when we push the buttons up/ down.

Figure 3. PWM variation.

        In the following figure 4. This is the main function. In which first I Initialize the PWM module for frequency 152Hz. Remember this controller is currently running at 10MHz with cycle frequency of 5MHz. So we have 15bits resolution for our PWM. Then I initialize the LCD working in 8bit mode. Then we have some strings to display. After that I enable the Internal Pullups resistors for push buttons. And also configure its PINs as an input. Then again some strings are displayed on LCD. Now in this loop I continuously watch the push buttons if one of them is pressed for example in case of up button when pressed. I quickly increase the PWM DutyCycle variable which is then passed to Set PWM. The If statement before increasing duty cycle variable act as a guard so that duty cycle could not increase more than 100%.  Now to show duty cycle as percentage as well as in a symbolic representation on LCD I called function named ShowSymbolicPercentageOnLCD() which actually requires two variables to pass on. One is duty cycle and other is the 100% dutycycle value. These variables helps this function in calculating percentage and display them as a symbols on LCD quite amazing.

Figure 4. Main function Fan control.
In the following figure 5. you will find a Schematic for DsPIC33Fj with Fan and LCD connected.

Figure 5. Schematic for Digitally controlled Fan.

For having a clear picture of experimental setup please watch this video.


You can download Code (MPLAB X and C30 compiler) and Schematic. Click here

Reading Suggestions:

·         Over Voltage Protection
·         AC Voltmeter RMS + Peak voltage 
For all topics Click here:
That’s all for this post hope you will learn please comment if you have any questions for upcoming posts please subscribe or follow.

All the text and graphics contained on this blog belongs to owner except otherwise mentioned. Other parties' trademarks and service marks that may be referred to herein are the property of their respective owners. Reproducing or distributing text and graphics on your own site is strictly not allowed without proper linking to original content and before publishing that you should ask for permission. 
© Copyright 2013 IbrahimLabs. All rights reserved.

Getting started with Arduino coding. (A beginner’s quick guide)

Welcome to my first Arduino coding guide. This would take you to kickstart with Arduino boards. I would go through following things real...