Arduino Tx, Rx with MAX485

Hello Friends,

I'm a new to arduino world. I needed to establish a master slave connection between arduino's using rs-485 protocol.

I succeeded in doing so by connecting the serial ports of both arduino's. But, when i tried to interpret the same code in rs-485 communication using MAX485 between arduino's it doesn't work.
when trying the code using the chip, I uncommitted "digitalWrite(controlPin, HIGH);"

This is the code for my slave arduino(arduino uno):

#define myID 1
#define controlPin 3

int called_ID;

void setup() {
  Serial.begin(9600);
  //pinMode(controlPin, OUTPUT);
  //digitalWrite(controlPin, LOW);
}

void loop() {

  
  called_ID = Serial.read();
  //digitalWrite(controlPin, HIGH);

  if(called_ID == myID){
    
    Serial.println("" + String(10)+","+ String(20)+","+ String(30)+","+ String(40)+","+ String(50)+","+ String(60));
    delay(100);
    //digitalWrite(controlPin, LOW);

    called_ID = 0;
  }

}

This is the code for my master arduino(arduino mega):

#define slave_1 1
#define slave_2 2
#define slave_3 3
#define slave_4 4
#define slave_5 5
#define slave_6 6

#define controlPin 3



void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);

  pinMode(controlPin, OUTPUT);
  Serial.println("Setup is ready!");
  Serial.println("Waiting for the communication process...");
  digitalWrite(controlPin, LOW);
}

void loop() {
  read_slave_1();
}

And, read_slave_1() function:

const byte numChars = 32;
char receivedChars[numChars];  // an array to store the received data

boolean newData = false;

void read_slave_1() {
  //digitalWrite(controlPin, HIGH);
  Serial1.write(slave_1);
  //digitalWrite(controlPin, LOW);
  latchData();
  //digitalWrite(controlPin, LOW);
  printData();
}

void latchData() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  while (Serial1.available() > 0 && newData == false) {
    rc = Serial1.read();

    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
    }
  }
}

void printData() {
  if (newData == true) {
    Serial.print("This just in ... ");
    Serial.println(receivedChars);
    newData = false;
  }
}

I couldn't figure out the problem. can anyone please help me in this regards? I would really appreciate it.

Thank you.

I'm a new to arduino world.

That's no excuse for relying on crutches like the String class.

A schematic would be in order, along with some proof that what you send is not what you get.

Renaz:
Hello Friends,

I'm a new to arduino world. I needed to establish a master slave connection between arduino's using rs-485 protocol.

I succeeded in doing so by connecting the serial ports of both arduino's. But, when i tried to interpret the same code in rs-485 communication using MAX485 between arduino's it doesn't work.
when trying the code using the chip, I uncommitted "digitalWrite(controlPin, HIGH);"

as PaulS said, a schematic is really important. The reason Pauls is twitting you about using the String class, is because it is a RAM and FLASH hog. Unless you are doing almost nothing in you program, it is better to learn how to use "C strings", basically array of char's.
they are much less resource hungry.

Nick Gammon has written a very good comparison between Cstrings, and the String Object here: Nick Gammon's String Comparison you should read it.

PaulS:
That's no excuse for relying on crutches like the String class.

A schematic would be in order, along with some proof that what you send is not what you get.

Here is part of a RS485 circuit I use. This screen grab actually has 2 RS485 channels on it. Notice the pullups on the R pin. I have tied DE,!RE together, so I am using simplex communication, only receive or transmit. not both.

The pullup on R is important because whenever DE,!RE is high(transmit mode), R is tristated, floating. without this pullup the UARTs in the AVR's sometimes see erroneous start bits. this causes garbage in the RX buffer. My other Pullups and pull downs force the RS485 drivers into a standby state whenever the AVR is in Reset or not actively controlling it.

Without a Schematic, and the exact result you are seeing it is very hard to guess what is failing.

Here is simple code to transmit. I don't recommend using delay().

#define txModePin 3  //Arduino pin connected to DE,!RE

void txBuffer(char *buf, uint8_t len){

digitalWrite(txModePin,HIGH); // into TX Mode
Serial.print(buf,len);
Serial.flush();                        // flush returns after all characters have been actually sent out the TX pin.
digitalWrite(txModePin,LOW); // back into receive mode
}

Chuck.

Mr PaulS
I'll bear it in mind. Thanks for getting back to me :slight_smile:

Thank you Mr Chuck.
Your reply is Really helpful.
I'll try it and check it out :slight_smile: