RS485 Serial

RS485 - Comunicação Serial

Com este circuito pretende-se fazer um teste com 3 Arduinos em que vão comunicar de forma a existir 1 Master e 2 Slaves.

Esquema de ligação de um Arduino a um MAX485:

Através deste protocolo podemos estabelecer comunicação entre dispositivos até 1200 metros. 

Esquema geral de ligações entre os 3 Arduinos: 

Para grandes distâncias, derivado a interferências, temos de usar uma resistência de 120R "termination resistance", coloca-se entre os pinos A e B no primeiro e no último MAX485 da nossa linha de comunicação:

 

Neste caso não vamos utilizar esta resistência de 120R e relativamente ao esquema 'geral' acima, não vamos colocar nenhum condensador e o Pino no Arduino (para definir o modo no MAX485 , se está a transmitir ou a receber) para controlar os pinos RE/DE é o 3: 

 

Código utilizado:

MASTER

/* RS485 Master Node S Example*/

/*-----( Declare Variables )-----*/

int EN = 2;

void setup() /****** SETUP: RUNS ONCE ******/

{

  pinMode(EN, OUTPUT );

  Serial.begin (9600);

}//--(end setup )---

void loop()    /****** LOOP: RUNS CONSTANTLY ******/

{

  // Send Data

  digitalWrite(EN, HIGH ); // enable send

  Serial.print ( 'A' );

  delay(1000);

  Serial.print ( 'B' );

  delay (1000);

}//--(end main loop )---

SLAVE A

/* RS485 Slave Node A Example*/

/*-----( Declare Variables )-----*/

int ledPin=13;

int EN = 2;

int Val;

void setup()/****** SETUP: RUNS ONCE ******/

{

  pinMode(ledPin, OUTPUT );

  pinMode(EN, OUTPUT );

  Serial.begin (9600);

}//--(end setup )---

void loop()  /****** LOOP: RUNS CONSTANTLY ******/

{

  // receive Data

  digitalWrite (EN, LOW ); // enable receive

  Val = Serial.read ();

  if (-1 != Val) 

  {

    if ( 'A' == Val) 

    {

      digitalWrite (ledPin, HIGH );

      delay (500);

      digitalWrite (ledPin, LOW );

      delay (500);

    }

  }

}//--(end main loop )---

SLAVE B

/* RS485 Slave Node B Example */

/*-----( Declare Variables )-----*/

int ledPin=13;

int EN = 2;

int Val;

void setup()/****** SETUP: RUNS ONCE ******/

{

  pinMode(ledPin, OUTPUT );

  pinMode(EN, OUTPUT );

  Serial.begin (9600);

}//--(end setup )---

void loop()  /****** LOOP: RUNS CONSTANTLY ******/

{

  // receive Data

  digitalWrite (EN, LOW ); // enable receive

  Val = Serial.read ();

  if (-1 != Val) 

  {

    if ( 'B' == Val) 

    {

      digitalWrite (ledPin, HIGH );

      delay (500);

      digitalWrite (ledPin, LOW );

      delay (500);

    }

  }

}//--(end main loop )---

Informação disponível em http://arduino-info.wikispaces.com/RS485-Brick 

Vídeo final: