Controlling an LCD backlight - example using a mosfet
Posted: Mon Aug 25, 2014 12:32 am
A recent project as required me to use an LCD (cheap 16x2) but I needed a way of controlling the LCD backlight, mostly to save power when the PIC is in sleep mode.. The LCD backlight only needed to either be fully on or completely off so a good transistor could have been used, but I decided to make the LCD backlight more controllable by using a mosfet (2N7000) and a PWM channel from a PIC, this would allow me to completely control a good range of brightness and to turn the backlight on/off..
Below Fig1 shows a common way of connecting up a LCD backlight and Fig2 shows how to connect up a mosfet to the LCD backlight.
Example Swordfish code:
Example Video:
Below Fig1 shows a common way of connecting up a LCD backlight and Fig2 shows how to connect up a mosfet to the LCD backlight.
Example Swordfish code:
Code: Select all
Device = 18F2553
Clock = 20
#option LCD_DATA = PORTB.4
#option LCD_EN = PORTA.4
#option LCD_RS = PORTA.5
Include "LCD.bas"
Include "PWM2.bas"
Dim LcdBrightness as byte
TRISB = %00000111
ADCON1 = %00001111
CMCON = %00000111
LcdBrightness = 0
lcd.writeat(1,2,"LCD Backlight")
lcd.writeat(2,1,"PWM Controlled!")
PWM.SetFreq(5000)
PWM.Start1
PWM.SetDuty1Percent(LcdBrightness)
While True
repeat
pwm.setduty1percent(LcdBrightness)
inc(LcdBrightness)
delayms(50)
until LcdBrightness = 100
repeat
pwm.setduty1percent(LcdBrightness)
dec(LcdBrightness)
delayms(50)
until LcdBrightness = 0
Wend