ESP32 read GPIO0 button

Hello there, I was running out of pins on my ESP32 so I was thinking to reuse the GPIO0 (aka Boot button) as a user button

but I am unable to read the state of it from my sketch

// set pin numbers
const int BootButtonPin = 0;
const int UpButtonPin = 25;
const int CenterButtonPin = 26;
const int DownButtonPin = 4;
const int TuneRotaryButtonPin = 39;

// variable for storing the pushbutton status 
int BootButtonState = 0;
int UpButtonState = 0;
int CenterButtonState = 0;
int DownButtonState = 0;
int TuneRotaryButtonState = 0;

void setup() {
  Serial.begin(115200);  
  
  // initialize the pushbutton pin as an input
  pinMode(BootButtonPin, INPUT);  //Boot button
  pinMode(UpButtonPin, INPUT);    //UP
  pinMode(CenterButtonPin, INPUT); //CENTER
  pinMode(DownButtonPin, INPUT);  //DOWN
  pinMode(TuneRotaryButtonPin, INPUT); //Tune rotary potentiometer push button

  Serial.println("Power on");
}

void loop() {
  // read the state of the pushbutton value
  BootButtonState = digitalRead(BootButtonPin);;
  UpButtonState = digitalRead(UpButtonPin);;
  CenterButtonState = digitalRead(CenterButtonPin);;
  DownButtonState = digitalRead(DownButtonPin);;
  TuneRotaryButtonState = digitalRead(TuneRotaryButtonPin);;
  //Serial.println("CenterButtonStat: " + CenterButtonState);

  if (BootButtonState == HIGH) {
    Serial.print("Boot button pressed");
    Serial.println();
  }
  if (UpButtonState == LOW) {
    Serial.print("Up button pressed");
    Serial.println();
  }
  if (CenterButtonState == LOW) {
    Serial.print("Center button pressed");
    Serial.println();
  }
  if (DownButtonState == LOW) {
    Serial.print("Down button pressed");
    Serial.println();
  }
  if (TuneRotaryButtonState == LOW) {
    Serial.print("Tune rotary button pressed");
    Serial.println();
  }
}

I know GPIO0 should be active high instead of low like other buttons, but nothing I tried works properly

at first when I power on my ESP32 GPIO0 will go print, but then when I press it nothing happens

Other buttons are working totaly fine, I can also enter into flash mode with GPIO0 held at startup

Its just that I am unable to read its state from my program

Can someone help?

Are you using 30-pin or 38-pin version of ESP32?

I guess it has a pull-up on the board you are using and pushing it changes HIGH to LOW

38 pin version (according to this picture): Imgur: The magic of the Internet

also according to schematic (cannot share the whole one) there are no pullups: Imgur: The magic of the Internet

Can you please, list the devices names and sensors names you have connected with your ESP32 in order to allow me to see if some of the pins could be shared by more than one device/sensor?

ILI9341 with XPT2046 Touch Controller controller over SPI (Touch controller does not use interupt pin)
TEF6686 (over I2C)
3 regular buttons
1 rotary encoder with build in button: https://www.aliexpress.com/item/1005006046981815.html

GPIO 27 is sadly already taken by signal meter

wanted to also connect I2S pins (need 4pins for that) from TEF6686 direcly to ESP32 (so ESP32 could analyze the audio) but I am sadly running out of usable pins

Please, post the picture of your 38-pin ESP32.

Which one of the following you are using or none?


Figure-1: 30-pin ESP32


Figure-2: 38-pin ESP32

You need a pullup or set the pinMode to INPUT_PULLUP. The button should connect to GND
Then read when it goes LOW NOT HIGH

the Figure-2: 38-pin ESP32 one

that worked, thnx

You are welcome
Have a nice day!

Your devices have consumed only 17 IO lines. Your 38-pin ESP32 has 31 GPIO lines. I don't understand why you are strugging to use GPIO-0 which is a boot pin?

Not all of those pins are usable though, but yeah, there should be plenty. Actually between the 30-pin and the 38-pin, GPIO 0 is the only freely usable pin that is exposed on the 38-pin but isn't on the 30-pin. Pretty sure that it does actually have a 10K external pullup on it, although there are multiple 38-pin variants of the board around.

1 Like

@veso266 ,

Your topic was moved to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

It will help you get the best out of the forum in the future.

Thank you