Issue with dispalying the real-time data on Heltec LoRa esp32 WiFi BLE module.

I need to detect the temperature using MLX90614 sensor and LoRa esp32 (V3) and I can be able to see the temperatures on serial monitor but can’t be able to see on OLED. can someone help to resolve the issue?
#include <Wire.h>
#include “HT_SSD1306Wire.h”
#include <Adafruit_MLX90614.h> // MLX90614 library

// Initialize display and MLX90614 sensor
SSD1306Wire oledDisplay(0x3C, 400000, 21, 19, GEOMETRY_128_64, -1); // I2C address, frequency, SDA, SCL, geometry, reset pin
Adafruit_MLX90614 mlx = Adafruit_MLX90614(); // MLX90614 sensor

void setup() {
Serial.begin(115200);
Wire.begin(21, 19); // Initialize I2C with SDA, SCL pins

// I2C Scanner to detect devices on the I2C bus
Serial.println(“Scanning for I2C devices…”);
byte error, address;
int 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++;
}
}
if (nDevices == 0)
Serial.println(“No I2C devices found.\n”);
else
Serial.println(“I2C devices found.\n”);

// Initialize MLX90614 sensor and check if it started correctly
if (!mlx.begin()) {
Serial.println(“Error initializing MLX90614 sensor!”);
while (1); // Stop if sensor init fails
} else {
Serial.println(“MLX90614 sensor initialized successfully.”);
}

// Initialize OLED display
oledDisplay.init();
oledDisplay.flipScreenVertically(); // Rotate display if necessary
oledDisplay.setContrast(255);
oledDisplay.clear();
oledDisplay.display();
}

void loop() {
// Read temperatures
double ambientTemp = mlx.readAmbientTempC(); // Read ambient temperature in Celsius
double objectTemp = mlx.readObjectTempC(); // Read object temperature in Celsius

if (isnan(ambientTemp) || isnan(objectTemp)) {
Serial.println(“Failed to read from MLX90614 sensor!”);
} else {
// Display temperature values on Serial Monitor
Serial.print(“Ambient Temp: “);
Serial.print(ambientTemp);
Serial.print(” °C\t”);
Serial.print(“Object Temp: “);
Serial.print(objectTemp);
Serial.println(” °C”);

// Display only the object temperature on the OLED display
oledDisplay.clear();
oledDisplay.setTextAlignment(TEXT_ALIGN_CENTER);
oledDisplay.setFont(ArialMT_Plain_24);
oledDisplay.drawString(64, 22, String(objectTemp, 1));  // Show only object temperature
oledDisplay.display();

}

delay(2000);
}

I think you’ll find you’re not using the correct GPIOs for the OLED I2C bus—it’s on pins 17/18/21 (just use the predefined names: OLED_SDA/OLED_SCL/OLED_RST).

And no, you can’t easily use the same I2C bus for both the OLED display and other devices. A seach of these forums will return a number of threads discussing this issue and how to define a second I2C bus. Perhaps start here:

Thanks for the information.

I have modified the code according to your comments, still the issue hasn’t resolved. Can you please check the code once?

#include <Wire.h>

#include <Adafruit_MLX90614.h>

#include “HT_SSD1306Wire.h”

// Define GPIOs for MLX90614 sensor on a second I2C bus

#define SDA_SENSOR 19

#define SCL_SENSOR 20

// Define OLED GPIOs as specified by Heltec

#define OLED_SDA 17

#define OLED_SCL 18

#define OLED_RST 21

#define I2C_ADDRESS 0x5A // I2C address for MLX90614

#define OLED_UPDATE_INTERVAL 500

Adafruit_MLX90614 mlx;

SSD1306Wire oledDisplay(0x3C, 400000, OLED_SDA, OLED_SCL, GEOMETRY_128_64, OLED_RST);

unsigned long lastUpdate = 0;

void setup() {

Serial.begin(115200);

while (!Serial);

// Initialize I2C for MLX90614 sensor

Wire.begin(SDA_SENSOR, SCL_SENSOR);

if (!mlx.begin()) {

    Serial.println("MLX90614 sensor not found!");

    while (1);

}

// Initialize OLED display

oledDisplay.init();

delay(500);

oledDisplay.clear();

oledDisplay.display();

Serial.println("Setup completed. Display initialized.");

// Initial message on OLED

oledDisplay.setFont(ArialMT_Plain_16);

oledDisplay.setTextAlignment(TEXT_ALIGN_CENTER);

oledDisplay.drawString(64, 20, "Starting...");

oledDisplay.display();

delay(2000);

}

void loop() {

float ambientTemp = mlx.readAmbientTempC();

float objectTemp = mlx.readObjectTempC();

// Display temperature details on Serial Monitor

Serial.print("Ambient Temp: ");

Serial.print(ambientTemp);

Serial.print(" °C\t");

Serial.print("Object Temp: ");

Serial.println(objectTemp);

// Update OLED display periodically

if (millis() - lastUpdate >= OLED_UPDATE_INTERVAL) {

    lastUpdate = millis();

    oledDisplay.clear();

    oledDisplay.setFont(ArialMT_Plain_16);

    oledDisplay.setTextAlignment(TEXT_ALIGN_CENTER);

    oledDisplay.drawString(64, 20, String(objectTemp) + " °C");

    oledDisplay.display();

}

delay(1000);

}

The I2C bus used for the sensor is the second I2C bus (Wire is the first, Wire1 is the second) and needs to be defined as such. Try:

Wire1.begin(SDA_SENSOR, SCL_SENSOR);

The link [here] in my previous post above provides an alternative way to define the second I2C bus.