Voltage when changing from input to output

amirgeva

New member
I'm connecting GPIO pins to a bus, where some of the time they're used as inputs, and some of the time they are output.
Is there a way to know what would be the voltage between the pinMode(pin,OUTPUT) and the digitalWrite(pin,value) (even if it is a fraction of a uS)?
I wouldn't want to get an involuntary pulse going the wrong way.
 
Are you asking how to check if something else is driving the line before you switch the pinmode to OUTPUT?
 
Assuming there is nothing external driving the line, I want to know if there is something I can do in order to make sure I can control what it is. Lets assume, for example, that I'm using an input pin with an internal pull up to connect to a device that is activated by the line going low. When my pin is in input mode, some other part of the system is controlling the device, and I can read the pin and monitor that.
When I want to control the device, I change the pin to output and when all other settings are ready, I set the pin to low.
I want to know it will not go low on its own when I change to output, because of some internal setting I'm not aware of.

Of course, if it's not possible or something like that, I can always bypass the problem in some way like setting this output pin as the last action I do before I need to drive it low, but I would like to know if I can avoid such considerations to make my code simpler.
 
Are you saying you want an open-collector pin? Then toggle pinMode, not DigitalWrite... This is how you drive open-collector, i.e. you never actively drive HIGH, but leave that entirely to the pull-up resistor.
 
I'm not sure I understand you. I don't want to drive it HIGH, just LOW, but I want to make sure I control when it goes LOW. Perhaps I just don't fully understand how an output pins works. When I use pinMode for output, does it start driving an output immediately, or does it wait for a digitalWrite. If it doesn't wait, what controls the value of the pin before the write.
 
in setup call digitalWrite(pin, LOW) (although this is the default),
to actually set the pin low, call. pinMode(pin, OUTPUT), to let it go high call pinMode(pin, INPUT). This is how open-collector (aka open-drain) works on a microcontroller.

When in mode is INPUT, it floats. When mode is OUTPUT it is determined by the latest call to digitalWrite. No waiting is involved.
 
MarkT is correct and describes how to achive the goal. However, if your circuit or the hardware that connects with your circuit does not have a built-in pull-up resistor, you can use pin mode INPUT_PULLUP when the pin is an input to reduce the likelihood of getting into a halfway-active metastable state.

I had hoped that the Arduino pinMode() has an option that I know is supported by the NXP hardware in Teensy: Pins can be configured as "open drain" outputs, with or without pullup resistor. It is always possible to read the state of the pin. So if external hardware is pulling it down, your code would read "low", and if external hardware is not pulling it down your code woult read "high". Performing a digitalWrite(pin, LOW) drives the pin low, and digitalWrite(pin HIGH) lets the pin float. That mode ensures that your Teensy won't try to waste power to drive a pin high at the same time the external device may be trying to driving the pin low. (That would be a bug but bugs happen...)

I use Teensy in lab test equipment and use the NXP microcontrollers for production equipment, and the hardware works exactly as expected. Our code sets the pin mode hardware directly in order to use this capability.

Lacking that choice in the Arduino world, you may want to consider switching modes between INPUT_PULLUP and OUTPUT almost as MarkT suggests.
 
Some of the info in the above posts might need to be clarified a bit.
Using digitalWrite() while a pin is not in OUTPUT mode will not set the future output level. This is because historically, Arduino did not have INPUT_PULLUP and INPUT_PULLDOWN modes; instead you were expected to use just plain INPUT mode and digitalWrite() to activate the built-in pull up/down modes. Teensy copies this behaviour so that existing source code written for Arduino performs the same way (visible here).

If you really want to pre-configure a pin's output level before switching it to output mode, use digitalWriteFast().

Teensy does, in fact, support OUTPUT_OPENDRAIN as a parameter to pinMode to properly support the NXP hardware's open drain mode.
 
Last edited:
Back
Top