I'm searching for a solution to let 4 Arduino's communicate in a star topology.
The cable is AWG 29 or 24, not twisted and not shielded.
The lengths from the middle are different as you can see in the attachment.
I need 2 wires for power - so 2 wires are left.
There is no speed for transfer. Just if a button on one Arduino is pressed -> to something on other Arduinos.
Does it have to be connected in the middle like you showed?
Using unshielded and untwisted wire is asking for trouble. Good for power, not good for data. In fact, thinking about that, it may be better to use NRF24 radio modules on each Arduino to talk to each other and only use the wires for power.
Even though you have shown the Arduinos to be equal peers, it's usually better to designate one as the master which can then call all the others one at a time and manage the communication network from a single point.
I'd go with RS485. Everyone listens all the time, if no one is talking one can start a transfer with an address and some data, if the others see their address they can do something with the data.
Over those distances, RS485 would work on untwisted cable. It will be more receptive to unwanted interference but so long as your protocol includes a checksum or CRC it should be OK.
MODBUS will work on RS485. One of the Arduinos must be a master and it addresses the other slaves as required.
Over the distances you have specified, it doesn't matter if it is twisted or not, so long as you are able to detect single-bit errors.
I would make one the master. It transmits on the green wires. All the slaves listen to the green. The master listens to the blue wires.
The tricky part is that only one slave may control the blue wires. Otherwise one trying to transmit a 1 will be dragged down by the other two who are idle at 0. You need an RS232 transmitter which has an 'enable' input. Each slave lets the blue wire float unless it is actively transmitting.
Coding for this is easy on a Teensy 3.x, as its hardware can use any digital output as the enable pin. For other Arduinos you need a little more code to identify when Serial transmission has finished.
Power for the MAX3223 can be either 3.3V or 5V. The Arduino hooks up to T1In, R1Out and TX_En. T1out and R1in go to your wiring system.
The important code to enable and disable the transmitter on the slave is...
void modbusBegin() {
pinMode(TX_EN, OUTPUT);
digitalWrite(TX_EN, LOW); //LOW disables the output
//Note that output will always be disabled when there's
//no valid input voltage on the receive line (-5V)
EXCITER_Serial.begin(EXCITER_BAUD_RATE);
ModbusHighwater = EXCITER_Serial.availableForWrite();
}
inline void enableTX() {
if (!TransmittingModbus) {
TransmittingModbus = true;
digitalWrite(TX_EN, HIGH);
//Not clear from datasheet how long the MAX3223 takes to start up
//Could be 200ns or 240us
//19200 works without the delay but 57600 and above needs it
delayMicroseconds(240);
}
//If we were to just transmit one char, the availableForWrite
//would not report it. So we need to assume that the caller
//of this function is actually going to transmit at least
//one char after enabling the TX.
LastCharXmitBegin = millis();
}
inline void disableTX() {
if (TransmittingModbus) {
if (EXCITER_Serial.availableForWrite() < ModbusHighwater) {
//there's chars in the queue, record this time
LastCharXmitBegin = millis();
} else {
//no chars - wait until the buffer has been
//empty for some time before cutting the TX_EN
if (millis() - LastCharXmitBegin > CharXmitTime) {
digitalWrite(TX_EN, LOW);
TransmittingModbus = false;
}
}
}
}
In this project, the slave units are called "exciters".
Every time the slave starts to transmit a Modbus response, it calls enableTX() first. It regularly calls disableTX() every time through loop().
I took the capacitor values from the MAX3223 datasheet for 3.0 to 5.5 volts.
This one is in the basement and mainly the timer switch for the stairway lights.
So on the front door will be the master and the bells in the apartments are slaves.
S1 is just for the transition phase until i have build the rest.