-2

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

Diego
  • 17
  • 1
  • 9
  • 7
    Floating inputs are very susceptible to electrical noise, and are not "LOW" by default. Please see https://electronics.stackexchange.com/q/481378/9612 and https://electronics.stackexchange.com/q/437647/9612 for explanation and guidance. – nanofarad May 06 '22 at 02:24
  • those links don't answer my question – Diego May 06 '22 at 14:33
  • 2
    They almost certainly do. Your photo of the breadboard clearly shows floating CMOS inputs, and your code clearly shows no attempt to enable a pullup or pulldown resistor. Add pullup/pulldown resistors. If you still think they don't, [edit] your post with additional info to explain why the issue seems different than floating CMOS inputs. – nanofarad May 06 '22 at 15:19
  • The input pins are still there whether you connect something to them or not. When the processor reads the pin it must read either a High or a Low, whether you have connected something to the pin or not. An unconnected CMOS input will float randomly between High and Low. – Peter Bennett May 06 '22 at 15:35
  • I can't connect a pull-up resistor because I want to connect a sensor – Diego May 06 '22 at 23:40
  • I want to connect many sensors, I need first the pin state to show low – Diego May 07 '22 at 00:04
  • 2
    While you don't have the sensor connected, you need something to pull it down. If it's a digital sensor with a push-pull interface then having a reasonable pull-down or pull-up should cause it no significant problem. Also, from https://www.esp32.com/viewtopic.php?f=18&t=11349: "Note that not all GPIOs on an ESP32 have pullup and pulldown resistors." -- you may need to use physical resistors, as described in the existing link. – nanofarad May 07 '22 at 01:49
  • I use pinMode(pin,INPUT_PULLDOWN) and it doesn't work correctly – Diego May 07 '22 at 22:15
  • 3
    @Diego The ESP8266 doesn't have a pull-down resistor option on most of its pins (except pin 16) https://www.esp8266.com/viewtopic.php?t=7255 , so INPUT_PULLDOWN will not actually do anything – BeB00 May 08 '22 at 03:10
  • 1
    @BeB00, the board on the picture is an ESP32. the esp8266 core doesn't have INPUT_PULLDOWN, it only has INPUT_PULLDOWN_16 – Juraj May 08 '22 at 05:35
  • 1
    _”I can't connect a pull-up resistor because I want to connect a sensor”_ Sure you can. They are not mutually exclusive. – winny May 08 '22 at 10:07

3 Answers3

1

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.

Richard the Spacecat
  • 1,689
  • 15
  • 23
0

If you attach 10k resistors from each pin that is giving you trouble to ground your spurious high signals will go away.

Also there’s a good chance that if you attach the sensors you intend to use to these inputs the spurious readings will go away.

This problem is caused by the high sensitivity of CMOS inputs - they will pick up stray fields and charge like you wouldn’t believe.

Bryan
  • 2,156
  • 1
  • 6
  • 11
0

As Juraj states in the comments this is an ESP32, not ESP8266.

According to ORParga in this forum you have to configure the GPIO pin you want to use with gpio_config().

This function accepts as the only parameter a gpio_config_t structure with five members that must be completely filled.

If you wish to use pin 15 as an input pin with internal pulldown you could try this:

void setup(){
   Serial.begin(9600);
   io_conf.intr_type = GPIO_PIN_INTR_DISABLE;//disable interrupt
   io_conf.mode = GPIO_MODE_INPUT;//set as inputmode
   io_conf.pin_bit_mask = (1ULL<<15);//bit mask of the pins that you want to set,e.g.GPIO15
   io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;//enable pull-down mode
   io_conf.pull_up_en = GPIO_PULLUP_DISABLE;//disable pull-up mode
   esp_err_t error=gpio_config(&io_conf);//configure GPIO with the given settings

   if(error!=ESP_OK){
    Serial.print("error configuring inputs\n");
   }
}

void loop(){
   int a = digitalRead(15);
   if(a == HIGH) Serial.println("PIN 15 IS HIGH");
}

I don't possess an ESP32 so I can't test this code. Please implement this code and investigate the results - I'm interested.

Carl
  • 3,646
  • 1
  • 14
  • 33
  • when compiling it shows this error exit status 1 'io_conf' was not declared in this scope – Diego May 08 '22 at 22:14
  • Arduino:1.8.15 (Windows 10), Tarjeta:"ESP32 Dev Module, Disabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 921600, None" C:\Users\Diego\AppData\Local\Temp\arduino_modified_sketch_939246\sketch_may08b.ino: In function 'void setup()': sketch_may08b:3:4: error: 'io_conf' was not declared in this scope io_conf.intr_type = GPIO_PIN_INTR_DISABLE;//disable interrupt ^ exit status 1 'io_conf' was not declared in this scope – Diego May 08 '22 at 22:27
  • replacing this compiles me gpio_config_t io_conf; io_conf.intr_type = (gpio_int_type_t)GPIO_PIN_INTR_DISABLE; this link https://esp32.com/viewtopic.php?t=15795 – Diego May 08 '22 at 22:52
  • Internal Pull-down don't work on GPIO1, GPIO3 and GPIO5 – Diego May 08 '22 at 23:46