I have a very simple sketch being uploaded to an ESP32-S3, like this:
void setup() {
pinMode(GPIO_NUM_20, OUTPUT);
pinMode(GPIO_NUM_21, OUTPUT);
}
void loop() {
delay(500);
}
These two GPIO pins are connected to the input side of two typical MOSFET relays controlling much larger outputs. Right now, I am using two LEDs (one on each relay's output side) to indicate when the normally-open relays are being activated.
What I have noticed is that GPIO_NUM_21
will be brought to HIGH upon boot-up with that simple sketch flashed. That is, for roughly 30ms, that pin will be HIGH despite no digitalWrite()
or other manipulation of the pin. This results in the downstream relay being activated for that 40ms, which is undesirable in the larger goal of this board.
This behavior also happens when waking-up from sleep, using a simple sleep timer like this:
rtc_gpio_pullup_en(GPIO_NUM_18);
rtc_gpio_pulldown_dis(GPIO_NUM_18);
esp_sleep_enable_ext0_wakeup(GPIO_NUM_18, 0);
After running esp_deep_sleep_start()
, then bringing the GPIO_NUM_18
(a simple button) to LOW, the board wakes up as expected, and again, GPIO_NUM_21
goes to HIGH for ~30ms despite no digitalWrite()
or similar being executed, and the setup()
block not being executed.
Looking at the Espressif pin diagram for the ESP32-S3, I see that GPIO20 and GPIO21 are not "identical" with regards to their features:
Is there a way to prevent this behavior on that pin, or will I need to choose another GPIO pin for my purposes (not activating the relay upon boot-up/wake)?
ESP32 GPIO pins are normally floating (no definitive state) at power up and when sleeping unless you specifically program them. The setup time between power-on and your code getting executed appears to be the 30ms you've measured.
The simple solution that comes to mind is to place external pull-up or pull-down resistors on these GPIO lines so that you control their default state. A value between 10K and 100K will work well for your application.