Page 1 of 1

Can I write to an INPUT GPIO pin?

Posted: Thu Sep 27, 2018 4:58 pm
by cspwcspw
In some touch-screen detection software I found this code:

Code: Select all

    pinMode(_yp, INPUT);
    pinMode(_ym, INPUT);
    digitalWrite(_yp, LOW);
    digitalWrite(_ym, LOW); 
Does it mean anything to write to a pin configured for INPUT?

Is it possible to make a pin OUTPUT while ensuring its initial value. For example, I want to change a pin to OUTPUT,
but I don't want any spurious spikes. So suppose I have

Code: Select all

    pinMode(pin, INPUT_PULLUP);
    ... 
    pinMode(pin, OUTPUT);
    // How can I avoid an unintended low or undefined value happening here?  Can I set the pin value before
    // I make it OUTPUT?
    digitalWrite(pin, HIGH);
  

Thanks
Peter

Re: Can I write to an INPUT GPIO pin?

Posted: Thu Sep 27, 2018 6:46 pm
by urbanze
In some boards, write in an input will active pull up or pull down.

Re: Can I write to an INPUT GPIO pin?

Posted: Fri Sep 28, 2018 1:57 am
by ESP_Sprite
Moved to the Arduino forum.

Re: Can I write to an INPUT GPIO pin?

Posted: Fri Sep 28, 2018 7:34 am
by bobtidey
It is perfectly legitimate and quite normal to write data to a GPIO before setting its mode.

The value will be held locally in the GPIO data register but will have no effect if the mode is input. Setting the mode to output will then immediately reflect the state of the data on the pin.

So it is common to write the value before setting the mode to avoid a glitch. The value written would depend on whether a pull up or pull down is used to determine the state of the pin whilst it is still in the input mode.

Re: Can I write to an INPUT GPIO pin?

Posted: Fri Sep 28, 2018 10:14 am
by cspwcspw
@ESP_sprite. Hi - I'm not sure why my topic was moved to Arduino forum. Did I miss something about where this should have gone in the first place?

I'm specifically wanting to know what happens on the ESP32 where I am running this code ...

Thanks
Peter

Re: Can I write to an INPUT GPIO pin?

Posted: Fri Sep 28, 2018 10:16 am
by cspwcspw
@bobtidey - thanks, we live and learn! Makes sense that it works like this.

Peter

Re: Can I write to an INPUT GPIO pin?

Posted: Fri Sep 28, 2018 10:59 am
by ESP_Sprite
cspwcspw wrote:@ESP_sprite. Hi - I'm not sure why my topic was moved to Arduino forum. Did I miss something about where this should have gone in the first place?
You're using digitalWrite, which is an Arduino-specific statement.

Re: Can I write to an INPUT GPIO pin?

Posted: Tue Oct 02, 2018 3:45 am
by cspwcspw
@ESP_sprite - thanks, was not aware that digitalWrite was Arduino specific.

Is there a preferred way to set individual pins high or low on the ESP32, other than the port-at-a-time w1ts / w1tc manipulation?

Thanks
Peter