Changing the pinMode of single GPIO from OUTPUT to INPUT and back inside loop?

Is it possible to change the pinMode initialization of a single GPIO multiple times?

#define PREDICT GPIO_NUM_25

int input = 0;
int read = 0;

void setup() {

  Serial.begin(115200);	


  
}
void loop() {

  int buttonState = 0;
  
  pinMode(PREDICT, OUTPUT);    
  digitalWrite(PREDICT, HIGH);
  Serial.printf("Interrupt send to 1st Arduino\n");
  digitalWrite(PREDICT, LOW);
  delay(1000);
  digitalWrite(PREDICT, LOW);
  pinMode(PREDICT, INPUT);    
  read= digitalRead(PREDICT);
  Serial.printf("Prediction: %d received from 1st Arduino\n", readSVM);

  delay(10000);

}

Sending the HIGH to 1st Arduino is working but when 1st Arduino sends HIGH it keeps on showing LOW only.

I am only allowed to use one GPIO to transfer and receive 0/1.

rahullabs:
I am only allowed to use one GPIO to transfer and receive 0/1.

Schoolwork?

Nop. Personal Project.
In nutshell.

  1. Receive HIGH from PLC through GPIO 25 (eg)
  2. Perform some calculation in the Arduino
  3. Use same GPIO 25 to send HIGH/LOW depending upon the calculation result to PLC

PLC Arduino
High Signal ------------------------------->Calculate
<-------------------------------Send HIGH/LOW

(Single Wire)

In that case you need an open collector with a pullup. And, you need to understand what that is before you do any more damage to your Arduino.

I'm curious. How will the PLC know to change its I/O from output to input - if that's even possible.

I assume, because it's the only way I can imagine it could work, the PLC is using a weak pull-up and strong pull-down, in other words an open collector and a pull-up resistor. The device it is communicating with should be the same, although a second pull-up might not be needed. This is the way many signalling methods use, such as i2c and 1-wire.

So to answer the OP's question, yes, you can change from INPUT or INPUT_PULLUP to OUTPUT in loop as needed. BUT take care never to set the Arduino pin to OUTPUT+HIGH because that could cause a short-circuit and a damaging large current to flow if the PLC is sending LOW.

I'm not sure about this, but there could be a danger that changing directly from INPUT_PULLUP to OUTPUT could result in sending HIGH and causing that short circuit. So maybe safer to avoid INPUT_PULLUP. Perhaps another forum member can confirm or deny that.

As a safety measure, you could put a current limiting resistor like 330R between the PLC output and the Arduino pin. Then if something goes wrong, it might not work but at least there will not be a short circut.