Help with esp32 phantom interrupts

Hi all,

I'm not sure whether this is a problem in the sketch or if it is a hardware problem. I am using the DOIT ESP32 DEVKIT V1 board and I've got a "D-PAD" of sorts with 4 tactile buttons. The buttons share a common ground wire. It works as expected most of the time but occasionally I press 1 button and the interrupt for a different button gets triggered first. It's almost like the jittering behavior except instead of seeing a double press its, I'm getting a one-two punch.

I've got some output to demonstrate. There are some other parts to the sketch that I've omitted for simplification here (but i did double check the behavior still occurs in the sketch below) so if it seems overly complicated that's part of the reason.

#define UP 25
#define DOWN 33
#define LEFT 26
#define RIGHT 32

#define DEBOUNCE_MICROS 50000

volatile unsigned long last_micros;
bool isInterrupted = false;
int vAxis = 0;
int vMax = 10;
int vMin = 0;
int hAxis = 0;
int hMax = 10;
int hMin = 0;

int vValue = 0;
int hValue = 0;

void setup() {
  Serial.begin(115200);
  pinMode(UP, INPUT_PULLUP);
  attachInterrupt(UP, debounceUp, RISING);
  pinMode(DOWN, INPUT_PULLUP);
  attachInterrupt(DOWN, debounceDown, RISING);
  pinMode(LEFT, INPUT_PULLUP);
  attachInterrupt(LEFT, debounceLeft, RISING);
  pinMode(RIGHT, INPUT_PULLUP);
  attachInterrupt(RIGHT, debounceRight, RISING);
}

void loop() {
  if (isInterrupted) {
    update();
  }
}

void debounceUp() {
  if((long)(micros() - last_micros) >= DEBOUNCE_MICROS) {
    if (!isInterrupted && vAxis < vMax) {
      ++vAxis;
      isInterrupted = true;
    }
    last_micros = micros();
  }
}

void debounceDown() {
  if((long)(micros() - last_micros) >= DEBOUNCE_MICROS) {
    if (!isInterrupted && vAxis > vMin) {
      --vAxis;
      isInterrupted = true;
    }
    last_micros = micros();
  }
}

void debounceLeft() {
  if((long)(micros() - last_micros) >= DEBOUNCE_MICROS) {
    if (!isInterrupted && hAxis > hMin) {
      --hAxis;
      isInterrupted = true;
    }
    last_micros = micros();
  }
}

void debounceRight() {
  if((long)(micros() - last_micros) >= DEBOUNCE_MICROS) {
    if (!isInterrupted && hAxis < hMax) {
      ++hAxis;
      isInterrupted = true;
    }
    last_micros = micros();
  }
}

void update() {
  if (hAxis != hValue) {
    hValue = hAxis;
    Serial.print("updated h to ");
    Serial.println(hValue);
  }
  else if (vAxis != vValue) {
    vValue = vAxis;
    Serial.print("updated v to ");
    Serial.println(vValue);
  }
  Serial.print("vValue: ");
  Serial.print(vValue);
  Serial.print("; hValue: ");
  Serial.println(hValue);
  isInterrupted = false;
}

Playing with the buttons some here is what I'm seeing
Walking right then left along the horizontal axis
11:33:52.643 -> updated h to 5
11:33:52.643 -> vValue: 0; hValue: 5
11:33:53.568 -> updated h to 4
11:33:53.568 -> vValue: 0; hValue: 4
11:33:54.389 -> updated v to 1 <————— THIS (Up button wasn’t pressed)
11:33:54.389 -> vValue: 1; hValue: 4
11:33:54.554 -> updated h to 3
11:33:54.554 -> vValue: 1; hValue: 3

Only pressing the UP button
11:34:53.842 -> updated v to 1
11:34:53.842 -> vValue: 1; hValue: 0
11:34:55.028 -> updated v to 2
11:34:55.028 -> vValue: 2; hValue: 0
11:34:56.217 -> updated v to 1. <————— THIS (Down button wasn’t pressed)
11:34:56.217 -> vValue: 1; hValue: 0
11:34:56.350 -> updated v to 2
11:34:56.350 -> vValue: 2; hValue: 0

I've tried assigning each interrupt function its own "last_micros" value and that didn't do anything. I really appreciate any assistance with this. Thanks so much in advance. Let me know if I need to provide any additional details

Hi and welcome!

I know I know, that button circuit is almost the worlds most easy circuit, but would you be so kind and upload a schematics or circuit diagram over the buttons and how they are wired to the ESP32 DEVKIT?

I don't want to bother you with something you already know, but you can get an interrupt during the time your interrupt code is executing and you do not take care of that. Try stopping interrupts at the beginning of each interrupt code and turning them back on just before exiting each code.

Sure thing...and I totally am thinking this is where my mistake is coming from. I'm using a cnc router to mill my own pcb for the buttons. Built it using EasyEDA. You'll notice a 5th button in the diagram but it isn't involved in the dpad or main loop function. the buttons run to a set of header pins. Then using wires i crimped with female ends I'm connecting directly to the corresponding pins on the devkit .....though I was planning to mill another board to hold the more complicated components (assuming i can figure this out)


I did not know that :slight_smile: I'll update my code accordingly! Thanks

Elegant PCB, and I see nothing wrong with it. A tip, out of this topic, check out ground planes in future PCB projects. It'll make connecting everything a lot more simple, since your EDA now manage the GND connections, and gives you feedback from the ERC. You can even use one for a single sided board.

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