Hi my name is Merel,I am working on my graduation project and now I am stocked
I bought PTA8D08 and a arduino UNO and a RS485/TTL (Dollatec) converter. This is how I connected the wires:
- external power supply to the PTA8D08 ground and VIN
- PTA8D08 A+ B- to RS485 A and B respectively
- RS485 VCC and GND to Arduino 5V and GND
- RS485 DI to Arduino pin RX
- RS485 DE to Arduino pin 2
- RS485 RE to Arduino pin 4
- RS485 R0 to Arduino pinTX
I tried with ChatGPT to generate the code. It uses the library ModbusMaster.h but it gives an 'error reading data!'
Can anybody help me with the code.
This is the code I now have:
#include <SoftwareSerial.h>
#include <ModbusMaster.h>
#define DE_PIN 2 // Pin voor Driver Enable op RS485-converter
#define RE_PIN 4 // Pin voor Receiver Enable op RS485-converter
#define RS485_BAUD_RATE 9600 // Baudrate van RS485-communicatie
#define PTA8D08_ADDRESS 1 // Adres van de PTA8D08-sensor op het RS485-netwerk
SoftwareSerial rs485Serial(2, 4); // RX, TX
// Create a ModbusMaster object
ModbusMaster node;
void setup() {
Serial.begin(9600); // Begin seriële communicatie met de computer
rs485Serial.begin(RS485_BAUD_RATE); // Begin seriële communicatie met RS485-converter
pinMode(DE_PIN, OUTPUT); // DE_PIN instellen als uitgang
pinMode(RE_PIN, OUTPUT); // RE_PIN instellen als uitgang
// Initialize Modbus communication
node.begin(PTA8D08_ADDRESS, rs485Serial); // Modbus-adres van de sensor is 1, RS485-communicatie
}
void loop() {
// Wissel naar de transmissiemodus (TX)
digitalWrite(DE_PIN, HIGH);
digitalWrite(RE_PIN, HIGH);
// Stuur verzoek om temperatuurgegevens naar PTA8D08-sensor
uint8_t result = node.readHoldingRegisters(0, 1); // Commando om temperatuur te lezen (verander dit naar het juiste commando)
// Check if the request was successful
if (result == node.ku8MBSuccess) {
// Wissel naar ontvangsmodus (RX)
digitalWrite(DE_PIN, LOW);
digitalWrite(RE_PIN, LOW);
// Lees temperatuurgegevens van de sensor
float temperatureCelsius = node.getResponseBuffer(0); // Assuming temperature is in the first register
// Print de temperatuur naar de seriële monitor
Serial.print("Temperatuur: ");
Serial.print(temperatureCelsius);
Serial.println(" °C");
} else {
Serial.println("Error reading data!");
}
// Wacht voordat je opnieuw verzoekt naar temperatuurgegevens
delay(5000); // Wacht 5 seconden voordat je opnieuw gegevens aanvraagt
}