The ESP32 how use internal INPUT_PULLDOWN

esp32

The ESP32 devkit recognizes inputs 1,3 and 5 as high state when they shouldn't

the code

void setup()
{
  Serial.begin(115200);
  pinMode(23, INPUT_PULLDOWN);
  pinMode(22, INPUT_PULLDOWN);
  pinMode(1, INPUT_PULLDOWN);
  pinMode(3, INPUT_PULLDOWN);
  pinMode(21, INPUT_PULLDOWN);
  pinMode(19, INPUT_PULLDOWN);
  pinMode(18, INPUT_PULLDOWN);
  pinMode(5, INPUT_PULLDOWN);
  pinMode(17, INPUT_PULLDOWN);
  pinMode(16, INPUT_PULLDOWN);
  pinMode(4, INPUT_PULLDOWN);
  pinMode(2, INPUT_PULLDOWN);
  pinMode(15, INPUT_PULLDOWN);
}

void loop()
{
  int puerto1 = digitalRead(23);
  if (puerto1 == HIGH)
  {
    Serial.println("Puerto 23 pulsado");
  }

  int puerto2 = digitalRead(22);
  if (puerto2 == HIGH)
  {
    Serial.println("Puerto 22 pulsado");
  }

  int puerto3 = digitalRead(1);
  if (puerto3 == HIGH)
  {
    Serial.println("Puerto 1 pulsado");
  }

  int puerto4 = digitalRead(3);
  if (puerto4 == HIGH)
  {
    Serial.println("Puerto 3 pulsado");
  }

  int puerto5 = digitalRead(21);
  if (puerto5 == HIGH)
  {
    Serial.println("Puerto 21 pulsado");
  }

  int puerto6 = digitalRead(19);
  if (puerto6 == HIGH)
  {
    Serial.println("Puerto 19 pulsado");
  }

  int puerto7 = digitalRead(18);
  if (puerto7 == HIGH)
  {
    Serial.println("Puerto 18 pulsado");
  }

  int puerto8 = digitalRead(5);
  if (puerto8 == HIGH)
  {
    Serial.println("Puerto 5 pulsado");
  }

  int puerto9 = digitalRead(17);
  if (puerto9 == HIGH)
  {
    Serial.println("Puerto 17 pulsado");
  }

  int puerto10 = digitalRead(16);
  if (puerto10 == HIGH)
  {
    Serial.println("Puerto 16 pulsado");
  }

  int puerto11 = digitalRead(4);
  if (puerto11 == HIGH)
  {
    Serial.println("Puerto 4 pulsado");
  }

  int puerto12 = digitalRead(2);
  if (puerto12 == HIGH)
  {
    Serial.println("Puerto 2 pulsado");
  }

  int puerto13 = digitalRead(15);
  if (puerto13 == HIGH)
  {
    Serial.println("Puerto 15 pulsado");
  }

  delay(100);
}

I have nothing connected
enter image description here

status shows high when it should show low

enter image description here

Internal Pull-down don't work on GPIO1, GPIO3 and GPIO5
I need the pin state to show in low

Best Answer

Many ESP boards have built-in resistors to pull certain IO pins high/low, as that's required for boot configuration or suchlike. The ESP micros are indeed quite annoying in this, and you have to keep this in mind when you actually want to use your GPIOs as anything; at least compared to most other microcontrollers which actually let you use your GPIOs without too many strings attached :)

Here's a website I found, showing which IO pins can be safely used for what purpose, as many aren't as general purpose as you'd wish they were. It does list GPIO 5 as one with a pull-up, but I'm not terribly sure about pins 1 and 3 - those seem to connect to the USB-UART chippo?

Do check the schematic of your development board, I'm sure you can find it somewhere :D


Just a small note about the test software you've written: Wouldn't it be slightly more readable/easier to make fewer mistakes in if you used an array of pin numbers there, and just iterated through it to test their status? Copy-pasted code is generally a pain to maintain. "Don't repeat yourself," and all that mantra.

Related Topic