Arduino Modbus with OLED (SimpleModbus.h)

Hello, I have a project to make a display to monitor data from a PLC. I'm using Arduino Nano, 128x64 OLED, and TTL to RS485 (MAX485). The libraries I use are Adafruit_SSD1306.h and SimpleModbusMaster.h. Communication is smooth when I use the SimpleModbusMaster.h Library. But when I combine programs using OLED. Communication does not occur in OLED or PLC. I'm using I2C on OLED, and SoftwareSerial in pin (2,3) in library SimpleModbusMaster.h. Here's the code I use and the wiring. Is there any suggestion so that OLED and Modbus communication run smoothly?

#include "SimpleModbusMaster.h"
#include "ConvIntToFloat.h"
#include <Wire.h>
#include <Adafruit_SSD1306.h> //Legacy include:include "SSD1306.h"
#include <Adafruit_GFX.h>
#include <Fonts/Roboto_Light_14.h>

Adafruit_SSD1306 lcd(128, 64, &Wire, -1);

#define connection_error_led 13
#define baud 38400
#define timeout 2000
#define polling 200
#define retry_count 10
#define TxEnablePin 8

float rh = 0.0, temp = 0.0;
int Flag = 1;
String Flag1;

//This is the easiest way to create new packets, Add as many as you want. TOTAL_NO_OF_PACKETS, is automatically updated.
enum {
  PACKET1,
  PACKET2,
  // leave this last entry
  TOTAL_NO_OF_PACKETS
};

//Create an array of Packets for modbus_update()
Packet packets[TOTAL_NO_OF_PACKETS];
packetPointer packet1 = &packets[PACKET1];
packetPointer packet2 = &packets[PACKET2];

unsigned int data485[4];
unsigned int data485write[1];
unsigned long last_toggle = 0;
unsigned long previousMillis = 0;

void setup() {

  Serial.begin(38400);
  delay(100);

  lcd.begin(SSD1306_SWITCHCAPVCC, 0x3c);

  //===Read 4 registers starting at address 8983===//
  packet1->id = 1;
  packet1->function = READ_HOLDING_REGISTERS;
  packet1->address = 8983;
  packet1->no_of_registers = 2;
  packet1->register_array = data485;

  //===Write the 1 registers to the PLC starting at address 8985===//
  packet2->id = 1;
  packet2->function = PRESET_MULTIPLE_REGISTERS;
  packet2->address = 8985;
  packet2->no_of_registers = 1;
  packet2->register_array = data485write;

  //Initialize communication settings etc...
  modbus_configure(baud, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS);

}


void loop() {

  unsigned long currentMillis = millis();

//USE For Read Data Register
  if (currentMillis - previousMillis >= 200)
  {
    previousMillis = currentMillis;
    Serial.println("Masuk Case 1");
    unsigned int connection_status = modbus_update(packets);
    rh = InttoFloat(data485[1], data485[0]);
    temp = InttoFloat(data485[3], data485[2]);
    Serial.println(rh);
    Serial.println(temp);
    Serial.print("Nilai Modbus Update = ");
    Serial.println(connection_status);

  }

//Use for show data to OLED
  previousMillis = currentMillis;
  Serial.println("Masuk Case 2");
  lcd.clearDisplay();
  lcd.setFont(&Roboto_Light_14);
  lcd.setTextColor(WHITE);
  lcd.setCursor(10, 11);
  lcd.print("AHU Condition");
  lcd.setCursor(0, 35);
  lcd.print("Temp");
  lcd.setCursor(40, 35);
  lcd.print(":");
  lcd.setCursor(47, 35);
  lcd.print(String(temp));
  lcd.setCursor(0, 50);
  lcd.print("RH");
  lcd.setCursor(40, 50);
  lcd.print(":");
  lcd.setCursor(47, 50);
  lcd.print(String(rh));
  lcd.display();
}

Is your sketch low on memory? The graphical SSD1306 library can use a fair amount of RAM.

What do the compilation results say about the amount of memory used.

1 Like

Thanks Mark. I don't notice if you say RAM memory. I found the problem. Arduino Nano have 2 Kb SRAM/RAM memory. I check the free RAM when I execute OLED program is 235 bytes, and when I execute modbus program is 1093 bytes. That's free memory. If I calculate Total used RAM in arduino when OLED and Modbus program execute, is greater than 2 Kb, It's make arduino stop running.

If you are only displaying text on your display, then have a look at:

1 Like

Thanks. You're very helpful :smiley:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.