Can one preset the state of a gpio pin before setting as an output?

Gibbedy

Well-known member
using a teensy 3.2.
I thought doing a digitalwrite before setting a pin as an input pre-set the state the pin would take when set to an output.

I could not get this to work.
Can someone confirm this is true or not?
Thanks
Gavin.
 
It won't because in the arduino world using digitalWrite() on an input pin is how you control the pullups, and Teensy does the same thing so that code stays compatible.
 
Thanks.
Is there a way to acheive this? That is, changing a pin to an output with a pre-defined state.
 
Just do
Code:
digitalWrite(pin, HIGH);
pinMode(pin, OUTPUT);
It unlikely to matter that a weak pullup got enabled a microsecond or so before the pinMode() call?
 
You could use digitalWriteFast() which skips the Arduino pullup compatibility, just writing directly to the GPIO hardware register.
 
Paul:
Thanks.

MarkT:
What i'm wanting to do is change a pin from an input to an output at runtime, but I wan't it to start at a particular state (either high or low).
I thought the code you posted would do that too, but it doesn't.
What i gather from testing is that PinMode will imediatly set your output to the last value that was set with digitalWrite while the pin was an output. That is, doing a <code>digitalWrite(pin,HIGH):</code> before the pinMode has no effect on the state the pin will be when you do a
Code:
pinMode(pin,OUTPUT);
If i'm understanding correctly paul is telling me that doing a
Code:
digitalWriteFast(pin,HIGH);
pinMode(pin,OUTPUT);
will set the pin high imediatly without the sub microsecond glitch low that i would get if i just did
Code:
pinMode(pin,OUTPUT);
digitalWrite(pin,HIGH); // or digitalWriteFast(pin,HIGH)
 
Here's a quick test to confirm calling pinMode(pin, OUTPUT) first can create a moment of the pin driven to the prior value, and indeed calling digitalWriteFast(pin, HIGH) before pinMode(pin, OUTPUT) does avoid it.

Code:
void setup() {
  pinMode(3, OUTPUT); // pin to trigger scope
}

void loop() {
  digitalWriteFast(3, HIGH);
  delayMicroseconds(1);
  digitalWriteFast(3, LOW);

  pinMode(2, INPUT);
  digitalWriteFast(2, LOW); // preconfigure LOW
  delayMicroseconds(1);
  pinMode(2, OUTPUT);  // pinMode before digitalWriteFast HIGH
  digitalWriteFast(2, HIGH);
  delayMicroseconds(1);

  pinMode(2, INPUT);
  digitalWriteFast(2, LOW); // preconfigure LOW
  delayMicroseconds(1);
  digitalWriteFast(2, HIGH);
  pinMode(2, OUTPUT);  // digitalWriteFast HIGH before pinMode
  delayMicroseconds(1);
}

To run this test, I connected a pair of 10K resistors to pin 2, one to 3.3V and the other to GND, so pin 2 will "float" at approx 1.6 volts when not driven.

Here's the resulting waveform my scope sees. Driving the pin LOW or HIGH happens quickly, but because the resistors are only 10K, returning to 1.6V takes a few hundred nanoseconds. Hopefully this makes it easy to see when the pin is being controlled by OUTPUT mode to 0V or 3.3V and when it's floating back to 1.6V due to INPUT mode.

1761890029166.png



1761890484856.png
 
Last edited:
Same test repeated with Teensy 3.2 rather than Teensy 4.1. Same result, but slightly slower due to software overhead on Teensy 3.2's slower CPU.

1761890857810.png


1761891044466.png
 
Back
Top