RS485 to arduino

i have here my code
can someone help me in UART
the thing is im using RS485 module in 9600 setting
is there any way to connect from my Arduino MEGA


MEGA CODE:
#include <Wire.h>
#include “Adafruit_AHTX0.h”
#include <SoftwareSerial.h>

#define DE 4
#define RE 3

SoftwareSerial RS485Serial(10, 11); // RX, TX for RS485 communication

Adafruit_AHTX0 aht;

void setup() {
Serial.begin(9600); // Start serial communication at 115200 baud rate
RS485Serial.begin(921600); // Start RS485 communication at 9600 baud rate
Wire.begin(); // Initialize I2C communication

pinMode(DE, OUTPUT); // Set RS485 direction control pin DE as output
pinMode(RE, OUTPUT); // Set RS485 direction control pin RE as output
digitalWrite(DE, LOW); // Set DE low for receive mode
digitalWrite(RE, LOW); // Set RE low for receive mode

if (!aht.begin()) {
Serial.println(“AHT30 not detected. Check wiring.”);
while (1) delay(10); // If sensor initialization fails, halt the program
}

Serial.println(“AHT30 initialized”); // Confirm successful initialization
}

void loop() {
sensors_event_t humidity, temp;
aht.getEvent(&humidity, &temp); // Get temperature and humidity readings

float temperature = temp.temperature; // Extract temperature
float rh = humidity.relative_humidity; // Extract relative humidity

// Prepare the message string to send over RS485
String message = String(temperature, 2) + " °C, " + String(rh, 2) + " %";

// Send the message over RS485
digitalWrite(DE, HIGH); // Set DE high to enable transmission
digitalWrite(RE, HIGH); // Set RE high to enable transmission
RS485Serial.println(message); // Send the message

// Set DE/RE low to return to receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);

delay(1000);
}


AMB82 MINI CODE:

#include “Arduino.h”

// RS-485 Direction Control Pins
#define RS485_DE 17 // Drive Enable
#define RS485_RE 16 // Receive Enable (pick any spare GPIO)

// UART Serial1 Pins (PA2/PA3 are default for Serial1)
#define RS485_RX 22
#define RS485_TX 21

String incomingMessage = “”;

void setup() {
Serial.begin(115200); // Debug monitor

// Begin RS485 Serial1 communication on default Serial1 pins
Serial1.begin(9600);    // PA2 = TX, PA3 = RX

pinMode(RS485_DE, OUTPUT);
pinMode(RS485_RE, OUTPUT);

// Set RS485 to receive mode
digitalWrite(RS485_DE, LOW);
digitalWrite(RS485_RE, LOW);

Serial.println("AMB82 Mini RS-485 Receiver (PA2/PA3) Ready");

}

void loop() {
while (Serial1.available()) {
char c = Serial1.read();
if (c == ‘\n’) {
Serial.print("Received string: ");
Serial.println(incomingMessage);
incomingMessage = “”;
} else {
incomingMessage += c;
}
}
}

Hi @MJK ,

Please make sure that your baud rate is matched between RS485Serial on Arduino controller and Serial1 on AMB82 Mini.

Thank you.

Im using 9600 and still not working

Am i using the correct pin naming ..for GPIOs

AMB82MINI Code


#include “Arduino.h”

// RS-485 Direction Control Pins
#define RS485_DE 17 // Drive Enable
#define RS485_RE 16 // Receive Enable

// UART Serial1 (PA2 = TX, PA3 = RX)
#define RS485_BAUDRATE 9600

String incomingMessage = “”;

void setup() {
// Debug Serial Monitor
Serial.begin(115200);
while (!Serial); // Wait for Serial monitor

// RS-485 UART
Serial1.begin(RS485_BAUDRATE);

// Setup direction control pins
pinMode(RS485_DE, OUTPUT);
pinMode(RS485_RE, OUTPUT);

// Set to receive mode
digitalWrite(RS485_DE, LOW);
digitalWrite(RS485_RE, LOW);

Serial.println("AMB82-Mini RS-485 Receiver Ready (921600 baud)");

}

void loop() {
// Read incoming RS-485 bytes
while (Serial1.available()) {
char c = Serial1.read();

    if (c == '\n') {
        // End of message
        Serial.print("Received string: ");
        Serial.println(incomingMessage);
        incomingMessage = "";
    } else {
        incomingMessage += c;
    }
}

// Optional: You can add a heartbeat LED or status blink here

}

#include “Arduino.h”

// RS-485 Direction Control Pins
#define RS485_DE PD_17 // Drive Enable (DE)
#define RS485_RE PD_14 // Receive Enable (RE)

// RS-485 Serial1: PA_2 (TX), PA_3 (RX)
#define RS485_BAUDRATE 9600

// Optional status LED
#define STATUS_LED PF_9 // Change if needed

String incomingMessage = “”;
unsigned long previousMillis = 0;
const long blinkInterval = 1000;
bool ledState = false;

void setup() {
// Start debug serial
Serial.begin(115200);
delay(100); // Ensure serial output is not lost

Serial.println("=== Booting AMB82-Mini RS-485 Receiver ===");

// Setup RS-485 control pins
pinMode(RS485_DE, OUTPUT);
pinMode(RS485_RE, OUTPUT);

// Set RS-485 to receive mode
digitalWrite(RS485_DE, LOW);
digitalWrite(RS485_RE, LOW);

// Setup Serial1 for RS-485 communication
Serial1.begin(RS485_BAUDRATE);
delay(50); // Let UART settle

// Setup optional status LED
pinMode(STATUS_LED, OUTPUT);
digitalWrite(STATUS_LED, LOW);

Serial.println("RS-485 Ready. Listening for messages...");

}

void loop() {
// Handle RS-485 incoming messages
while (Serial1.available()) {
char c = Serial1.read();
if (c == ‘\n’) {
Serial.print("Received string: ");
Serial.println(incomingMessage);
incomingMessage = “”;
} else {
incomingMessage += c;
}
}

// Status LED blink
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= blinkInterval) {
    previousMillis = currentMillis;
    ledState = !ledState;
    digitalWrite(STATUS_LED, ledState);
}

}


i tried another attept but this appear

It now works finally! Thank you

1 Like