SD Card, disable my Touch screen - Help me

Could anyone shed some light on why when I enable the SD Card on the board I’m using, the touchscreen is disabled? (It just doesn’t work anymore.) I don’t know what it is, it doesn’t give an error, it doesn’t crash and the other things keep working.

I’m using the LVGL code generated by Squareline Studio and everything works fine, only when I enable SDCard it stops.


If any of the information below helps:

and code:

void setupDisplay(){
        //background light pin
        pinMode(27, OUTPUT);
        digitalWrite(27, HIGH);

        lv_disp_draw_buf_init( &draw_buf, buf1, NULL, screenWidth * screenHeight / 13 );

        static lv_disp_drv_t disp_drv;
        lv_disp_drv_init( &disp_drv );
        //Display driver port of LVGL
        disp_drv.hor_res = screenWidth;
        disp_drv.ver_res = screenHeight;
        disp_drv.flush_cb = my_disp_flush;
        disp_drv.draw_buf = &draw_buf;
        lv_disp_drv_register( &disp_drv );

        static lv_indev_drv_t indev_drv;
        lv_indev_drv_init( &indev_drv );
        indev_drv.type = LV_INDEV_TYPE_POINTER;
        indev_drv.read_cb = my_touchpad_read;
        lv_indev_drv_register( &indev_drv );

        //f_progressoDisplay("Iniciando Sistema...", 500);

and code the enable SD Card:

void v_Datalogger(void *parameters){
      Serial.println("Ponto A");
      TickType_t now;now = xTaskGetTickCount();
              Serial.println("Ponto B");
              //DataLogger_.NomeArq = f_CriaArq();
              DataLogger_.NomeArq = "/mapa.csv";
              Serial.println("Ponto C");
              //time_t tt = time(NULL);//Obtem o tempo atual em segundos. Utilize isso sempre que precisar obter o tempo atual
              //data = *gmtime(&tt);//Converte o tempo atual e atribui na estrutura
              //strftime(data_formatadaX, 64, "%d/%m/%Y %H:%M:%S", &data);//Cria uma String formatada da estrutura "data"
              if (xSemaphoreTake(xMutexArq, 200)) {
                  Serial.println("Ponto D");
                  File oFile =, FILE_APPEND);if (!oFile){Serial.println("Failed to open config file for writing");}
                  char buffer[128];
                  snprintf(buffer, sizeof(buffer), "%s;%.2f", datahora_formatadaX, tempC);
char *f_CriaArq() {
    Serial.println("Ponto B.2");
    char *dataFormatada = data_formatadaX;
    static char NomeArq[64];
    snprintf(NomeArq, sizeof(NomeArq), "/%s.csv", dataFormatada);
    char *nomeArquivo = (char *)malloc(strlen(NomeArq) + 1);
    strcpy(nomeArquivo, NomeArq);
    Serial.println("Ponto B.3");
    return nomeArquivo;

Any suggestion ? Where should I start looking for this problem?

Does it share the same SDI bus? of the Display?

I just saw this post.

what display are you using? That schematic looks really familiar.

I am guessing that you are using a resistive touch panel and not I2C. The resistive touch panel shares the same bus as the display where as the capacitive touch panel uses I2C.

I know what that it… It is a Sunton display with the 2.4" or 2.8" screen.

Sunton 2.4" Resistive

OK so now I know whet we are dealing with as far as hardware goes.

This post is the same question that you are helping me with in another. I’m happy to find a solution, and I’ll document everything that happens because it helps others who may have the same question in the future.

Yeah I got that this post is tied to the other post. This one you have the schematics for the board.

I need to to post those schematics again without them being cut off. I cannot make out the section about the SDCard reader. It looks like the SDCard reader shares the same CS line as the touch panel. That would be very odd if they did that.

I’ll order the Chinese one on Aliexpress. But the scheme already came like this. I also found it strange.

I’m sure the SDCard is using CS as pin 5 because I tested the command SD.Begin(5); and the SD Card worked.

However, the problem of the Resistive Touch Screen not working after initializing SD.Begin() continues.

OK I think I have your solution…

Remove the

#define USE_HSPI_PORT 1

The you need to bring this code into your code. I located the pin numbers for the SD card reader.

#include "FS.h"
#include "SD.h"
#include "SPI.h"

#define SD_CS = 5
#define SD_MOSI = 23
#define SD_CLK = 18
#define SD_MISO = 19

SPIClass * sd_spi = NULL;
sd_spi = new SPIClass(HSPI);

void setup() {
    Serial.println("Serial communication started.");
    sd_spi->begin(SD_CLK, SD_MISO, SD_MOSI, SD_CS);
    pinMode(sd_spi->pinSS(), OUTPUT);
    if (!SD.begin(SD_CS, sd_spi, 80000000)) {
        Serial.println("SD Initialization failed!");

1 Like

I apologize for the delay, I’m trying to implement these suggestions.


Thank you very, very, very, very, very, very, very, very, very, very much, I had to make some adjustments to the code to fit the Arduino.

But the fact is that it worked!

I need to complete more detailed tests, such as recording on the card, etc.

But the resistive touch works again and the SD Card begins normally.

Below is the implemented code in case anyone goes through a similar situation in Arduino IDE:

      #define SD_CS  5
      #define SD_MOSI 23
      #define SD_CLK 18
      #define SD_MISO 19
      SPIClass* sd_spi; // Declare sd_spi aqui fora da função setup


void setupSDCard(){

        sd_spi = new SPIClass(HSPI);

        sd_spi->begin(SD_CLK, SD_MISO, SD_MOSI, SD_CS);
        pinMode(sd_spi->pinSS(), OUTPUT);
        if (!SD.begin(SD_CS, *sd_spi, 80000000)) { // Desreferencie sd_spi aqui
            // Lidar com falha na inicialização do cartão SD

        uint8_t cardType = SD.cardType();

        if(cardType == CARD_NONE){
            Serial.println("No SD card attached");

        Serial.print("SD Card Type: ");
        if(cardType == CARD_MMC){
            strcpy(SD_.Tipo, "MMC");
        } else if(cardType == CARD_SD){
            strcpy(SD_.Tipo, "SDSC");
        } else if(cardType == CARD_SDHC){
            strcpy(SD_.Tipo, "SDHC");
        } else {
            strcpy(SD_.Tipo, "NOT");

        uint64_t cardSize = SD.cardSize() / (1024 * 1024);
        SD_.Tam = cardSize;
        SD_.TotalSpace = SD.totalBytes() / (1024 * 1024);
        SD_.Uso = SD.usedBytes() / (1024 * 1024);
        Serial.printf("SD Card Size: %lluMB\n", cardSize);
        Serial.printf("Total space: %lluMB\n", SD_.TotalSpace);
        Serial.printf("Used space: %lluMB\n", SD_.Uso);


I knew you would have to adapt it for your use case. I got ya steered in the right direction so you could get it added in the way it needed to be for your setup.

Not really an LVGL problem but it was something that I knew I could help ya with. Glad it’s working now That’s awesome!!

Don’t forget tippin’ is not a town in China :wink:
If my help saved you from going bald (from all the hair pulling), and most importantly saved you time. if you think that time saved is also saved money… sharing the wealth helps us greatly…

If you do donate mention that I helped ya out in the donation message.

All donations are fully transparent so you can see we don’t make any money for doing this. It’s just enough to pay for web hosting and to also put up money to pay volunteers to get some of the coding things done that are specific to different kinds of hardware. If you can swing a buck that’s great just make sure to fat finger a couple extra zeros onto the end :crazy_face:

I didn’t even know there was crowdfunding for the LVGL project.

I’m just starting to learn LVGL and use Squareline Studio.

The LVGL Content in my language is very small. In the future, I intend to contribute to the expansion of the use and knowledge of LVGL here in Brazil

I made a small contribution to crowdfunding. The value is not significant. Because there are currently no resources available. But I hope to soon make greater contributions and even be a major financier.

The day this happened is because my projects worked out.

NOTE: I forgot to mention your help. I got lost when it came to making the contribution screen.

Oh your fine. Thank you for donating. it all helps to keep the gears moving.