carduinoesp32espressif-idf

ESP WROOM32 ST7789 display stops working after connecting to WiFi


In my project I use an ESP WROOM32 with a 240x280 ST7789 display connected as following:

SCL: G18

SDA: G23

RES: G4

DC: G2

CS: G5

I initialize it like this:

#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <SPI.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <string.h>

#define TFT_CS     5
#define TFT_RST    4
#define TFT_DC     2

Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);

void setup() {
    tft.init(240, 280);
}

These are the functions I use to print text or clear it. Using tft.fillScreen(ST77XX_BLACK) takes way longer than printing the same text in black again hence I dont use it

void printText(String text, int cx, int cy, bool accent) {
    tft.setTextSize(2);
    tft.setTextWrap(true);
    tft.setTextColor((accent) ? tft.color565(0, 190, 255) : tft.color565(255, 255, 255));

    for (int i = 0; i < ceil((float)strlen(text.c_str()) / (float)maxCharsPerLine); i++) {
        tft.setCursor(cx, i * 20 + cy);
        char tmp[15];
        strncpy(tmp, text.c_str() + i * 14, 14);
        tmp[14] = '\0';
        tft.print(tmp);
    }
}


void clearText(String text, int cx, int cy) {
    tft.setCursor(cx, cy);
    tft.setTextSize(2);
    tft.setTextWrap(true);
    tft.setTextColor(tft.color565(0, 0, 0));

    for (int i = 0; i < ceil((float)strlen(text.c_str()) / (float)maxCharsPerLine); i++) {
        tft.setCursor(cx, i * 20 + cy);
        char tmp[maxCharsPerLine + 1];
        strncpy(tmp, text.c_str() + i * maxCharsPerLine, maxCharsPerLine);
        tmp[maxCharsPerLine] = '\0';
        tft.print(tmp);
    }
}

I don´t think they are causing the problem though. I started the project by doing all the graphics stuff and now I want to request data from a server, but the second I connect to the WiFi the display stops working. This is the function I use to connect to the WiFi:

void connect() {
    String ssid = preferences.getString("ssid");
    String pwd = preferences.getString("password");

    WiFi.begin(ssid, pwd);
}

After pushing a button the function gets called, the text on the display gets cleared and this code get executed by the loop() function:

printText("Connecting to", 20, 40, false);
Serial.println(WiFi.status());

The status gets printed to the serial monitor, but the display just freezes. The status changes to connected after a few seconds, but the old text doesn´t get cleared and the new text doesn´t get printed. There are no errors printed to the serial monitor. The weird thing is that commenting WiFi.begin(ssid, pwd); out removes the bug. The code seems to work, but connecting to a WiFi makes the display freeze.

What causes the error? I thought, maybe the WiFi.h library uses a pin that I use for the display?

EDIT

This is a small program which reproduces the error:

#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <SPI.h>
#include <string.h>
#include <WiFi.h>

#define TFT_CS           5
#define TFT_RST          4
#define TFT_DC           2

Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);

bool b = false;
int i = 0;

void setup() {
    Serial.begin(115200);

    tft.init(240, 280);
    tft.setRotation(2);
    tft.fillScreen(ST77XX_BLACK);
    tft.setTextSize(2);
}

void loop() {
    i++;
    if (i == 10) WiFi.begin("ssid", "password");
    Serial.println(WiFi.status());

    b = !b;
    tft.setTextColor((b) ? tft.color565(255, 255, 255) : tft.color565(255, 0, 255));
    tft.setCursor(20, 80);
    tft.print("Hello World");

    delay(500);
}

Solution

  • The WiFi seems to use the ADC2. Answers on other websites state, that that doesn´t matter, if your not using the pins that the ADC2 uses as an ADC channel. That turns out to be wrong.

    Pin 2 and 4, which in this case were used as TFT_DC and TFT_RST, are channels of ADC2. Changing the pins to 32 and 33 does the job:

    #define TFT_CS           5
    #define TFT_RST         32
    #define TFT_DC          33
    
    Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);