Communication between arduinos over 4 interchangeable pins

Here is what I need to do:
I need to connect 2 arduinos using 4 wires. Easy, right? The tricky part is that they need to communicate despite the way these 4 wires are connected. Let me explain:
4 I/O pins on one arduino will be connected to 4 I/O pins on the other arduino, but the communication MUST be possible even if we interchange the connections.
Is this possible? I could add a 5th wire for common ground but it would actually be best if one of the wires could act as ground (even though it is not known which one). Does it make sense? Do you guys think it can be done?

Do you have a "Master-Slave" setup ?, Half or Full-duplex ?, what is you desired throughput ?

It all depends on the start condition.
If one Arduino is the master and always switched on as the first one, it is not so hard.
But even if both can be master and switched on and off at any moment, it would still be possible (I think).

Suppose you have RX and TX with SoftwareSerial, http://arduino.cc/en/Reference/SoftwareSerial
The TX can be recognized if some kind of "synchronisation byte" is sent for example 10 times a second.
The RX could be default high (with a weak pull-up) to distinguish it from the ground.
You would need only three wires. After recognition, the SoftwareSerial can set a serial port on those pins.
The ground could be an output pin set to LOW to be some kind of simulated ground. It would need some protection for ground currents, but I think it is possible.

With I2C, I can't figure it out, how to do it.

With SPI should be possible, but harder to implement than the SoftwareSerial.

OneWire protocol. This would be possible. I don't know a lot of it, but it's a single wire and data can travel in both directions.

In the end, I would prefer a real ground (not a simulated ground with a pin) to both Arduinos. And 2 wires for TX and RX.

granmicky:
Here is what I need to do:
I need to connect 2 arduinos using 4 wires. Easy, right? The tricky part is that they need to communicate despite the way these 4 wires are connected. Let me explain:
4 I/O pins on one arduino will be connected to 4 I/O pins on the other arduino, but the communication MUST be possible even if we interchange the connections.
Is this possible? I could add a 5th wire for common ground but it would actually be best if one of the wires could act as ground (even though it is not known which one). Does it make sense? Do you guys think it can be done?

This sounds basically unworkable. The two arduinos must know for sure what pins to use as output pins and which to use as input pins. If they are randomly mixed up then you are likely to have two output pins wired to together effectively causing a short circuit and pin damage.

Can you tell us what you are trying to accomplish rather then how you think it might be accomplished?

Lefty

but the communication MUST be possible even if we interchange the connections.

Does that mean any pin can be connected to any pin? If that's true, is it a must that both Arduinos run the same sketch, so they are interchangeable too?

retrolefty:
If they are randomly mixed up then you are likely to have two output pins wired to together effectively causing a short circuit and pin damage.

I think it ought to be possible to design a discovery procedure to negotiate the identity of the send/receive/ground pins, but this would still leave the possibility that both sides tried to enable the same pin as an output at the same time and drive it to different states during the discovery. Perhaps that could be dealt with by using current-limiting resistors? During normal operation the receiving side would be high impedance so that shouldn't affect the signal drastically.

granmicky:
...the communication MUST be possible even if we interchange the connections.

Why? What problem are you trying to solve?

Do you guys think it can be done?

Probably. But I doubt it is worth the effort.

I think 2 wires is all that is needed - Gnd on both sides, and then any 1 pin to any 1 pin.
Have both sides start as all pins as Inputs, listening for a low on any pin, pullups enabled.
After some arbitrary time (each can have random seeded millis() that it waits for) each side uses pinMode (pinX, OUTPUT) and asserts a pin low for some brief time ,then goes back to input mode. If the other side sees it, it asserts low back and then waits for a 'fast' burst of high/lows for a 1, or a 'slow' burst of high/lows for a zero.
Repeat that back for the receiver to show the sender sync has been estalished. The fast/slow bursts become like a slow software serial.
Each side could use PORTx commands to quickly look at its 3 ports for a non-0xFF (or masked for the bits to be used) to see of a low was asserted by the other side. The Low would have to be asserted long enough for the 3 reads to be able to catch it, and after seeing a low, the initial receiver could assert low for a longer time for the initial transmitter to catch it.

Or something like that ...

First some questions:

Why 4 pins, exactly?
Will there be a "master" Arduino?
Will there only ever be two connected together?

As others have pointed out, it would be dangerous to connect two outputs together. But with some bus arbritation, it should be possible.

If this is beyond an academic, "can it be done" activity and simply must operate like this, I think we are going to need more details about what is going on exactly.

retrolefty:

granmicky:
Here is what I need to do:
I need to connect 2 arduinos using 4 wires. Easy, right? The tricky part is that they need to communicate despite the way these 4 wires are connected. Let me explain:
4 I/O pins on one arduino will be connected to 4 I/O pins on the other arduino, but the communication MUST be possible even if we interchange the connections.
Is this possible? I could add a 5th wire for common ground but it would actually be best if one of the wires could act as ground (even though it is not known which one). Does it make sense? Do you guys think it can be done?

This sounds basically unworkable. The two arduinos must know for sure what pins to use as output pins and which to use as input pins. If they are randomly mixed up then you are likely to have two output pins wired to together effectively causing a short circuit and pin damage.

Can you tell us what you are trying to accomplish rather then how you think it might be accomplished?

Lefty

Ok, I am trying to make a puzzle with cubic pieces. You know, the type with different images on each side. What I want to accomplish is to have one arduino (a microcontroller really) inside each piece (I'm thinking 6 or 9 pieces) and establish a connection with the adjacent pieces thru a conductive joint on each corner of each piece. Each cube can inform its adjacent cubes of its orientation so that when the information flow is complete each cube know the pictures that are showing (one on each side). Does it make sense to you guys?
That is why I need the communication wires to be interchangeable, because the relative position of each cube can change by rotation (i.e. if corners 1, 2, 3 & 4 are connected to corners 1, 2, 3 & 4 and the cube is rotated, then the connection would be 1-2, 2-3, 3-4 & 4-1).
Obviously, the sketch running on each cube is identical except for the information about themselves, that is, which images are showing on each side. Also, I onlu spoke of connecting four pins but I would need to implement it for all sides of the cube.

I like the idea of making some kind of an initial poll to assign pins and then using softserial. I haven't really worked on the consequences of an eventual shortcircuit so I really don't know how to protect the circuit from over currents.

Thank you very much.

Thank you, that makes sense.

With the cubes, I would do it in an other way.
I would use two diodes per pin (642=48 diodes per cube, perhaps only 12 is enough) to power the cubes, and have the datastream on top of the power. I would use a high frequency (100kHz or more) and multi-master-slave configuration. Like the network for the 433MHz (I forgot the name). You would only need two wires with that.
With the high frequency, it is easier to seperate it from the power lines.

You could also have a capacitance coupling. Make the inside of the walls of the cube with copper, and perhaps it is possible to communicate with the next cube.

I'm curious about other ideas, now that we know what it is for.

Trying to visualize this. When you say cubic puzzle, I am picturing a rubik's cube, but you mention only 6 to 9 pieces, which is not enough. So, then I am thinking sliding puzzle, but that doesn't use cubes. So, I did a search and found photo puzzles, but couldn't find any example that would work with 6 or 9 pieces.

Do you have a link to a simple example?

The reason I am asking is because math is likely to solve your problem easier than trying to figure out how to communicate over different wires. When I was assuming rubik's cube for example, I was envisioning a way to simply complete a circuit based on position since a single piece is actually limited in the combinations it can be in.

There is likely a much simpler way using truth tables, if I could visualize this properly.

Retroplayer:
Trying to visualize this. When you say cubic puzzle, I am picturing a rubik's cube, but you mention only 6 to 9 pieces, which is not enough. So, then I am thinking sliding puzzle, but that doesn't use cubes. So, I did a search and found photo puzzles, but couldn't find any example that would work with 6 or 9 pieces.

Do you have a link to a simple example?

The reason I am asking is because math is likely to solve your problem easier than trying to figure out how to communicate over different wires. When I was assuming rubik's cube for example, I was envisioning a way to simply complete a circuit based on position since a single piece is actually limited in the combinations it can be in.

There is likely a much simpler way using truth tables, if I could visualize this properly.

The puzzle should be something like this. http://www.finewoodentoys.com/files/3373883/uploaded/SG_farm_cube_puzzle_1.jpg
There's quite a lot of math worked out already in order to limit the amount of information being transfered and to make it easy to process by the arduino. But it always boils down to a cube transmitting thru each corner the id of the corner, the id of the cube itself and the info of the cubes connected to it and reading from each corner such information. Hope to make the project clearer. Thank you all!

I would imagine rather than using multiple wires, a different approach might be to have a 4 wire connection to use i2c communication in a standardized location, such as the center of each face of the cube. You would assign each cube a separate i2c address. For each connection point, each cube would send out a query to the other cubes asking who its neighbors are, and the cube would then record who is each of its neighbors, and then when queried, it would respond, listing each of its neighbors. The master would then build up a list in real time of the geometry.

In case it wasn't clear, i2c is a 4 wire connection (ground, power, 2 data wires) where each device on the bus has an address, and you can transmit bytes, words, etc.

Does that mean that each end point will be in common with up to four devices, all trying independently to discover and identify each other? That strikes me as a very difficult approach to make work. I'd say you have a far greater chance of success if you put contacts on each face of the cube. I'd look for a solution which used asymmetrical contact positions to make it possible to know which contact was which, so that the 'discovery' part only had to establish a handshake between the two devices (that could be achieved by a collision detection/random backoff algorithm).

This may give you some ideas... Dice Game