Interrupt triggering

MuchMore
Posts: 7
Joined: Sat Mar 17, 2018 10:28 am

Interrupt triggering

Postby MuchMore » Sat Mar 17, 2018 10:54 am

Hey

I am quite new to the ESP32 Platform. Today I tried to connect an rotory encoder with button to my ESP32.
On the Arduino UNO I used interrupts to speed up the detection. And also to make the edge cleaner I use 100nF caps.

Now to my problem: The ESP32 triggers constantly when the edge is changing. The higher to capasitor, and therefor slower the rise/fall time, the more events are detected. This goes so far that if I use a 1000uF cap the ESP32 gets stuck inside a loop that doesn't do away if I remove the cap.

Of cours this makes the use of interrups totally useless. even if I code a software timed backout, it would be more economical to use aktiv pooling.

Am I doing something wrong or is this the desired operation of interreupts, and the UNO was just to slow to give me the same problems?

The code I used:

Code: Select all

const byte interruptPin = 27;
volatile int interruptCounter = 0;
int numberOfInterrupts = 0;
 
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
 
void IRAM_ATTR handleInterrupt() {
  portENTER_CRITICAL_ISR(&mux);
  interruptCounter++;
  portEXIT_CRITICAL_ISR(&mux);
}
 
void setup() {
 
  Serial.begin(115200);
  Serial.println("Monitoring interrupts: ");
  pinMode(interruptPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, FALLING);
 
}
 
void loop() {
 
  if(interruptCounter>0){
 
      portENTER_CRITICAL(&mux);
      interruptCounter--;
      portEXIT_CRITICAL(&mux);
 
      numberOfInterrupts++;
      Serial.print("An interrupt has occurred. Total: ");
      Serial.println(numberOfInterrupts);
  }
}
BTW.: I checked the signal with an oscilloscop and it is totally clean so this is no bouncing problem.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Interrupt triggering

Postby kolban » Sun Mar 18, 2018 3:37 pm

Is the incoming signal a digital or analog signal? I'd like to hear more about the signal that is causing the detection of the interrupt? Do you have a square wave generator that you can use for testing that allows the frequency to be adjusted?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

MuchMore
Posts: 7
Joined: Sat Mar 17, 2018 10:28 am

Re: Interrupt triggering

Postby MuchMore » Sun Mar 18, 2018 8:44 pm

Hey,
It is a analog signal. Button and rotory Encoder.
Unfortunatly I do not have a freqeuenzy generator. (maybe I can use my audio gear and windows sound generator)
However I also use an DS1307 RTC Clock in my project. The SQWOUT pin gives a 1Hz signal to trigger an Update in my code. And this signal does not have any bouncing issue and so I am not bound to use smoothing caps.
In this szenario the interrupts work perfectly. Even RISING/FALLING seems to work as they sould.(since I only get 1 event per second)

Since I posted my first post I tryed to inclued delays and checking the pin-state again. However it seems that delays are somewhat ignored inside the interropt handler. Delays > 10ms do not bring any better results and still trigger min 2 Events for one edge.

Next I tried to check with analogRead however analogRead does not work inside the interrupt handler.

Also I read that it is possible to trigger on HIGH and LOW. However since the rotory encoder can stop on HIGH or LOW it would trigger continuously.

It realy comes down to this: The signal is bouncing therfor multiple interrupts are triggert. If smoothing caps are used the edge becomes slow and multiple interrupts are triggert.
If there is no funktion to set a threshold value for triggering than I have no clue how to get a sigel trigger event.

MuchMore
Posts: 7
Joined: Sat Mar 17, 2018 10:28 am

Re: Interrupt triggering

Postby MuchMore » Mon Mar 19, 2018 1:52 am

So after reading a lot about software debouncing and trying some things out this code works 50% of the time:

Code: Select all

void IRAM_ATTR INT_ENC_A() {
  if((long)(micros() - last_micros_A) > debouncing_time) {
    ENCODER_UPDATE();
    last_micros_A = micros();
  }
}
void IRAM_ATTR INT_ENC_B() {
  if((long)(micros() - last_micros_B) > debouncing_time) {
    ENCODER_UPDATE();
    last_micros_B = micros();
  }
}
The good part: since Signal A and B are correlated it is relly robust against extra steps.
The bad part: when the knob is twisted quit fast there are between 1000-3000 trigger events. And after some time the interrupt handler seems to stop responding. It recovers after 10-20sec.

So I guess there are two options:
1.- use aktiv pooling to have an upper limmit in the number of checks.
2.- use schmitt triggers on the inputs.

Me as a noob can't relly belive that a pin without threshold intervall is usefull for interrupts.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Interrupt triggering

Postby kolban » Mon Mar 19, 2018 3:06 am

I'm very slow on electronics ... so please forgive me if this line of thinking is completely wrong. I am imagining a "logical" story as follows. I am imagining in an input signal from 0v (low) to 1v (high). A signal < 0.5v is considered low and a signal >0.5v is considered high. I am imagining an interrupt firing when the signal transitions from high to low or low to high.

With discrete signals, the signal will either always be 0v or 1v. However if I am sensing an analog story, then the signal can be anywhere in between. For values near the center, "odd" things might happen. If we imagine an increasing signal ... when it transitions for 0.49 to 0.50, that might be a transition fro low to high. However, the signal might not be that clean or it may fluctuate between 0.49 and 0.50 rapidly (0.49999 to 0.50000) ...

This might be what is causing your fluctuations. It may be you need a hysteresis circuit as part of your story. For example, a switch from low to high may not be allowed before the signal passes 0.75v and a switch from high to low may not be allowed before the signal falls below 0.25v. I think there are some ICs/components that can be configured for just those state transitions.

See also:

http://www.quantumdev.com/what-is-hyste ... -encoders/
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: Interrupt triggering

Postby tele_player » Tue Mar 20, 2018 4:29 am

Depending on how you’ve wired the button and encoder, you may need pull-ups or pull-downs, either internal or external.

Who is online

Users browsing this forum: No registered users and 14 guests