This is a related thread to
"Can the Sainsmart 3.2"TFT be turned off?"Suggestions on that thread helped me make a simple hardware mod to the shield board that uses the PWM pin 8 of the Arduino Mega 2560 to control the brightness of the LCD backlight.
The trimpot is removed and a PNP transistor Collector & Emitter connected between the +3.3V and centre pads. The trimpot GND is ignored.
A 1K resistor is connected between the transistor base and the PWM pin 8.
Also, one or more header pins need to be added to the shield to bring the PWM pins to it from the Mega 2560.
Simple diag attached below.
Writing different values between 0 and 255 to this output pin allow control of the brightness of the backlight.
The relationship between the number written to the pin and the apparent brightness is clearly non-linear, not surprising given the various factors involved.
Here's a demo sketch.
#include <UTFT.h>
const int backlight_pin = 8;
const int backlight_on = 0;
const int backlight_off = 255;
const int backlight_half = 127;
const int backlight_quarter = 192;
const int backlight_3quarter = 64;
extern uint8_t BigFont[];
UTFT myGLCD(ITDB32S,38,39,40,41); // Remember to change the model parameter to suit your display module!
// initialisation
void setup() {
// Setup the LCD
myGLCD.InitLCD();
myGLCD.setFont(BigFont);
myGLCD.lcdOn();
analogWrite(backlight_pin, backlight_on);
drawBackground();
}
// Fool with the backlight
void loop() {
screenMessage(" Full Backlight ");
analogWrite(backlight_pin, backlight_on);
delay(3000);
screenMessage(" Half Backlight ");
analogWrite(backlight_pin, backlight_half);
delay(3000);
screenMessage(" Quarter Backlight ");
analogWrite(backlight_pin, backlight_half);
delay(3000);
screenMessage(" Fade Down ");
for(int i=0;i<256;i++) {
analogWrite(backlight_pin, i);
delay(25);
}
screenMessage(" Fade Up ");
for(int i=255;i>-1;i--) {
analogWrite(backlight_pin, i);
delay(25);
}
}
// Display the backlight state
void screenMessage(String message) {
myGLCD.setColor(255,255,255);
myGLCD.setBackColor(0,0,0);
myGLCD.print(message, CENTER, 113);
}
// Put some mixed colours in
void drawBackground() {
myGLCD.clrScr();
int r=255;
int g,b=0;
for(int y=0;y<100;y+=20){
myGLCD.setColor(r, g, b);
myGLCD.fillRect (0,y,319,y+10);
r-=16;
g+=16;
b+=16;
}
r=0;
g,b=255;
for(int y=226;y>100;y-=20){
myGLCD.setColor(r, g, b);
myGLCD.fillRect (0,y,319,y-10);
r+=16;
g-=16;
b-=16;
}
}