Hello,
I am trying to send commands from a master arduino to a slave arduino, both UNOs, using MAX485 modules (TLL to RS485, 8 pins). I am using SoftwareSerial because of issues with hardwareSerial (RX (pin 0) and TX (pin 1) not working when the rs485 module is connected to them, even preventing me from uploading code to the arduino). Simply trying to make an LED blink for now.
Here is the code for both arduinos as well as the wiring diagram. Both arduinos are powered via the barrel plugs (9v) or from the USB cable when I'm trying to debug the setup. My issue is that the command "A" is simply not being sent and I can't understand why. I'm not getting confirmation from my debugging lines in the serial monitor.
This is slightly modified from a tutorial I found on youtube: https://www.youtube.com/watch?v=ZR9QEWpGEDo
master code
#include <SoftwareSerial.h>
#define MASTER_EN 8 // RS485 Enable Pin
#define RX 10 // SoftwareSerial RX
#define TX 11 // SoftwareSerial TX
SoftwareSerial rs485Serial(RX, TX); // Create SoftwareSerial instance
void setup() {
pinMode(MASTER_EN, OUTPUT);
rs485Serial.begin(9600);
digitalWrite(MASTER_EN, LOW);
}
void loop() {
digitalWrite(MASTER_EN, HIGH); // Enable transmit mode
delay(10); // 5ms minimum delay
rs485Serial.write('A'); // Send character
rs485Serial.flush(); // Wait until data is sent
digitalWrite(MASTER_EN, LOW); // Back to receive mode
delay(1000); // Wait before next send
}
slave code
#include <SoftwareSerial.h>
#define SLAVE_EN 8 // RS485 Enable Pin (DE/RE)
#define LED 13 // On-board LED
#define RS485_RX 10 // SoftwareSerial RX
#define RS485_TX 11 // SoftwareSerial TX
SoftwareSerial rs485(RS485_RX, RS485_TX); // RX, TX
void setup() {
pinMode(SLAVE_EN, OUTPUT);
pinMode(LED, OUTPUT);
digitalWrite(SLAVE_EN, LOW); // Set to receive mode
rs485.begin(9600); // RS485 communication
Serial.begin(9600); // Optional: for debugging via Serial Monitor
}
void loop() {
if (rs485.available()) {
char incoming = rs485.read();
if (incoming == 'A') {
digitalWrite(LED, !digitalRead(LED)); // Toggle LED
Serial.println("Received: A"); // Debug info
delay(100);
}
}
}