pin set as input is it ok to write to it?

I inherited some code and found in many places they set the pin to input and then did a digital write, does this matter.

It seems you should write to the pin first then set it to input to place it in high z

for example they have the following

void enableTriPin(int sequence)
{
  int idx;

      for (idx = 0; idx < 5; idx++){
          if (idx == sequence){
               pinMode(triStatePins[idx], OUTPUT); //rev 1.71
               digitalWrite(triStatePins[idx], HIGH);
          }else{
               pinMode(triStatePins[idx], INPUT);  //rev 1.71
               digitalWrite(triStatePins[idx], LOW);
               
          }
      }
}

I think it should be

void enableTriPin(int sequence)
{
  int idx;

      for (idx = 0; idx < 5; idx++){
          if (idx == sequence){
               pinMode(triStatePins[idx], OUTPUT); //rev 1.71
               digitalWrite(triStatePins[idx], HIGH);
          }else{
               
               digitalWrite(triStatePins[idx], LOW);
               pinMode(triStatePins[idx], INPUT);  //rev 1.71
               
          }
      }
}

I’m wondering if it makes a difference using either method
Thanks

The write HIGH/LOW to an input pin enables/disables the pullup-resistor. The switcing of the two statments will pull pin LOW before making pin High-Z ..the 1.st example does not do this

When a pin is set as INPUT, calling a write on it turns on or off the internal pullup resistor.

I find it much easier to remember what is going on by using

pinMode(aPin, INPUT_PULLUP);

when I want to turn on the internal pullup resistor.

It makes a difference. But what is right depends on how the pin is used.

A digital pin of an Arduino Uno can be 4 things:

  • high inpedance input
  • input with internal pullup resistor enabled
  • output HIGH
  • output LOW

Beside those 4 things, pins can also have an analog input, or a timer output, and they all have a bit toggle for fast toggle of output pin, and so on...

The internal pullup resistor is switched on by writing a HIGH to the output (while the pin is input). The internal pullup resistor is switched off by writing a LOW.

When the pin is set to INPUT and after that a LOW is written, that means the internal pullup resistor is turned off. I don't know if that was the goal, there should be comment to explain it. No comment and no explanation, no function header that tells what it is... then it is bad code.

(while I was writing this, three others have already replied. Well, I'm gonna post it anyway)

(while I was writing this, three others have already replied. Well, I'm gonna post it anyway)

The only thing that hasn't been mentioned is the default state of the pins - INPUT with pullups turned off.

That may, or may not, matter.

The pin is also in INPUT state during a reset, so if you are driving other devices and can't live with the pins floating during a reset, your external hardware needs to address that. Could be a simple external pullup or pulldown so the pin sits at a known level during the reset and before the sketch kicks in to drive it high or low. Could be connecting reset to other devices so they restart when the arduino does. Could be something else, varies from project to project.