ESP32-S3-Zero - digitalRead() not working in PlatformIO but works in Arduino IDE

I have a ESP32-S3-Zero (FH4R2, “Waveshare” genuine) and it works perfectly well in the Arduino IDE.

For some obscure reason, it mostly works well in the PlatformIO IDE (VSCode) BUT I am having trouble with digitalRead() on a GPIO port. The digitalRead works correctly when I run the code through the Arduino IDE, but it is always returning LOW when the same code is run through the PlatformIO IDE.

Here is my platformio.ini file:

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:esp32-s3-fh4r2]
platform = espressif32
board = esp32-s3-fh4r2
framework = arduino

Here is the code that works fine in Arduino IDE but doesn’t work in PlatformIO:

#include <Arduino.h>


// constants won't change. They're used here to set pin numbers:
const int buttonPin = 7; // the number of the pushbutton pin
const int ledPin = 13;   // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup()
{
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin, PULLDOWN);
}

void loop()
{
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH)
  {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  }
  else
  {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

Just to be sure :wink: The comment says pin 2 while

your code says pin 7 …

Thanks for pointing that out. My first post…can I edit what I posted? (the example code was changed from pin 2 to pin 7 for my testing)

Normally you should be able to alter your posting just by pressing the small pencil shown underneath your post

But if if 7 is intended everything is fine

Hopefully after my edit, there is enough content to show the issue. So basic: can’t read an input with PlatformIO when it is so easy with Arduino IDE :upside_down_face:

Can you show a screenshot of the Arduino IDE → Tools menu with which it works?

What ESP32 core version is it showing in the Arduino IDE → Tools → Board Manager → esp32?

Thx, must rest. Later today :slightly_smiling_face:

Check the following things:

Use the platformio.ini

[env:esp32-s3-fh4r2]
platform = espressif32@6.8.1
board = esp32-s3-fh4r2
framework = arduino

to make sure that PlatformIO is using Arduino-ESP32 2.0.17 as well.

If the button pin is still always read as low although it should be high, please measure with a multimeter that the actual voltage on IO16 is +3.3V to double check.