RS485 Issues with code


I am trying to send data from an SD card on one arduino to another arduino using RS485. I have been slowly adding to the code below and currently know it can read data from the SD card correctly and that the RS485 is wired up correctly. I have managed to get single numbers to bounce between the two using example codes online. Ideally I am trying to send a “float” or an “int” however I have spent the day looking online with no success.

Any help would be greatly appreciated.

The code i am using on the transmit side is the:

  SD card sending info over RS485

/*-----( Import needed libraries )-----*/
#include <SoftwareSerial.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define RS485rx    7 // RS485 Receive pin
#define RS485Tx   8 // RS485 Transmit pin
#define RS485inout  3 // RS485 Transmit or Receive status

#define RS485Transmit HIGH
#define RS485Receive  LOW
#define ledPin      13
#define baudRate    115200

/*-----( Declare objects )-----*/
SoftwareSerial RS485(RS485rx, RS485Tx); // RX, TX

/*-----( Declare Variables )-----*/
int check;

/*-----( SD Card )-----*/
#include <SPI.h>
#include <SD.h>
File myFile;
const int chipSelect = 10;

float T;  // Checking temp previous 
float P1;  // Checking temp previous 
float test;
int i;
int z;

/*+++++++++++++++++++++++++++++++   RTC Setup   +++++++++++++++++++++++++++++++*/
#include "RTClib.h"
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int Sec;

// -----------------------------------------------------------------
// SETUP         SETUP         SETUP         SETUP         SETUP
// -----------------------------------------------------------------
void setup() {      /****** SETUP: RUNS ONCE ******/
  /*-----( RS485 )-----*/
pinMode(RS485inout, OUTPUT);
// Set RS485 device to read initially
  digitalWrite(RS485inout, RS485Receive);
   /*-----( SD Card )-----*/ 
  pinMode(chipSelect, OUTPUT);
  // Open serial communications and wait for port to open:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only

  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  Serial.println("initialization done.");
 // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile ="TEST1.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
 } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");

/*+++++++++++++++++++++++++++++++   RTC Start Up   +++++++++++++++++++++++++++++++*/


// -----------------------------------------------------------------
// LOOP     LOOP     LOOP     LOOP     LOOP     LOOP     LOOP
// -----------------------------------------------------------------
void loop() {

 /*-----( SD Card Code )-----*/  
myFile ="TEST1.txt");
if (myFile) {
File TEST1; // The file with your data in it.

 /*-----( For loop which now continuosly runs )-----*/
//for (i=0; i < 8000; i=i+19){
for (i=0; i < 98000; i=i+19){


    DateTime now =;
    Sec = ((now.second()));
    Serial.print(now.year(), DEC);
    Serial.print(now.month(), DEC);
    Serial.print(, DEC);
    Serial.print(" (");
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(now.minute(), DEC);
    Serial.print(now.second(), DEC);


uint32_t fileSize = myFile.size(); - (fileSize-i)); 

char temp [9];
char oldRecord [18];, 18); // Read 18 bytes.
oldRecord[18] = 0; // Null termination (may not be needed)  
strncpy(temp, oldRecord, 9); // Read 9 characters from line starting at the 1st character and copy this into char array temp.
T = atof(temp);

char press1 [9];
strncpy(press1, oldRecord + 9, 9);
P1 = atof(press1);
Serial.println(P1,4); // print the first 4 digits 

/*-----( Should make RS485 Transmit )-----*/ 
// Set the RS485 to transmit mode and send the value
   digitalWrite(RS485inout, RS485Transmit);
   check = (P1);
  //check = String(P1); 
  //check = (P1,3); 

The receiving side has the following very simple code:

/*-----( Import needed libraries )-----*/
#include <SoftwareSerial.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define SSerialRX        00  //Serial Receive pin
#define SSerialTX        01  //Serial Transmit pin

#define SSerialTxControl 3   //RS485 Direction control
#define RS485Transmit    HIGH
#define RS485Receive     LOW

#define Pin13LED         10

/*-----( Declare objects )-----*/
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX

/*-----( Declare Variables )-----*/
int byteReceived;
int byteSend;

void setup()   /****** SETUP: RUNS ONCE ******/
  // Start the built-in serial port, probably to Serial Monitor
  Serial.println("SerialRemote");  // Can be ignored
  pinMode(Pin13LED, OUTPUT);   
  pinMode(SSerialTxControl, OUTPUT);  
  digitalWrite(SSerialTxControl, RS485Receive);  // Init Transceiver
  // Start the software serial port, to another device
  RS485Serial.begin(4800);   // set the data rate 
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
  //Copy input data to output 
  digitalWrite(SSerialTxControl, RS485Receive);  // Init Transceiver 
  if (RS485Serial.available()) 
    digitalWrite(SSerialTxControl, RS485Receive);  // Init Transceiver
    byteSend =;   // Read the byte 



  }// End If RS485SerialAvailable
}//--(end main loop )---

Look at Serial.write() instead of Serial.print(). Or in your case, RS485.write().


I had a look at what you said and i though that RS485.write((check)); would have sent the command? I'm using Serial.println((check)); to make sure Check was actually a number in some of the versions of the code I tried.

Does anyone have any ideas?

#define baudRate 115200

SoftwareSerial doesn't work reliable above 19200 baud. If you need higher baud rates, use a hardware serial interface.

It's also very likely that you have a memory overrun. Your sketch uses 1497 bytes for literal strings and the like. The SD card library uses another 512 bytes for a local sector copy. The UNO has only 2kB of RAM, so I'm quite sure you run out of it.

Hi Pylon,

Any other ideas on how to write the code to achieve the same result?

Thank you for the reply

Hi Pylon,

Any other ideas on how to write the code to achieve the same result?

Thank you for the reply

Did you try the suggested changes and did they help?


Any other ideas on how to write the code to achieve the same result?

Learn about the F() Macro, it may reduce memory usage.

And use the hardware serial interface for the communication. This may mean you're loosing the easy debugging channel but if you need high baud rates this should be worth it.

If that doesn't help, it may be time to tell us more about what exactly you're trying to achieve and why you decided the way you did.