SSD1306 OLED display connected to an ESP32-S2 over hardware I2C. I noticed that the text will sometimes get scrambled or lag during a screen update. When left running long enough, the screen will get stuck and stop updating, despite serial monitor continuing to function. Does anyone know how to fix this? Here is a video of the laggy behavior: https://youtu.be/9OYW00cE24U

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
unsigned long drawState = 1;
unsigned long previousTime = 0;
unsigned long eventInterval = 4000;
#define OLED_ADDR 0x3C
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(115200);
Wire.setClock(400000);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
;
}
}
int screenSwitchLast = millis();
void loop() {
if (millis() - screenSwitchLast >= eventInterval) {
screenSwitchLast = millis();
switch (drawState) {
case 1:
drawTemp(); //draw Temperature
break;
case 2:
drawHum(); //draw Humidity
break;
}
drawState++;
if (drawState > 2) {
drawState = 1;
}
}
}
void drawTemp() {
Serial.print("Temp ");
Serial.println(millis());
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 12);
display.print("Temp: ");
display.print("10");
display.print("F");
display.display();
delay(1);
}
void drawHum() {
Serial.print("Hum ");
Serial.println(millis());
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 12);
display.print("Hum: ");
display.print("20");
display.print("%");
display.display();
delay(1);
}
I saw this post which recommends running screen update code on the second core but the ESP32-S2 only has 1 core. I'm at odds with how I'd get this to work reliably on a single core ESP32.
Board: Adafruit ESP32-S2 Feather- 4M Flash and 2M PSRAM
Display: 128x32 12C OLED - Ox3C
Arduino Settings:
- USB CDC on Boot: Enabled
- CPU Frequency: 240mHz (WiFi)
- Core Debug Level: None
- USB DFU on Boot: Disabled
- Erase All Flash Before Sketch Upload: Disabled
- Flash Frequency: 80mHz
- Flash Mode: QIO
- Flash Size: 4MB (32Mb)
- USB Firmware MSC on Boot: Disabled
- Partition Scheme: Minimal SPIFFS 1.9MB APP with OTA/190KB SPIFFS
- PSRAM: Enabled
- Upload Mode: Internal USB
- Upload Speed: 921600