This sample program demonstrates how to use the ESP32's LEDC (LED Controller) PWM functionality to control the backlight brightness of a TFT LCD screen. By touching the slider on the screen, users can adjust the backlight brightness in real-time, achieving stepless dimming from 0 (off) to 255 (maximum brightness).
This sample code is based on the ESP32-WROOM-32E development board, using the TFT_eSPI library to drive the ILI9341 LCD screen and touch functionality.
The following introduces the basic configuration of the program:
TFT_eSPI.h (core graphics driver library) and TFT_Touch.h (touch driver library) for LCD display and touch detection functionality.BACKLIGHT_PIN (GPIO 21): Backlight control pin, outputs PWM signalRTP_DOUT (GPIO 39), RTP_DIN (GPIO 32), RTP_SCK (GPIO 25), RTP_CS (GPIO 33): Touch screen interface pinsfreq = 2000: PWM frequency of 2000 Hzchannel = 0: Uses LEDC channel 0resolution = 8: 8-bit resolution, range 0-255 (256 brightness levels total)TFT_eSPI my_lcd and TFT_Touch my_touch objects for LCD display and touch detection respectively.The setup() function completes the initialization of the serial port, LCD screen, and touch screen, and draws the UI elements of the backlight control interface.
void setup()
{
Serial.begin(115200);
my_lcd.begin();
my_lcd.setRotation(1);
my_touch.setCal(495, 3398, 721, 3448, 320, 240, 1);
my_lcd.fillScreen(TFT_WHITE);
my_lcd.setTextColor(TFT_BLACK, TFT_WHITE);
my_lcd.setTextSize(2);
my_lcd.setCursor(my_lcd.width()/2-150, 160);
my_lcd.println("0");
my_lcd.setCursor(my_lcd.width()/2+110, 160);
my_lcd.println("255");
my_lcd.fillRoundRect(my_lcd.width()/2-138, 130, 276, 21,10,0x07FF);
my_lcd.fillCircle(my_lcd.width()/2+128, 140,10,TFT_MAGENTA);
my_lcd.setTextColor(TFT_RED);
my_lcd.drawNumber(255, my_lcd.width()/2-15, 65,2);
ledcAttachChannel(BACKLIGHT_PIN, freq, resolution, channel);
ledcWrite(BACKLIGHT_PIN, 255);
}
Serial.begin(115200);
Sets the serial baud rate to 115200 for debugging information output.
my_lcd.begin();
my_lcd.setRotation(1);
my_lcd.begin(): Initializes the TFT_eSPI library, configures the SPI interface and LCD driver chipmy_lcd.setRotation(1): Sets the screen to landscape display mode (rotated 90°)my_touch.setCal(495, 3398, 721, 3448, 320, 240, 1);
Sets touch calibration parameters to map the touch screen's raw analog coordinates to screen pixel coordinates. The parameters (495, 3398, 721, 3448, 320, 240, 1) correspond to X-axis minimum, X-axis maximum, Y-axis minimum, Y-axis maximum, screen width, screen height, and rotation direction respectively.
my_lcd.fillScreen(TFT_WHITE);
my_lcd.setTextColor(TFT_BLACK, TFT_WHITE);
my_lcd.setTextSize(2);
my_lcd.setCursor(my_lcd.width()/2-150, 160);
my_lcd.println("0");
my_lcd.setCursor(my_lcd.width()/2+110, 160);
my_lcd.println("255");
my_lcd.fillRoundRect(my_lcd.width()/2-138, 130, 276, 21,10,0x07FF);
my_lcd.fillCircle(my_lcd.width()/2+128, 140,10,TFT_MAGENTA);
my_lcd.setTextColor(TFT_RED);
my_lcd.drawNumber(255, my_lcd.width()/2-15, 65,2);
Sequentially draws the following UI elements:
fillScreen(TFT_WHITE) sets the screen background to whiteledcAttachChannel(BACKLIGHT_PIN, freq, resolution, channel);
ledcWrite(BACKLIGHT_PIN, 255);
ledcAttachChannel(): Binds GPIO 21 to LEDC channel 0, configured with 2000 Hz frequency and 8-bit resolutionledcWrite(BACKLIGHT_PIN, 255): Sets the initial backlight brightness to 255 (maximum)The loop() function is the main loop of the Arduino program, running repeatedly after setup() completes. It continuously detects touch input and dynamically adjusts the backlight brightness based on the touch position.
void loop()
{
uint16_t t_x = 0, t_y = 0; // Store touch coordinates
if(my_touch.Pressed())
{
t_x = my_touch.X();
t_y = my_touch.Y();
if((t_x>=my_lcd.width()/2-128)&&(t_x<my_lcd.width()/2+128)&&t_y > 129&&t_y < 149)
{
my_lcd.setTextColor(TFT_RED, TFT_WHITE);
my_lcd.fillRoundRect(t_x-10, 130, my_lcd.width()/2+138-t_x+10, 21,10,0x8430);
my_lcd.fillRoundRect(my_lcd.width()/2-138, 130, t_x-my_lcd.width()/2+148, 21,10,0x07FF);
my_lcd.fillCircle(t_x, 140,10,TFT_MAGENTA);
my_lcd.setTextPadding(50);
my_lcd.drawNumber((long)t_x-(my_lcd.width()/2-128), my_lcd.width()/2-15, 65,2);
ledcWrite(BACKLIGHT_PIN, t_x-(my_lcd.width()/2-128));
}
}
}
uint16_t t_x = 0, t_y = 0;
if(my_touch.Pressed())
{
t_x = my_touch.X();
t_y = my_touch.Y();
t_x and t_y variables to store touch coordinatesmy_touch.Pressed(): Detects whether the screen is being touchedmy_touch.X() and my_touch.Y(): Gets the X and Y coordinates of the touch pointif((t_x>=my_lcd.width()/2-128)&&(t_x<my_lcd.width()/2+128)&&t_y > 129&&t_y < 149)
Determines whether the touch point is within the slider range:
Only when the touch point is within the slider area will the brightness adjustment operation be executed.
my_lcd.setTextColor(TFT_RED, TFT_WHITE);
my_lcd.fillRoundRect(t_x-10, 130, my_lcd.width()/2+138-t_x+10, 21,10,0x8430);
my_lcd.fillRoundRect(my_lcd.width()/2-138, 130, t_x-my_lcd.width()/2+148, 21,10,0x07FF);
my_lcd.fillCircle(t_x, 140,10,TFT_MAGENTA);
my_lcd.setTextPadding(50);
my_lcd.drawNumber((long)t_x-(my_lcd.width()/2-128), my_lcd.width()/2-15, 65,2);
Updates the interface display based on the touch position:
ledcWrite(BACKLIGHT_PIN, t_x-(my_lcd.width()/2-128));
Maps the touch position to a PWM duty cycle:
t_x - (my_lcd.width()/2 - 128) converts the touch X coordinate to a 0-255 brightness valueledcWrite() to set the PWM output of the backlight pin, achieving real-time brightness adjustmentThis sample program implements touch-screen controlled backlight brightness through the following steps:
The key functions used in the program include:
my_lcd.begin(): Initializes the TFT_eSPI library and LCD screenmy_lcd.setRotation(): Sets the screen rotation directionmy_touch.setCal(): Configures touch screen calibration parametersmy_lcd.fillScreen(): Fills the entire screen with a specified colormy_lcd.fillRoundRect(): Draws a filled rounded rectanglemy_lcd.fillCircle(): Draws a filled circlemy_lcd.drawNumber(): Draws a number at a specified positionmy_touch.Pressed(): Detects whether there is touch inputmy_touch.X() / my_touch.Y(): Gets touch coordinatesledcAttachChannel(): Binds a LEDC channel to a specified pinledcWrite(): Sets the PWM output value (0-255)If you need to modify the code display effect, you can refer to the following aspects for adjustment:
Modify Backlight Pin:
#define BACKLIGHT_PIN 21 to another GPIO pinModify PWM Parameters:
freq (frequency), channel (channel), and resolution (resolution) variablesModify Initial Brightness:
ledcWrite(BACKLIGHT_PIN, 255)Modify UI Colors:
Modify Slider Position and Dimensions:
fillRoundRect() and fillCircle()Modify Touch Calibration Parameters:
setCal() according to the actual touch screen characteristicsModify Screen Rotation Direction:
setRotation() (0-3) to alter the screen display direction