PIN input without connection

Arisen
Posts: 2
Joined: Sat May 11, 2024 4:26 pm

PIN input without connection

Postby Arisen » Sat May 11, 2024 4:34 pm

I've been trying to do some basic stuff on ESP32 DevKit v1 but then run into some issues. Input pin reads values even if it's not connected to anything. It doesn't matter which pin i use (I've tried 18,15,12,27,35 and 25) it's still the same. I used this code to test it out:

Code: Select all

void setup() {
  Serial.begin(115200);
  pinMode(27,INPUT);
  pinMode(23,OUTPUT);
  pinMode(19,OUTPUT);
  digitalWrite(19,HIGH);
}

void loop() {
  Serial.println(digitalRead(27));
  delay(100);
  if (digitalRead(27)==1){
    digitalWrite(23,HIGH);
  }
  digitalWrite(23,LOW);
}
Last edited by Arisen on Sun May 12, 2024 10:03 am, edited 1 time in total.

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

Re: PIN input without connection

Postby ESP_Sprite » Sun May 12, 2024 3:23 am

Yeah, that's to be expected. An unconnected input pin is high-impedance and will pick up whatever EMC is around to determine its value.

Arisen
Posts: 2
Joined: Sat May 11, 2024 4:26 pm

Re: PIN input without connection

Postby Arisen » Sun May 12, 2024 9:21 am

How can I fix that? I've been thinking about using INPUT_PULLDOWN instead of INPUT but haven't tried that yet.
(That behaviour also happens if i connect a sensor there. It randomly gets a signal without ever recieving one)

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

Re: PIN input without connection

Postby ESP_Sprite » Mon May 13, 2024 6:44 am

You can use a pullup or pulldown, either external or internal, to pull the pin to a known level. If you connect a sensor, you need to check what output the sensor has: if it's open-drain, you'll need a pullup for it to work.

Who is online

Users browsing this forum: No registered users and 142 guests