ProblemmodbusRTU485

hello everyone
here I have a project using a pressure sensor to measure oil pressure and it will be connected to a Modbus RTu 485 with a UART TTL module (without re and de pins). when I try to create a project using the first Arduino as slave and the second Arduino as master. When I use a code containing a pressure sensor, error 224 occurs, but when I use just a potentiometer, communication can occur and is displayed on the LCD.
for code slaves using pressure sensor:

#include <ModbusRtu.h>
#define ID 1  // ID Slave
Modbus slave(ID, 0, 0);
// Jumlah register Modbus RTU
uint16_t data[3];
const int analogPin = A0;
const int potPin = A1; // Pin untuk potensiometer
void setup() {
  Serial.begin(9600);  // Komunikasi serial
  slave.begin(9600);   // Inisialisasi Modbus RTU
}
void loop() {
  // Membaca nilai dari potensiometer
  int pot = analogRead(potPin);
  // Membaca nilai dari sensor tekanan
  int sensorValue = analogRead(analogPin); 
  // Menghitung tegangan dari nilai analog
  float voltage = sensorValue * (5.0 / 1023.0); // Mengasumsikan tegangan referensi 5V
  // Menghitung tekanan menggunakan interpolasi linier
  float pressureBar = (voltage - 0.51) * (10.0 / (4.5 - 0.51)); // 0.52V = 0 Bar, 4.5V = 10 Bar
  if (pressureBar < 0) {
    pressureBar = 0; // Pastikan tekanan tidak negatif
  }
  float pressureKPa = pressureBar * 100; // Mengonversi Bar ke kPa
  // Menyimpan nilai ke dalam register
  data[0] = (uint16_t)(pressureKPa * 100); // Mengalikan dengan 100 untuk dua desimal
  data[1] = map(pot, 0, 1023, 0, 1000);      // Nilai potensiometer dalam skala tertentu
  data[2] = sensorValue;                     // Menyimpan nilai mentah sensor jika diperlukan
  // Memproses permintaan dari master
  slave.poll(data, 3); // Menunggu permintaan dari master
  // Menampilkan nilai di Serial Monitor untuk debugging
  Serial.print("Voltage: ");
  Serial.print(voltage,4);
  Serial.print(" V, Pressure: ");
  Serial.print(pressureKPa,2);
  Serial.println(" kPa");
  delay(1000); // Delay untuk menghindari pembacaan yang terlalu cepat
}

and this for master code:

#include <ModbusMaster.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Konfigurasi LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Inisialisasi ModbusMaster
ModbusMaster master;
uint8_t slaveID = 1;  // ID Slave yang diakses
void setup() {
  // Konfigurasi Serial
  Serial.begin(9600);
  // Inisialisasi Modbus
  master.begin(slaveID, Serial);
  // Inisialisasi LCD
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Modbus Master");
  delay(2000);
}
void loop() {
  uint8_t result;
  uint16_t data[3]; // Buffer data untuk membaca 3 register dari slave
  // Meminta data dari slave (3 register mulai dari alamat 0)
  result = master.readHoldingRegisters(0, 3);
  if (result == master.ku8MBSuccess) {
    // Membaca nilai register ke buffer
    for (int i = 0; i < 3; i++) {
      data[i] = master.getResponseBuffer(i);
    }
    float pressureKPa = data[0] / 100.0; // Membagi dengan 100 untuk mengembalikan skala
    float voltage = data[1] / 100.0;
    // Menampilkan data di Serial Monitor (debugging)
    Serial.print("Pressure: ");
    Serial.print(pressureKPa, 2); // 2 angka desimal
    Serial.print(" kPa ");
    Serial.println(voltage,2);
    // Menampilkan data di LCD
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("P: ");
    lcd.print(pressureKPa,2);  // Menampilkan data potensiometer
    lcd.setCursor(0, 1);
    lcd.print("V: ");
    lcd.print(voltage,2); 
 // Menampilkan data register 1
  } else {
    // Menampilkan error di LCD
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Error: ");
    lcd.print(result);
  }
  delay(1000); // Interval 1 detik
}

This code just only potensiometer to check communication and working, an i add the pressure sensor make e communication error 226 and sometimes 224

#include <ModbusRtu.h>
#define ID 1  // ID Slave
Modbus slave(ID, 0, 0);

// Jumlah register Modbus RTU
uint16_t data[3];

void setup() {
  Serial.begin(9600);  // Komunikasi serial
  slave.begin(9600);   // Inisialisasi Modbus RTU
}

void loop() {
  slave.poll(data, 3);  // Menunggu permintaan dari master
  
  // Membaca data dari potensiometer
  int pot = analogRead(A0);
  data[0] = map(pot, 0, 1023, 0, 1000);  // Potensiometer nilai 0-1000
}

please help me so that my project can be completed, sometimes the code work but when im try to change to tried to fix the pressure code and re-upload it but an error occurred and it couldn't communicate, even though the change was only in the form of the voltage number

Format your code.

Use code tags like suggested above. And describe the difference between "working" and non working" code.

please help sir

You have an grave-accent-a in your code. Find it. Replace it.

but sometime the code work and when i try again with same code and wiring make it error 224

Show the error.

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