3+ more Arduino Serial Communication in one port

Hi guys, I need a way to have 3 or more arduinos in comunication with only one serial port.
I know Arduino Mega has several Serial Ports that could be used, but in my case I just have an atmega328 with one only port (rx and tx).

The idea was of some arduino stations with a RFID module each. Whenever a tag is detected by the sensor, send the ID of the card is sent to the master (another arduino). The master arduino will decide if the card is allowed or not and will reply to the Arduino station.

Is there a way to connect all the Serial ports in series one to the other and send data with an address like this?

Arduino Station slave n1 [Address: 01]
Arduino Station slave n2 [Address: 11]
Arduino Station slave n3 [Address: 10]
Arduino Master [Address 00]

If Arduino Station n1 detects a card, it will send to the Master the Address+the Id of the card (eg: 00255255255255: where 00 is master address and 255255255255 is the 4 bytes ID)
I could program each arduino like: I read the serial data when available, if the address read is mine (egs the Master when data is sent from Arduino Station Slave to Master) I process it and reply with the slave address, meanwhile if the address isnt mine (egs another arduino station reading data from another arduino station) I just send it again as it is

You will need 3 serial ports.
One per Arduino.
Use software serial library.

You can connect one master to several slaves using a single serial port on the master. However you will need some extra components to prevent the signals coming from the slaves interfering with each other.

The problem is that the TX lines from the slaves idle HIGH so you need to prevent that from getting to the RX pin on the master by inserting a diode in the TX line from each slave. And, because the master expects the incoming signal to idle HIGH you need to put a pullup resistor (about 4700 ohms should work) on the RX pin of the master. Then the slaves will be able to pull the line LOW when they transmit data.

Of course you must also arrange your programs so that only one slave talks at any one time.

The signal on the master's TX line will be received by all of the slaves so you may need to include an ID in each message if a slave is to identify which messages are intended for it.

All of this is valid whether you use HardwareSerial or SoftwareSerial on the master.

The advantage of using SoftwareSerial is that you leave HardwareSerial free for debug messages.

...R

Like Robin2 said, you can use diodes on the transmitter's pins to logically "OR" their outputs into one RX pin.

The RX pin can either be a HardwareSerial receive pin (pin 0 on an UNO), or a software serial receive pin.

The BEST software serial library is AltSoftSerial, but it can only be used on pins 8 & 9.

The next best software serial library is my NeoSWSerial. It can be used on any two pins, but it only works at baud rates 9600, 19200 or 38400.

The SoftwareSerial library is not recommended, because it disables interrupts for long periods of time. This can interfere with other parts of your sketch or with other libraries. It cannot transmit and receive at the same time.

AltSoftSerial and NeoSWSerial are available from the Arduino IDE Library Manager, under the menu Sketch -> Include Library -> Manage Libraries.

Robin2:
Of course you must also arrange your programs so that only one slave talks at any one time.

How does he do that if multiple RFIDs are randomly going to show up at the slaves?

I am guessing it is something similar to an entry system with multiple doors.

How likely is it that 2 slaves at exactly the same time will want to know if its RFID is valid or not?
I don't know the answer to that, but I am curious.

How does he do that if multiple RFIDs are randomly going to show up at the slaves?

Since the OP called them "master" and "slaves", it would be safe to assume that the master must poll the slaves to see if a new RFID has appeared. Slaves do not normally transmit whenever they want.

-dev:
Since the OP called them "master" and "slaves", it would be safe to assume that the master must poll the slaves to see if a new RFID has appeared. Slaves do not normally transmit whenever they want.

With this kind of system, I was envisioning more a push of data from the slave to the master rather than a pull by the master from the slave.

Oh well.

Back in the good old days of real computers, almost all data communications was done this way and was very effective. Google "poll select protocol". Lots of examples. I would use RS485 so they can all be on the same set of wires.

Paul

How am I supposed to wire up the diodes?

Every arduino station should have a diode on the tx and the master should have a pullup resistor in the RX?

mattia22500:
How am I supposed to wire up the diodes?

Every arduino station should have a diode on the tx and the master should have a pullup resistor in the RX?

Yes. The diode should be oriented to allow current to flow towards the TX pin and block current coming from the TX pin.

...R

I did this and it worked, I got two arduino slaves comunicating with another arduino master, but why did it actually work?

mattia22500:
but why did it actually work?

I find it hard to answer that question without knowing why you think it might not work?

(I am trying to avoid writing a 3-page essay, most of which would probably be irrelevant to your particular quest for knowledge :slight_smile: )

...R

From the changes I made I assume RX is normally HIGH and when data is incoming there is a PWM, or just a sequence of LOW and HIGH states. If I connect more TX to a single RX, there might be disturbs or anything else, so to avoid it I just open the circuit placing a diode after every TX and bring +5V to the RX with a pullup resistor. When any TX sends data there are 5V in the cathode and anode so I get a LOW logic level. This way just lets only one device sends data to the master per time.

Was it a matter of circuitry with a basic knowledge of Serial port or It's way more deep the topic? xD

mattia22500:
From the changes I made I assume RX is normally HIGH and when data is incoming there is a PWM, or just a sequence of LOW and HIGH states. If I connect more TX to a single RX, there might be disturbs or anything else, so to avoid it I just open the circuit placing a diode after every TX and bring +5V to the RX with a pullup resistor. When any TX sends data there are 5V in the cathode and anode so I get a LOW logic level. This way just lets only one device sends data to the master per time.

i think you have the right idea but the way you describe it is a bit confusing.

When a TX sends data it makes the voltage on the TX pin switch between HIGH and LOW according to whether it is sending a 1 or 0 (binary data). The diode prevents the HIGH from getting to the RX pin but that does not matter because the pullup resistor brings the RX pin HIGH. When the TX pin goes low it draws current through the resistor and that makes the RX pin go LOW. In effect, the LOW TX pin connects the RX pin to GND through the diode.

...R