ILI9341 TFT LCD to ESP32 – Full HOW TO

The ILI9341 is a great display, fast, cheap and most modules for hobbyists come with an on-board SD Card reader and built in touch screen – marvellous! But getting all these things working, especially with the ESP32 has sometimes been a little hit and miss – if you trawl the internet and all the requests for help.

So here, hopefully, is a definite guide to getting all those three functions up and running with your ESP32. Below is a full video showing all the steps from wiring to libraries to examples. This page has other supporting materials that you may need.

IMPORTANT: There is an omission in the video, for the touch screen to work you must un-comment the line below (remove the hash at the beginning) in the “User_Setup.h” file.

#define TOUCH_CS 21 // Chip select pin (T_CS) of touch screen

The JPEG Code

Here’s the code shown in the SD Card section. It displays jpeg files that are stored in the root of the SD card. No other files should be in root (folders are fine) as it will attempt to display them as an image too! After the code you will find a zip file of the images used in the demo. If you want to do your own images it’s important to note that the max length of filename you can use is 8 characters and the image must be of the resolution 320×240 and be in landscape orientation. You can look at the code if you want to change to portrait.

The Sample Images (if you want to use them)

That’s it, if you’ve some this far and wonder where all the detail is, then you’ll find it in the video!

33 Comments

  1. Brilliant video, I am struggling to get the touch to work, I have removed the comments for #define TOUCH_CS 21 but I am getting these errors. Any idea what to do.

    On_Off_Button:79:11: error: ‘class TFT_eSPI’ has no member named ‘getTouch’

    if (tft.getTouch(&x, &y))

    ^
    On_Off_Button:143:9: error: ‘class TFT_eSPI’ has no member named ‘setTouch’

    tft.setTouch(calData);

    ^

    On_Off_Button:162:9: error: ‘class TFT_eSPI’ has no member named ‘calibrateTouch’

    tft.calibrateTouch(calData, TFT_MAGENTA, TFT_BLACK, 15);

    ^

  2. above code doesn’t work with sd cards, fails to compile correct, any ideas.
    seems to be issue with min funtion(not defined) , and even then fails to connect to sd card

    • Mmm… If it’s saying min function not defined then it sounds like your missing a header file perhaps, have you definitely installed all the ones you see at the top of the code? If you have then perhaps they are different versions to what I used, check the video to make sure you are using same as I do. As you can see with my set up in the video it does compile. Just need to figure out why yours is different. Also in a later video (https://youtu.be/ZmMTr8LEa1A) I mention problems you can have with the SD card reader on these screens even when compiled. Have a look, it solved a few people’s problems.
      Hope this helps

  3. Hello
    Please, regarding your video for the SD card. I tried to upload it on the Arduino Mega but have error message that it cant be uploaded. Do I have to create the header file for FS? I am using the 4.0 inch TFT display 14 pins + 4 for the SD card.

  4. no matching function for call to ‘min(uint16_t&, uint32_t)’

    uint32_t min_h = min(mcu_h, max_y % mcu_h);

  5. hello i use an esp32 with 30 pins and got a tft ili9488 display and wanted to use the touch function. Image is displayed, but the touch function does not work and every time a calibration is done, it is done automatically without me touching the screen, as if there is continuous contact on the display. Can you help me there?

    • Sorry for late reply, real life work has been very busy and I missed this comment. Did you check the addendum? It’s written in the video description and I think the article. There was a bit missed out in the video, for the touch screen to work you must un-comment the line below (remove the hash at the beginning) in the “User_Setup.h” file.

      #define TOUCH_CS 21 // Chip select pin (T_CS) of touch screen

  6. Brilliant tutorial ! 1K Thanks !!! Everything works just fine. Displaying full size (320×240) pics works wonderful !
    BUT … 🙂
    I want to put a small jpg (e.g. 25 x 25 pix) on a certain (x,y) location. (without destroying text, lines etc) Reason : instead of “drawing” on the screen, I think it’s much more convenient to store e.g. “icons” on the card and “read” them when needed but I can’t find out how to achieve this. i.e. What is the minimum code to do this ? I guess that there’s a lot of lines in the original code that are not necessary … I tried, tried but till now : no success…
    Any help is greatly appreciated !

    • You mean you want to put an icon over a background image with destroying the back ground? I think the library can handle GIF’s with transparency. You would need your GUF to have a “Mask” for where you want the background to show through. Most if not all art packages support this. However if you want to remove the icon you will have to redraw over it first with that area of background.

  7. Good Tutorial, Thanks.. Has Anyone found a way to get rid of the artifacts, left on the touch screen, after you’ve touched the screen… needs some kind of a Re-fresh I guess.??

  8. Great Tutorial,, Please disregard my last post,, as I thought there was a problem with
    artifacts…. Finally I see the line of code that removes the spots…
    // Comment out to stop drawing black spots*
    //#define BLACK_SPOT
    I am now vary happy with the Tutorial and Library…. Thanks..

  9. I really like your straight forward approach! Perfect! I followed your instructions step by step and of course (Murphy again) ended up with something completely different. I had to dig deep in my orders from AliExpress to find out my display has a different driver chip. After changing my setup.h I was happy as can be. Thanks for your wonderful tutorial(s)

  10. These displays should run at 3.3.V for the SPI signals, looking at the specs. I see that you have hooked it up with an usb cable so the esp32 is running at 5V. can’t find the wiring diagram you are using and mine simply doesn’t work.using an ESP WROOM-32D.

    • Don’t worry, it’s not running at 5v, there are voltage regulators on the boards that bring it down to 3.3v. It would break otherwise. There isn’t a wiring diagram, you have to follow the video for this one – sorry. I should produce one really.

  11. I thought your video was excellent and I look forward to getting my 3.5″ display.
    There is a lot of confusion with the ESP32. The actual pin numbers do not correspond to the physical package pin numbers and depends on what they call pin 1.
    I got it figured out and Im using it on my solar water system controller design.
    Thank You for the effort.

  12. umm, after im following ur instruction i got a problem on testing touch there’s no calibrate after i upload the sketch on_off_button, so the button won’t change to off or on

  13. Brilliant tutorial for a new tinkerer, thanks.

    I got this all working on the ESP32 but the touch doesn’t work on a LoRa 32 v2, it is an ESP32 based board with a LoRa transceiver built-in.

    The LCD works great on the LoRa 32 v2 but the touch screen calibrates itself without being touched and then will not respond. I am using 23 and 17 for CS on the LCD and touch controllers. I have reversed then and the LCD is still fine and the touch still faulty. MOSI, MISO and CLK are common so unless it needs the IRQ I am stumped.

    What is strange is you can unplug the CS and MISO wires from the touch controller and the screen still calibrates. Heaven knows where the data is coming from that the board thinks is from the touch screen when it calibrates.

    As I say, I am new and so I may be overlooking the obvious. Any pointers will be very much appreciated. Thanks again.

    • Errr… I’m annoyed that I can’t think of anything to help you. Nothing springs to mind. I’ve not got any of those boards in either so can’t even try to replicate.I’m really sorry I’m not sure what’s going on there 🙁

      • I sorted it and for the benefit of others playing with this board I will explain.

        This board has a LoRa radio on the SPI and the CS is connected to GPIO 18. If you don’t write GPIO 18 high then the LoRa radio ‘chatters’ onto the SPI MISO bus and this interrupts the touch controller signals. Define 18 is output and write it high is the simple fix if you don’t need the radio.

        Interestingly though you can still use the LoRa radio with this display, when you include the LoRa radio in the script but never ask it to do anything then GPIO 18 is sent high by the code and you get no problem with the SPI bus.

        If you want to use the LoRa radio you have 2 choices. (1) except that there may be some glitches because the SPI cannot update the display and communicate with LoRa at the same time or (2) you can define different SPI pins in the user_setup.h file, this gives another SPI channel but that drops the SPI bus from 80MHz to 40MHz.

        I went with option 1 and it seems fine for my needs but if you have lots of data being transmitted over LoRa and lots of graphics updating on the screen it may be a problem that they share the SPI bus.

        I am still learning this new black magic but I hope this helps someone else get LoRa and this touch screen to play nice…

Comments are closed.