[solved] Serial Communication Problem

Hello,

Iam currently building a model railroad using Arduinos to automate everything.

I have 5 Arduinos named A0-4.
A0 is the master.
A1 to A4 the slaves.
They are wired like this: A0 TX goes to RX on A1-4, A0 RX goes to TX on A1-4.
All Arduinos are connected to the same Ground and 5V.

Communication looks like this:
A1-4 are constantly listening for messages
A0 sends out a message using Serial.println.
The message is composed like this: the first two characters address the targeted Arduino "A1" for example. Then I add zeros and ones to tell the target what to do. The message is decoded by the target.
Then the target sends out an answer in the same way, A100100 for example.

This all works perfectly fine as long as only two Arduinos communicate.
When I add more Arduinos I still can send instructions from the master to the slaves but I wont get an answer.

Can someone tell me if this is a hardware/serial problem or should it work like described and my programm is the problem?

If the master can send to one slave, and wait until it gets a response, 4 software serial instances would make more sense. Call them A1, A2, A3, and A4. Then, use A1.print(), A2.print(), etc. The message then won't need to contain the ID of the recipient, since the message will only go to one slave.

If it needs to send to all slaves at once, and deal with the replies as they come in, you should still use a SoftwareSerial instance to talk to the slaves, and on the slaves to talk to the master, so you can use the hardware serial port to talk to the PC/debug your programs.

Of course, it could be the slave programs or the master program that is causing the problem. We can't see them, or any proof that things aren't working just fine.

Thanks for your answer.

Iam not familiar with software serial instances but as far as I can tell from googling it I need a pair of wires for every instance, correct?

There is no case where I need an answer from multiple Arduinos at the same time.
A1-4 only ever send information as a response to a message which is addressed to the specific Arduino received from A0. A1-4 are only there to turn on/off relais and collect the input of 30+ phototransistors.

Someone from another board suggest that perhaps the state of the TX pins of the other Arduinos could interfere with the message.
Is there a way to turn on/off TX pins only when they are needed to send a message?

Unfortunately I need almost all analog pins so I cant use I2C.

@OP

I have been preaching and preaching that UART Port is called 'Port' because we cannot make parallel connection of more than one UART devices/sensors.

On the other hand, I2C Bus is a 'Bus' because we can make parallel connection of more than one I2C devices/sensors.

UART Port works upto 50' without conversion into RS232 logic; RS232 logic travels upto 200'.

I2C Bus operates only with 2'?

My recommendation is:
1. Connect AR0 (Arduino - 0) with AR1 using Sotware UART Port (SUART Port). You might need the hardware UART Port (UART Port) for debugging and sketch uploading purposes. It is simple to create SUART Port by including only 3 lines codes in the sketch.

2. Connect AR1, AR2, AR3, and AR4 using Software I2C Bus as you need the A4, A5 lines to acquire analog signals.

3. SUART Codes for AR0 (only for test purposes: untested)

#include<SoftwareSerial.h>   //the header files comes with the IDE
SoftwareSerial SUART(2, 3); // DPin-2 works as SRX-pin; Dpin-3 as STX-pin

void setup()
{
   Serial.begin(9600);
   SUART.begin(9600);
}

void loop()
{
   SUART.write(0x41); //send binary value 0x41 (ASCII of A) to AR1
   delay(1000); //at 1sec interval
}

//----- Codes for AR1's SUART Port--------

#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3);

void setup()
{
    Serial.begin(9600);
    SUART.begin(9600);
}

void loop()
{
   byte n = SUART.available();
   if (n !=0)
   {
      byte x = SUART.read();   //x = 0x41
      Serial.print((char)x);       //character A should appaer on Serial Monitor of AR1
   }
}

4. Soft I2C Codes for I2C Master AR1 (untested)

#include<SoftwareWire.h>  //you have to get it from GitHub
SoftwareWire myWire(4, 5); //SSDA = DPin-4, SSCL = DPin-5

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

   //----roll calling I2C Slave-1 (AR2) with address 0x23-------
   myWire.beginTransmission(0x23); //slave-1Address); 0x23
   byte busStatus = myWire.endTransmission();
   if(busStatus !=0x00)
   {
      Serial.print("Slave is absent...!");
      while(1);  //wait for ever
   }
}

void loop()
{

}

//--- Codes for Soft I2C Slave-1 (AR1) ------

#include<SoftwareWire.h>
SoftwareWire myWire(4, 5);

void setup()
{
   Serial.begin(9600);
   myWire.begin(0x23); //
}

void loop()
{

}

Hope that the post might be useful. Just for you information that the SUART and SOFTI2C work; but, the codes presented in this post are not tested. Any query/question will be welcomed.

If you want to connect one Arduino to several others using Serial you can connect the A0 Tx to the Rx on the other 4. But the Serial system idles HIGH so you must isolate the Tx from the other 4 from each other. You can easily do this using a diode for each slave Tx oriented so that the slave can pull the master Rx LOW but the HIGH from the slave is blocked. Then the master Rx needs a pullup resistor - 4k7 or 5k6 should be fine.

Of course you will also need a protocol to prevent two slave trying to talk at the same time.

I am using a system like this for a club model railway system.

Life will probably be easier if you use a Mega for the master as then you can use Serial1 to talk to the other Arduinos and kee Serial free for debugging.

...R
Serial Input Basics - simple reliable ways to receive data.

Robin2:
If you want to connect one Arduino to several others using Serial you can connect the A0 Tx to the Rx on the other 4. But the Serial system idles HIGH so you must isolate the Tx from the other 4 from each other. You can easily do this using a diode for each slave Tx oriented so that the slave can pull the master Rx LOW but the HIGH from the slave is blocked. Then the master Rx needs a pullup resistor - 4k7 or 5ks should be fine.

THANK YOU!

Works like a charm now!