ESP32 toggle LEDs using assigned buttons

Hi,

I have an ESP32 with 4 buttons control an assigned LED to each but it looks like only 2 LEDs are blinking while the rest cannot be toggled. Is there an issue with my code that I could have overlooked?

const int buttonPins[] = {33, 32, 35, 34}; // {33, 32, 35, 34}
const int ledPins[] = {13, 12, 14, 27}; // {13, 12, 14, 27}
const int numButtons = 4; // calculate the number of buttons
const int numLeds = 4; // calculate the number of LEDs

int ledStates[] = {LOW, LOW, LOW, LOW}; // an array to store the state of each LED

unsigned long lastPressTime[numButtons] = {0}; // array to store the last press time for each button
unsigned long lastDebounceTime[numButtons] = {0}; // array to store the last debounce time for each button
int lastButtonState[numButtons] = {LOW}; // array to store the last button state for each button
int buttonState[numButtons] = {LOW}; // array to store the last button state for each button

const unsigned long pressInterval = 100; // Interval for button press in milliseconds
const unsigned long debounceDelay = 50;  // Debounce time in milliseconds

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < numButtons; i++) {
    pinMode(buttonPins[i], INPUT_PULLUP); // Using internal pull-up resistor for each button
    pinMode(ledPins[i], OUTPUT);
    digitalWrite(ledPins[i], ledStates[i]); // Initialize LED states
  }
}

void loop() {
  for (int i = 0; i < numButtons; i++) {
    int reading = digitalRead(buttonPins[i]);
    if (reading != lastButtonState[i]) {
      lastDebounceTime[i] = millis(); // Reset the debounce timer
    }
    if ((millis() - lastDebounceTime[i]) > debounceDelay) {
      // If the button state has been stable for the debounce delay
      if (reading != buttonState[i]) {
        buttonState[i] = reading; // Update the button state
        Serial.print(i);Serial.print(" ");Serial.println(buttonState[i]);

        // If the button is pressed
        if (buttonState[i] == HIGH) {
          // Check if press interval has passed since last press
          if (millis() - lastPressTime[i] >= pressInterval) {
            ledStates[i] = !ledStates[i]; // Toggle LED state
            digitalWrite(ledPins[i], ledStates[i]); // Update LED state
            lastPressTime[i] = millis();// Update last press time
          }
        }
      }
    }
    lastButtonState[i] = reading;
  }
}

and something like that below:

1 Like
  • Which LEDs work okay ?

  • Always show us a good schematic of your proposed circuit.
    Show us good images of your ‘actual’ wiring.

See image below:

  • Confirm all LEDs are installed with the Cathode going to GND.
    i.e. Anode to the output pin.

  • Are all the LED resistors the same value ?

  • Run the Blink sketch on all the LEDs to see if they work.

@roamingal
Is this image from wokwi.com?
If so, how about posting the link to the simulation.

@ruilviana here you go:

Basically each pushbutton toggles a specific led.

If you have a better way/really simpler way of doing this, it would be much appreciated.

Please see link for detail:

basically the 2 leds can be toggled properly while the other two cant.

Also tried to do this with a simpler code but to no avail:

const byte buttonPins[] = {33, 32, 35, 34};
const byte ledPins[] = {13, 12, 14, 27};
byte previousButtonStates[] = {HIGH, HIGH, HIGH, HIGH};
byte currentButtonState;

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  for (int p = 0; p < 4; p++)
  {
    pinMode(buttonPins[p], INPUT_PULLUP);
    pinMode(ledPins[p], OUTPUT);
  }
}

void loop()
{
  for (int p = 0; p < 4; p++)
  {
    currentButtonState = digitalRead(buttonPins[p]);
    if (currentButtonState != previousButtonStates[p] && currentButtonState == LOW)
    {
      digitalWrite(ledPins[p], !digitalRead(ledPins[p]));
    }
    previousButtonStates[p] = currentButtonState;
  }
}

@roamingal
The error is caused because GPIOs 34 to 39 are GPIs – input only pins. These pins don’t have internal pull-up or pull-down resistors.

Try using pins like 19 and 21 instead and 34 and 35 and it will work.

1 Like

Two buttons worked regardless if they are GPIs. Why is that so?

It is pins 34, 35, 36 and 39 that are input only so pins 32 and 33 work OK

Oh I understand. Thank you for clarifying that

How about my code? is there a better approach in toggling the LEDs using there assigned buttons?

I feel that's a lot of nested if statements for one thing and maybe a simpler way of doing it while still being able to properly debounce

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.