I was able to make my I2C SSD1306 OLED work with an ESP32 but after a few minutes with an USB battery plugged into the ESP32, the display stopped working.
I then plugged back the ESP32 into my PC and re-uploaded my code, but the display still refuses to work.
The display stills responds on I2C address 0x3C, as shown by this I2C scanner code :
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(115200);
Serial.println("\nI2C Scanner");
}
void loop() {
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ ) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
nDevices++;
}
else if (error==4) {
Serial.print("Unknow error at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found\n");
}
else {
Serial.println("done\n");
}
delay(5000);
}
Scanning...
I2C device found at address 0x3C
done
Then, whatever I do, the display stays black. For instance, this code does not display anything, while it was working before.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(115200);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
// Display static text
display.println("Hello, world!");
display.display();
Serial.println("Done writing to screen !");
}
void loop() {
}
I also checked that I had 3.3v between the Vcc and Gnd pins of the screen, and that is the case. So there does not seem to be any wiring issues. The SCL pin is connected to GPIO22 and the SDA pin to GPIO21.
It seems that the display is broken after a few minutes of working properly.
Is it common for this kind of displays to be so fragile ? Is it likely to be a software issue ?