ESP32 and ILI9341 ISP mode

After trying several options that I found on the web, I chose this one as the better to connect an ESP32 to an ILI9341 TFT display.

ESP32 ILI9341
3V3 VCC
GND GND
D15 CS
D2 RESET
D4 D/C
D23 MOSI
D18 SCK
Not connected MISO
3V3 (***or D19) LED

The code:

/**
 * ILI9341 TFT libraries and resources
 */
const int TFT_CS = 15;
const int TFT_DC = 4;
const int TFT_MOSI = 23;
const int TFT_SLK = 18;
const int TFT_RST = 2;
const int TFT_LED = 19;     

#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_SLK, TFT_RST);

void setup() {
  tft.begin();
  tft.setRotation(3); //Landscape orientation
}

The reason for the two possibilities for the LED pin is the next:

If you connect LED pin directly to 3V3 you will get maximum brightness in your screen. LED pin controls the backlight of your TFT display.

In my case, I didn’t want to have max brightness but I wanted to control the intensity based on ambient light, so I decided to connect LED pin to any of the IO pins of the ESP32 (#19 in the showed example).

The code to control backlight intensity (once you have connected LED to pin D19) is as follows:

.
const int TFT_LED = 19; 
.
/**
 * PWM Constants
 */
const int freq = 5000;
const int tftledChannel = 0;
const int resolution = 8;
.
.
.
void setup() {
.
.
.
  /**
   * TFT DISPLAY
   */
  //Background light PWM
  ledcSetup(tftledChannel, freq, resolution);
  ledcAttachPin(19, tftledChannel);
.
.
}

/**
 * Sets TFT background luminosity (0-255)
 */
void setBGLuminosity(int level){
  ledcWrite(tftledChannel, level);
}

Inserting the code above allows you to set the backlight intensity at any point just invoking setBGLuminosity() function passing a value between 0 and 255.

 

//Examples:
setBGLuminosity(0);   //Switch off screen backlight
setBGLuminosity(128); //Medium intensity (theoretically)
setBGLuminosity(255); //Full intensity