Go Down

Topic: Arduino RS485 serial communication problem (Read 387 times) previous topic - next topic

ambropete

Hi all,

I am trying to create a RS-485 Bussystem in which a master collects sensor data from several slaves. I want the master to ask let's say five slaves in a row for the measured distance data from the hc-sr04 ultrasonic sensor. This distance should be printed out on the serial monitor. The problem I'm facing is that I cannot get the serial conversation between the arduinos get started. I used an Elegoo MEGA2560 R3 as master, an Arduino nano as slave, the hc-sr04 ultrasonic sensor and two RS-485 modules to connect the two controllers.

The connections on the MEGA2560 are the following:
RX0 is connected to R0 of the first RS-485 module,
TX0 is connected to D1 of the first RS-485 module
and PWM PIN 2 is connected to DE and RE.

On the output side of the RS-485 module B and A are plugged into the + and - of my breadboard where the next RS-485 module B and A inputs are also connected to.
R0 PIN goes to Arduino nano (which is the slave) TX1
D1 PIN goes to Arduino nano RX1
RE and DE PINS go to Arduino nano D2.

The trigger PIN of the HC-SR04 is connected with D11 on the nano,
the echo PIN of the HC-SR04 to the D12 on the nano.

I attached the master and slave code and three pictures of my setup to this blog. The codes are partlyderived from http://www.naylampmechatronics.com/blog/37_Comunicaci%C3%B3n-RS485-con-Arduino.html.


I would greatly appreciate any help!

Best regards,

ambropete


MasterCode:



const int EnTxPin =  2; // HIGH:TX y LOW:RX
void setup()
{
 Serial.begin(9600);
 Serial.setTimeout(100); //establecemos un tiempo de espera de 100ms
 // inicializamos los pines
 pinMode(EnTxPin, OUTPUT);
 digitalWrite(EnTxPin, HIGH); //RS485 como Transmisor
}

void loop()
{
 
 //---solicitamos una lectura del sensor----------
 Serial.print("I"); //inicio de trama
 Serial.print("101");//direccion del esclavo
 Serial.print("L"); //L para indicarle que vamos a Leer el sensor
 Serial.print("F"); //fin de trama
 Serial.flush();    //Esperamos hasta que se envíen los datos
 //----Leemos la respuesta del Esclavo-----
 digitalWrite(EnTxPin, LOW); //RS485 como receptor
 if(Serial.find("i"))
 {
     int esclavo=Serial.parseInt();  //recibimos la direccion del esclavo
     int Distanz=Serial.parseInt();  //recibimos el dato
     if(Serial.read()=='f'&&esclavo==101) //si fin de trama y direccion son los correctos
     {
        Serial.print("Distanz=");
        Serial.print(Distanz);
        Serial.println("cm");
        delay(500);
     }
 }
     digitalWrite(EnTxPin, HIGH); //RS485 como Transmisor
 //----------fin de la respuesta----------
 
}



SlaveCode:



#define trigPIN 11
#define echoPIN 12
const int EnTxPin =  2;  // HIGH:TX y LOW:RX
const int mydireccion =101; //Direccion del esclavo
void setup()
{
 Serial.begin(9600);  
 Serial.setTimeout(100);  //establecemos un tiempo de espera de 100ms
 pinMode(trigPIN,OUTPUT);
 pinMode(echoPIN, INPUT);
 pinMode(EnTxPin, OUTPUT);
 digitalWrite(EnTxPin, LOW); //RS485 como receptor
}

void loop()
{
 if(Serial.available())
 {
   Serial.print("Hastalavista");
   if(Serial.read()=='I') //Si recibimos el inicio de trama
   {
       int direccion=Serial.parseInt(); //recibimos la direccion
       if(direccion==mydireccion) //Si direccion es la nuestra
       {
           char funcion=Serial.read(); //leemos el carácter de función
         
         
            //---Si el carácter de función  es L entonces el maestro está solicitando una lectura del sensor---
            if(funcion=='L')
            {
             int duration, distance;
             digitalWrite(trigPIN, LOW);
             delayMicroseconds(2);
             digitalWrite(trigPIN, HIGH);
             delayMicroseconds(10);
             digitalWrite(trigPIN, LOW);
             duration=pulseIn(echoPIN, HIGH);
             distance=(duration/2)*0.034;
             
               if(Serial.read()=='F') //Si el fin de trama es el correcto
                {
                  int lectura = distance; //realizamos  la lectura del sensor    
                  digitalWrite(EnTxPin, HIGH); //rs485 como transmisor
                   Serial.print("i"); //inicio de trama  
                  Serial.print(mydireccion); //direccion  
                  Serial.print(",");      
                  Serial.print(lectura); //valor del sensor
                  Serial.print("f"); //fin de trama  
                  Serial.flush(); //Esperamos hasta que se envíen los datos
                  digitalWrite(EnTxPin, LOW); //RS485 como receptor            
                }
            }
       }
   }
 }
 delay(10);
}

sterretje

#1
Mar 17, 2018, 03:02 am Last Edit: Mar 17, 2018, 03:02 am by sterretje
1)
Please use code tags when posting code. Please edit your post
Type [code] before the code
Type [/code] after the code

2)
Please make a simple diagram how everything is connected; a photo/scan of a hand-drawn one is fine and far better than a description that can be misunderstood.

3)
You're using a Mega with three additional serial ports; wire the RS485 to e.g. the pins for Serial1 and use Serial1 (Serial1.write, Serial1.print, Serial1.available, Serial1.read, ...) for the communication over the RS485 bus and the Serial for communication with the PC.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up