Loading...
Pages: [1]   Go Down
Author Topic: Controlling/fading the Sainsmart 3.2" UTFT Backlight  (Read 575 times)
0 Members and 1 Guest are viewing this topic.
Melbourne, Australia
Offline Offline
Newbie
*
Karma: 1
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
#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;
  }


Logged

France
Offline Offline
God Member
*****
Karma: 19
Posts: 617
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good job berniew smiley

I will do the same modification soon.

Karma ++ smiley
Logged

Melbourne, Australia
Offline Offline
Newbie
*
Karma: 1
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks. Karma to you too for your original info & code. (took me a while to figure out how to work the karma ++ thing)
Logged

Pages: [1]   Go Up
Print
 
Jump to: