Optimization Help for Ohmmeter with Modbus and OLED Display on 16 MHz Arduino

Title: Optimization Help for Ohmmeter with Modbus and OLED Display on 16 MHz Arduino

Hello Arduino Community,

I’m working on an Arduino project for a precision auto-ranging ohmmeter using a 16 MHz Arduino Pro Mini. The project involves measuring resistances from 0 Ohms to 1 GOhm, displaying the results on an OLED display, and handling Modbus master functionality.

The current code for the ohmmeter is functioning well, but I encounter performance issues when adding the Modbus library. The system becomes less responsive and struggles to keep up with both Modbus communication and OLED updates.

Project Requirements:

  • Modbus master functionality
  • OLED display updates (SSD1306)

Current Code Overview: The code uses FreeRTOS to manage tasks, including resistance measurement and display updates. However, the performance suffers due to the 16 MHz clock speed.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Arduino_FreeRTOS.h>
#include <ModbusMaster.h>

// Constants
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Reset pin #

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
ModbusMaster node;

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

// Initialize OLED
if(!display.begin(SSD1306_I2C_ADDRESS, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}

display.display();
delay(2000);
display.clearDisplay();

// Initialize Modbus
node.begin(1, Serial); // 1 is the Modbus slave ID
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);

// Create tasks
xTaskCreate(MeasureTask, "MeasureTask", 128, NULL, 1, NULL);
xTaskCreate(DisplayTask, "DisplayTask", 128, NULL, 2, NULL);
}

void loop() {
// FreeRTOS manages tasks; no code here
}

// Measure task function
void MeasureTask(void *pvParameters) {
for (;:wink: {
// Measure resistance (pseudo-code)
// int resistance = readResistance();
// Send Modbus command (pseudo-code)
// node.writeSingleRegister(0x01, resistance);
vTaskDelay(pdMS_TO_TICKS(1000)); // Delay between measurements
}
}

// Display task function
void DisplayTask(void *pvParameters) {
for (;:wink: {
// Update OLED display (pseudo-code)
// display.clearDisplay();
// display.setCursor(0, 0);
// display.print("Resistance: ");
// display.print(resistance);
// display.display();
vTaskDelay(pdMS_TO_TICKS(500)); // Delay between display updates
}
}

void preTransmission() {
// Prepare for Modbus transmission (if needed)
}

void postTransmission() {
// Clean up after Modbus transmission (if needed)
}
Issues and Questions:

  • The code becomes unresponsive when the Modbus library is added.
  • Are there any optimizations or improvements I can make to handle both Modbus communication and OLED updates more efficiently on a 16 MHz Arduino?

I appreciate any guidance or suggestions you can provide to help optimize the code and improve performance.

Hi, @vishwjeetkulkarni
Welcome to the forum.

You need to put your code in code tags, to put the code in its own scrolling window.

Are you using Kevin (4 Wire) connections to obtain your precision?
How often do you need to sample and update your measurements.

What is the Modbus for?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

I moved your topic to an appropriate forum category @vishwjeetkulkarni.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.