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;
}
}
}