Can I write to an INPUT GPIO pin?

cspwcspw
Posts: 18
Joined: Mon Apr 16, 2018 5:08 pm

Can I write to an INPUT GPIO pin?

Postby cspwcspw » Thu Sep 27, 2018 4:58 pm

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

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: Can I write to an INPUT GPIO pin?

Postby urbanze » Thu Sep 27, 2018 6:46 pm

In some boards, write in an input will active pull up or pull down.

ESP_Sprite
Posts: 9757
Joined: Thu Nov 26, 2015 4:08 am

Re: Can I write to an INPUT GPIO pin?

Postby ESP_Sprite » Fri Sep 28, 2018 1:57 am

Moved to the Arduino forum.

bobtidey
Posts: 43
Joined: Mon Jun 18, 2018 2:24 pm

Re: Can I write to an INPUT GPIO pin?

Postby bobtidey » Fri Sep 28, 2018 7:34 am

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.

cspwcspw
Posts: 18
Joined: Mon Apr 16, 2018 5:08 pm

Re: Can I write to an INPUT GPIO pin?

Postby cspwcspw » Fri Sep 28, 2018 10:14 am

@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

cspwcspw
Posts: 18
Joined: Mon Apr 16, 2018 5:08 pm

Re: Can I write to an INPUT GPIO pin?

Postby cspwcspw » Fri Sep 28, 2018 10:16 am

@bobtidey - thanks, we live and learn! Makes sense that it works like this.

Peter

ESP_Sprite
Posts: 9757
Joined: Thu Nov 26, 2015 4:08 am

Re: Can I write to an INPUT GPIO pin?

Postby ESP_Sprite » Fri Sep 28, 2018 10:59 am

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.

cspwcspw
Posts: 18
Joined: Mon Apr 16, 2018 5:08 pm

Re: Can I write to an INPUT GPIO pin?

Postby cspwcspw » Tue Oct 02, 2018 3:45 am

@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

Who is online

Users browsing this forum: No registered users and 144 guests