Change input pin to output pin or (vice versa ) in loop

I am using 2 Arduino nano boards and want to implement full duplex data transfer between them...just like I2C but by using GPIO pins. Both devices can send or receive data. See diagram. Let the initial I/O configuration be clk - output, tx- output, rx - output.

Scenario- when device 1 wants to send data to device 2 on tx pin, then the tx pin of device 2 should be configured to input.

Now during program execution in the loop, if I change the configuration of tx pin of device 2 to input, it does not receive data from device 1.

How to correct this?

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.

Ops diag.

Why don't you use I2C between them.
You will also have to have the two Nano ground pins connected together.
With three data lines, why do you need to change ports from input to output.

One line can be Nano1/Tx to Nano2/Rx and another Nano2/Tx to Nano1/Rx.

If you aim to change i/o ports from input to output and back then you will have half duplex, not full duplex.
As the controller can only do one thing at a time, you will only have half duplex but with full duplex transmission line.
That is the controller cannot Tx and Rx at the same time.

Can you explain and not on your diagram what your signal flow will be.

Thanks.. Tom... :slight_smile:
PS I2C is not full duplex.

It might be useful to know why you want to do this ? Could not software serial do the job for you ?

Thanks for ur quick response.

I can use the existing protocols. Just wanted to get a feel of doing something different. And there can be more than 2 devices connected...

So....for a moment ...if we don't consider the optimality of this method...my problem is -
I have a loop running and want to change the port configuration from input to output or vice versa.
Is it possible?

Thanks

void loop(){
pinMode (D2, INPUT_PULLUP);

delay(5000);

pinMode (D2, OUTPUT);

delay (5000);
]

Sure you can use pinMode like that to change from input to output to input ...

Put 470 Ohm resistors between the pins of the two devices - just in case you set both to OUTPUT. :roll_eyes:

If making use of internal pull up resistors, do I retain the external pull up resistors too?

Connecting 3-4 devices( 2 nano boards d 1 lcd and 1 memory)

seemamahajan:
If making use of internal pull up resistors, do I retain the external pull up resistors too?

If you need to, but it begs the question of why you feel the need for pull-ups?

The pull-ups are a part of an "open-drain" (formerly "open-collector") bus where no device actually drives the bus line HIGH, only LOW and the pull-ups are necessary to generate the HIGH. I2C - and the hardware used to implement it - does this and you emulate it by writing the port LOW and only swapping between INPUT and OUTPUT. With the port written LOW, INPUT_PULLUP is always disabled; it is messy to attempt to swap between OUTPUT and LOW, and INPUT and HIGH to enable the pullup.

If you are going to define one device as always INPUT and the other as OUTPUT, writing alternately HIGH and LOW, then you will not need the pull-ups at all. The cleverness of the open-drain bus such as I2C, is to use the same bus line for both directions. You can have a single-wire bidirectional communication if it is clocked by a timer, it is easier to use two bidirectional lines for asynchronous clocking.

Thanks...I am able to make some progress...will work on it..