I have this ESP8266 Heltec NodeMCU board with OLED. The pins are described here.
I have the following problem. The both OLED (with u8g2) and Wifi work fine individually. There is a problem getting them working together.
In particular, the following code works (and shows Hello World, blanks the screen and then repeats):
#include <U8g2lib.h>
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
void setup() {
u8g2.begin();
}
void loop() {
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.firstPage();
do {
u8g2.setCursor(0, 20);
u8g2.print(F("Hello World!"));
} while ( u8g2.nextPage() );
delay(2000);
u8g2.clear();
delay(2000);
}
Adding the Wifi to the code (the only difference to the code above are the 2 lines in the setup).
#include <ESP8266WiFi.h>
#include <U8g2lib.h>
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
void setup() {
u8g2.begin();
WiFi.begin ( "xxx", "yyy" );
while ( WiFi.status() != WL_CONNECTED ) { delay(500); }
}
void loop() {
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.firstPage();
do {
u8g2.setCursor(0, 20);
u8g2.print(F("Hello World!"));
} while ( u8g2.nextPage() );
delay(2000);
u8g2.clear();
delay(2000);
}
In this case, "Hello World" appears 3 times and then the screen remains blank.
Edit:
I noticed something which, I believe, is an important piece of information when I turned the debugging of the WiFi on.
The display stops working after the output pm open,type:2 0
of the debugger. I believe that pm refers to powermanagement. Could it be that the system goes sleeping? How to prevent this from happening?
1 Answer 1
I managed to solve the issue and figure out what the problem is. I put here an answer for reference, if this question appears again. It turns out that the reset of the OLED is important. It is easy to see, if one uses the Adafruit_SSD1306
library. The code
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET 0
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
produces exactly the issues described in the text. Changing it into
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET 16
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
solves the issue. Similarly, replacing U8X8_PIN_NONE
with 16
in the code above, runs as expected. It looks like the example provided with the board is incorrect.
-
For me, this solved a critical problem, of a sketch using
U8g2lib.h
where the OLED always switched off after about 30s. Using 16 instead ofU8X8_PIN_NONE
in the constructor:U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ 16);
solved this issue.tpetzoldt– tpetzoldt12/29/2024 17:41:02Commented Dec 29, 2024 at 17:41 -
Thanks a lot! /*reset*/ 16 helped me as well with a Heltec Wifi Kit 8Jens Bongartz– Jens Bongartz03/05/2025 11:26:23Commented Mar 5 at 11:26
while
loop insetup()
?