Hello guys, currently i have a problem with developing my project.
I'm creating a Temperature and Humidity Sensor that send the reading of the sensor to OLED display and Modbus.
I'm using HDC1080, OLED I2C and Max485 Rs-485 TTL to Serial converter.
I created some code to create the project
The first one is code for reading temperature and display it on the OLED
#include <Wire.h>
#include "ClosedCube_HDC1080.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
ClosedCube_HDC1080 hdc1080;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup()
{
Serial.begin(9600);
//================================================================SETUP SENSOR================================================================//
hdc1080.begin(0x40);
//=================================================================SETUP OLED=================================================================//
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
oled.clearDisplay(); // clear display
oled.setTextSize(1); // text size
oled.setTextColor(WHITE); // text color
// Tambahkan teks "Hello" pada layar OLED
oled.setCursor(50, 30);
oled.println("Hello");
oled.display(); // tampilkan teks "Hello" di layar
delay(1500); // tahan teks "Hello" selama 1.5 detik
oled.clearDisplay(); // hapus teks "Hello" dari layar
delay(500);
}
void loop()
{
//==============================================================LOOP SENSOR SERIAL===========================================================//
Serial.print("T=");
Serial.print(hdc1080.readTemperature());
Serial.print("C, RH=");
Serial.print(hdc1080.readHumidity());
Serial.println("%");
delay(500);
//================================================================DISPLAY OLED==============================================================//
oled.clearDisplay();
oled.setCursor(15, 20);
oled.println("TEMP : ");
oled.setCursor(65, 20); // position to display
oled.println(hdc1080.readTemperature());
oled.setCursor(105, 20);
oled.println("C");
oled.setCursor(15, 40);
oled.println("HUM : ");
oled.setCursor(65, 40);
oled.println(hdc1080.readHumidity());
oled.setCursor(105, 40);
oled.println("%");
oled.display();
delay(500);
}
The code above is working
The second code is reading sensor data and send it through Modbus
#include <Wire.h>
#include "ClosedCube_HDC1080.h"
#include "ModbusRTUMaster.h"
#include "SoftwareSerial.h"
const uint8_t RXPin = 8;
const uint8_t TXPin = 9;
const uint8_t MODBUS_SLAVE_ADDRESS = 1;
SoftwareSerial MySerial(RXPin, TXPin);
ModbusRTUMaster ModMaster(MySerial);
ClosedCube_HDC1080 hdc1080;
void setup() {
Serial.begin(9600);
MySerial.begin(9600);
hdc1080.begin(0x40);
ModMaster.begin(9600);
}
void loop() {
float temperature = hdc1080.readTemperature();
float humidity = hdc1080.readHumidity();
if (!isnan(temperature) && !isnan(humidity)) {
uint16_t temperatureValue = static_cast<uint16_t>(temperature * 10);
if (ModMaster.writeSingleHoldingRegister(MODBUS_SLAVE_ADDRESS, 0, temperatureValue)) {
Serial.println("Temperature data sent successfully");
} else {
Serial.println("Error sending temperature data");
}
uint16_t humidityValue = static_cast<uint16_t>(humidity * 10);
if (ModMaster.writeSingleHoldingRegister(MODBUS_SLAVE_ADDRESS, 1, humidityValue)) {
Serial.println("Humidity data sent successfully");
} else {
Serial.println("Error sending humidity data");
}
Serial.print("T=");
Serial.print(temperature);
Serial.print("C, RH=");
Serial.print(humidity);
Serial.println("%");
} else {
Serial.println("Error reading sensor data");
}
delay(3000);
}
The code above is working too.
Then i have a problem when i want to combine the code that included reading sensor data, display it on the oled and send it through the modbus, The OLED didn't show any.
Is anyone can take a look at my problem? i really appreciate with the help
Thank you.
Here is the combined code
#include <Wire.h>
#include "ClosedCube_HDC1080.h"
#include "ModbusRTUMaster.h"
#include "SoftwareSerial.h"
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // I2C address for the 128x64 screen
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const uint8_t RXPin = 5;
const uint8_t TXPin = 6;
const uint8_t MODBUS_SLAVE_ADDRESS = 1;
SoftwareSerial MySerial(RXPin, TXPin);
ModbusRTUMaster ModMaster(MySerial);
ClosedCube_HDC1080 hdc1080;
void setup() {
Serial.begin(9600);
MySerial.begin(9600);
hdc1080.begin(0x40);
ModMaster.begin(9600);
// Initialize the OLED display
if(!display.begin(SCREEN_ADDRESS, OLED_RESET, OLED_RESET)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
// Clear the display
display.clearDisplay();
display.display();
}
void loop() {
float temperature = hdc1080.readTemperature();
float humidity = hdc1080.readHumidity();
if (!isnan(temperature) && !isnan(humidity)) {
// Display temperature and humidity on OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print(F("Temp: "));
display.print(temperature, 1);
display.println(F("C"));
display.setCursor(0, 10);
display.print(F("Humidity: "));
display.print(humidity, 1);
display.println(F("%"));
display.display();
// Send data over Modbus
uint16_t temperatureValue = static_cast<uint16_t>(temperature * 10);
if (ModMaster.writeSingleHoldingRegister(MODBUS_SLAVE_ADDRESS, 0, temperatureValue)) {
Serial.println("Temperature data sent successfully");
} else {
Serial.println("Error sending temperature data");
}
uint16_t humidityValue = static_cast<uint16_t>(humidity * 10);
if (ModMaster.writeSingleHoldingRegister(MODBUS_SLAVE_ADDRESS, 1, humidityValue)) {
Serial.println("Humidity data sent successfully");
} else {
Serial.println("Error sending humidity data");
}
Serial.print("T=");
Serial.print(temperature);
Serial.print("C, RH=");
Serial.print(humidity);
Serial.println("%");
} else {
Serial.println("Error reading sensor data");
}
delay(3000);
}
The problem is at the serial monitor it says that "SSD1306 allocation failed" and the display didn't show anything.