Waveshare oled module is not working

Hii,

I connected waveshare oled module is not working this is a module name https://www.waveshare.com/wiki/2.42inch_OLED_Module I just connected with df robot firebettle esp32 board that is connected with this give wiring data in code

``#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include

// OLED display dimensions
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

// SPI pin assignments (FireBeetle ESP32-E compatible)
#define OLED_MOSI 23 // DIN
#define OLED_CLK 18 // SCK
#define OLED_DC 16
#define OLED_CS 4
#define OLED_RST 17

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
OLED_MOSI, OLED_CLK, OLED_DC, OLED_RST, OLED_CS);

// Wi‑Fi & MQTT credentials
const char* ssid = "Kapshi1";
const char* password = "Kaps2025";
const char* mqtt_server = "192.168.0.42";

// Networking objects
WiFiClient wifiClient;
PubSubClient client(wifiClient);

// Timing
unsigned long lastScanMillis = 0;

// MQ analog pins
const int mqPins[4] = {39, 36, 34, 35};

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

// SPI OLED setup
if (!display.begin(SSD1306_SWITCHCAPVCC)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.display();
display.setRotation(0);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);

// Connect Wi‑Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(300);
Serial.print(".");
}
Serial.println("\nWiFi connected: " + WiFi.localIP().toString());

// Setup MQTT
client.setServer(mqtt_server, 1883);
}

void loop() {
unsigned long now = millis();

// Reconnect Wi‑Fi if needed
if (WiFi.status() != WL_CONNECTED) {
WiFi.reconnect();
}

// Reconnect MQTT if needed
if (!client.connected()) {
static unsigned long lastAttempt = 0;
if (now - lastAttempt > 5000) {
lastAttempt = now;

  // ✅ CHANGE 1: MQTT Client ID
  if (client.connect("esp4")) {
    Serial.println("MQTT connected");

    // ✅ CHANGE 2: MQTT Status Topic
    client.publish("esp32/esp4/status", "connected");

  }
}

} else {
client.loop();
}

// Every second, scan I²C + MQ sensors and publish
if (now - lastScanMillis >= 1000) {
lastScanMillis = now;

// Scan I2C Devices
std::vector<String> i2cDevices;
for (uint8_t addr = 1; addr < 127; addr++) {
  Wire.beginTransmission(addr);
  if (Wire.endTransmission() == 0) {
    String addrStr = "0x";
    if (addr < 16) addrStr += '0';
    addrStr += String(addr, HEX);
    i2cDevices.push_back(addrStr);
  }
}
if (i2cDevices.empty()) {
  i2cDevices.push_back("No I2C");
}

// Read MQ Sensors
String mqs = "";
for (int i = 0; i < 4; i++) {
  int val = analogRead(mqPins[i]);
  mqs += "M";
  mqs += String(i + 1);
  mqs += ":";
  mqs += String(val);
  mqs += '\n';
}

// Create JSON payload
String jsonPayload = "{";

jsonPayload += "\"i2c\":[";
for (size_t i = 0; i < i2cDevices.size(); i++) {
  jsonPayload += "\"" + i2cDevices[i] + "\"";
  if (i < i2cDevices.size() - 1) jsonPayload += ",";
}
jsonPayload += "],";

jsonPayload += "\"mq\":[";
for (int i = 0; i < 4; i++) {
  int val = analogRead(mqPins[i]);
  jsonPayload += "\"M" + String(i + 1) + ":" + String(val) + "\"";
  if (i < 3) jsonPayload += ",";
}
jsonPayload += "]}";

// ✅ CHANGE 3: Sensor topic
client.publish("esp32/esp4/sensors", jsonPayload.c_str());

Serial.println("Published JSON:");
Serial.println(jsonPayload);

// OLED Display
display.clearDisplay();
display.setCursor(0, 0);
display.println("I2C + MQ List:");

int line = 1;
for (String dev : i2cDevices) {
  display.setCursor(0, line * 8);
  display.println(dev);
  if (++line >= 8) break;
}

for (int i = 0; i < 4 && line < 8; i++) {
  int val = analogRead(mqPins[i]);
  display.setCursor(0, line * 8);
  display.print("M");
  display.print(i + 1);
  display.print(":");
  display.println(val);
  line++;
}

display.display();

}
}
that is the code to display the oled
`

You started a topic in the Uncategorised category of the forum when its description explicitly tells you not to

Your topic has been moved to a relevant category. Please be careful in future when deciding where to start new topics

I don't know what you did when you posted your code but it is unreadable as it is

Please follow the following instructions and post it again in a new reply

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Hi,
I used waveshare module with [DFRobot FireBeetle 2 ESP32-E IoT Microcontroller]
https://www.waveshare.com/wiki/2.42inch_OLED_Module
that is not working i didn't configure what issues is i will update coded
paste it below .Please give me any suggestion to reslove the issues.
``#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include

// OLED display dimensions
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

// SPI pin assignments (FireBeetle ESP32-E compatible)
#define OLED_MOSI 23 // DIN
#define OLED_CLK 18 // SCK
#define OLED_DC 16
#define OLED_CS 4
#define OLED_RST 17

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
OLED_MOSI, OLED_CLK, OLED_DC, OLED_RST, OLED_CS);

// Wi‑Fi & MQTT credentials
const char* ssid = "Kapshi1";
const char* password = "Kaps2025";
const char* mqtt_server = "192.168.0.42";

// Networking objects
WiFiClient wifiClient;
PubSubClient client(wifiClient);

// Timing
unsigned long lastScanMillis = 0;

// MQ analog pins
const int mqPins[4] = {39, 36, 34, 35};

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

// SPI OLED setup
if (!display.begin(SSD1306_SWITCHCAPVCC)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.display();
display.setRotation(0);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);

// Connect Wi‑Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(300);
Serial.print(".");
}
Serial.println("\nWiFi connected: " + WiFi.localIP().toString());

// Setup MQTT
client.setServer(mqtt_server, 1883);
}

void loop() {
unsigned long now = millis();

// Reconnect Wi‑Fi if needed
if (WiFi.status() != WL_CONNECTED) {
WiFi.reconnect();
}

// Reconnect MQTT if needed
if (!client.connected()) {
static unsigned long lastAttempt = 0;
if (now - lastAttempt > 5000) {
lastAttempt = now;

  // ✅ CHANGE 1: MQTT Client ID
  if (client.connect("esp4")) {
    Serial.println("MQTT connected");

    // ✅ CHANGE 2: MQTT Status Topic
    client.publish("esp32/esp4/status", "connected");

  }
}

} else {
client.loop();
}

// Every second, scan I²C + MQ sensors and publish
if (now - lastScanMillis >= 1000) {
lastScanMillis = now;

// Scan I2C Devices
std::vector<String> i2cDevices;
for (uint8_t addr = 1; addr < 127; addr++) {
  Wire.beginTransmission(addr);
  if (Wire.endTransmission() == 0) {
    String addrStr = "0x";
    if (addr < 16) addrStr += '0';
    addrStr += String(addr, HEX);
    i2cDevices.push_back(addrStr);
  }
}
if (i2cDevices.empty()) {
  i2cDevices.push_back("No I2C");
}

// Read MQ Sensors
String mqs = "";
for (int i = 0; i < 4; i++) {
  int val = analogRead(mqPins[i]);
  mqs += "M";
  mqs += String(i + 1);
  mqs += ":";
  mqs += String(val);
  mqs += '\n';
}

// Create JSON payload
String jsonPayload = "{";

jsonPayload += "\"i2c\":[";
for (size_t i = 0; i < i2cDevices.size(); i++) {
  jsonPayload += "\"" + i2cDevices[i] + "\"";
  if (i < i2cDevices.size() - 1) jsonPayload += ",";
}
jsonPayload += "],";

jsonPayload += "\"mq\":[";
for (int i = 0; i < 4; i++) {
  int val = analogRead(mqPins[i]);
  jsonPayload += "\"M" + String(i + 1) + ":" + String(val) + "\"";
  if (i < 3) jsonPayload += ",";
}
jsonPayload += "]}";

// ✅ CHANGE 3: Sensor topic
client.publish("esp32/esp4/sensors", jsonPayload.c_str());

Serial.println("Published JSON:");
Serial.println(jsonPayload);

// OLED Display
display.clearDisplay();
display.setCursor(0, 0);
display.println("I2C + MQ List:");

int line = 1;
for (String dev : i2cDevices) {
  display.setCursor(0, line * 8);
  display.println(dev);
  if (++line >= 8) break;
}

for (int i = 0; i < 4 && line < 8; i++) {
  int val = analogRead(mqPins[i]);
  display.setCursor(0, line * 8);
  display.print("M");
  display.print(i + 1);
  display.print(":");
  display.println(val);
  line++;
}

display.display();

}
}
``

Code problems

Please supply your complete code in code tags <CODE/>
Before posting code, please use Tools / Auto Format at the top of the IDE. It makes the code much easier to read and you will probably find it very helpful yourself. Once you've done that, use Edit / Copy for Forum.

For more information please read THIS then edit your first post and keep all and only your code inside a single "CODE" tag pair.
Thanks.

More about posting code and using code tags.

Why did you start a new post instead of update your original post?

Please stop, don't add new posts before cleaning up the first one (and remove the duplicate). You must show us a clean and readable code we can check, not the mess you can still see up here.
Do it, please, if you want any help.

@abhay_viv

You were asked to post a new reply to your original topic containing your sketch and to use code tags when you did it. I explaned the simplest method of doing that. Instead you created a new topic and although the code is more readable this time you still failed to use code tags

Please edit your post and add the code tags by selecting all of the code then clicking on the < CODE > icon above the reply editor then saving the reply

I have merged your two topics

Start with Hello World

Thanks for reply ,I am a new bie so I also check Hello world but it could not start
I applied with different microcontrollers like esp32 ,Uno ,so i didn't recognised with what acutally issues The wiring is correct or display is not damage

@abhay_viv
Your sentences are difficult to understand for me. Please use a Google translate if English is not native to you.

Show link to your esp32 module and LCD module, run this code, do you see on serial monitor - hello world ?

   void setup() {
      // Initialize serial communication at a baud rate of 9600
      Serial.begin(9600); 
    }

    void loop() {
      // Print "Hello World!" to the serial monitor
      Serial.println("Hello World!"); 
      // Wait for 1 second before printing again
      delay(1000); 
    }

Ok but your code is still almost unreadable due to the forum filters and a bad format, and this prevents anyone from being able to help you.

As previously asked, you need to edit your post(s) (using the small pencil below each post), delete the lines with code (you made a mess on your first one, lines are all joined!) together with any code special characters (three small single quotes, the forum tags for code), then select the "<CODE/>" button and paste a fresh new and good formatted code inside the tags, then save. Do the same on both posts.
Thanks.

Thanks for reply ,
I fixed the code that is readable .Please let me know for oled is working

Thanks for reply tom , all i connected with esp32 that is pin configuration that interface with esp32 ,Actually the i didn't know what is issues in that configuration

#define OLED_MOSI  23  // DIN
#define OLED_CLK   18  // SCK
#define OLED_DC    16
#define OLED_CS    4
#define OLED_RST   17

Thanks for reply ,I edit the post that it readable

No it isn't.
A couple of examples:


...

The code isn't clearly included inside a CODE tag represented by a triplet of back quotes, showing mixed text and a few code blocks inside the sketch. In this case because before the first line ("#include <WiFi.h>") you have two back quotes, one is missing.
And that happens in BOTH posts #1 and #4.

Please carefully read this (if needed because you don't understand english, use Google Translator to convert the text to your native language) and do what is required to make correctly formetted code in your posts:

Waveshare product demos come with proprietary drivers and frozen libraries, and in my experience, it can be difficult to find compatible drivers and libraries.

To isolate the problem, I recommend first trying the Arduino or ESP32 demo available from the link in post #1 to verify that there's no problem with the OLED.

Alternatively, you can refer to the following threads, which have been marked as solved by the thread owner.

I then recommend getting advice on whether the code posted in post #1 or #4 has any issues. To do so, follow the instructions from @docdoc.

Thanks for reply I attached with Arduino uno that is not working .

Well, you have:

And u8g2 supports ESP32.