Digital Bulb Intensity Controller using PIC. (Code + Schematic)

This post will provide a practical example of how to control intensity of a DC bulb digitally. The intensity of bulb is controlled through PWM (Pulse Width Modulation) by varying a 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 works on of 5.0VDC while PIC is running on 3.3VDC. You may use this single NMOS basedLevel translators. PWM is generated through OC1 (Output compare 1). I am using a parking lights car bulb which is connected with 12VDC and second terminal is connected with collector of NPN transistor BD135 (12W transistor). Base of this transistor is connected with an opto-coupler because bulb needs higher current so at higher current this transistor needs some extra amount of current on its base which is not provided by PIC Pin. That’s why I used optocoupler. You may use a transistor instead of optocoupler it will also work. Now the LED side of optocoupler PC817 is connected with OC1 through a resistor of 220Ω. Complete circuit is given in following figure 1. I also connected two push buttons with RD14 and RD15. These are for increasing and decreasing Duty cycle of PWM signal.

Figure 1. Schematic Digitally controlled bulb Intensity.

 In the following figure 2. You have seen that when a switch is connected to 3.3VDC the current flows through the Bulb. This means that in ON (High) condition bulb will glow. And when you connect this switch to Ground the NPN transistor is OFF and hence there will be no current flowing through the bulb. Arrows are showing the flow of current. This simple animation demonstrating the effect of High and LOW signals to NPN. So this is the science now this ON, OFF sequence is controlled through PWM Duty cycle. Larger the duty cycle larger the bulb will glow. Because ON cycle is larger so current will flow for larger time as compare to smallest value of Duty cycle. So by increasing ON width of the signal we will increase the speed and vice versa.

Figure 2. bulb Intensity controller.

In the following figure 3. An animations shows how LCD is updated when we increase and decrease the PWM and ultimately the intensity of bulb. This percentage shows the intensity of light bulb. Which is increased or decreased by using UP or down buttons.

Figure 3. LCD updates.


        In the following figure 4. This is the main function. In which first I Initialize the PWM one 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 bulb Intensity controller.

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.

Popular posts from this blog

Defining code regions in MPLAB X IDE

Digital AC voltmeter Schematic + Code + Proteus Simulation

Graphical LCD interfacing with PIC16F877 Code + Schematic + Proteus Simulation