Page 1 of 1

PIN input without connection

Posted: Sat May 11, 2024 4:34 pm
by Arisen
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);
}

Re: PIN input without connection

Posted: Sun May 12, 2024 3:23 am
by ESP_Sprite
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.

Re: PIN input without connection

Posted: Sun May 12, 2024 9:21 am
by Arisen
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)

Re: PIN input without connection

Posted: Mon May 13, 2024 6:44 am
by ESP_Sprite
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.