I am working on a university project and using an ESP32 DevKitC V4. While trying to interface with a quadrature rotary encoder, I realized that all of my input pins were floating regardless of how I assigned pullup/pulldown resistors. The below code increments the toggleCounter every time the floating pin toggles, and it's reliably printing very high numbers when the pin is exposed to any kind of electrical disturbance. I've spent a decent amount of time googling this problem and the closest issue I've found is people connecting to pins with no internal PU/PD. Even stranger is that I swear it has played nice before, but I can't actually prove that. I'm using Arduin IDE to upload code, selecting the DOIT ESP32 DEVKIT V1 device. Any help is appreciated!
#define A 4
//Vars
int counter = 0;
int lastCountTime = 0;
bool currAState = 0;
bool prevAState = 0;
int toggleCounter = 0;
void setup(void)
{
printf("Starting...\r\n"); // Leaves Serial at default config (115200)
pinMode(A, INPUT_PULLUP); // or INPUT_PULLDOWN, doesnt change anything
}
void loop() {
if (millis() - lastCountTime >= 1000) {
printf("Counter: %d, Toggles: %d\r\n", counter, toggleCounter);
counter++;
toggleCounter = 0;
lastCountTime = millis();
}
currAState = digitalRead(A);
if (currAState != prevAState) {
toggleCounter++;
prevAState = currAState;
}
}