[solved] falling and rising interrupt seem to behave the same as change

Ahoi! :slight_smile:

I'm working on a project controlling standard dc motors via a photoelectic sensor with a lm393 and an encoder disc.
I connect the sensor to digital pin 2 and attach an interrupt via

attachInterrupt(digitalPinToInterrupt(2), sensor1Pulse, FALLING);

sensor1Pulse() just increases or decreases an integer according to a +1, or -1 rotation variable.
I also tried with RISING.

I tried with my Uno rev3, my Nano rev3 and my Mega2560, but interrupt mode does not seem to have any effect. When defining FALLING it also triggers on rising input, and vice versa.

The sensor output seems as expected and I tried with all 4 sensors I own. Dso oscilloscope (and dito my cheap multimeter) shows the output potential is around 0.2 V when not blocked and 4.2V when the ir-light is blocked. The transition does not seem overly bouncy or noisy, but quite "sharp". The sensors indicator-led also behaves as expected.

A problem that might be related to this is that I get incorrect results, when I spin the motor which is connected to the encoder disc at a certain speed (i.e. I use a Lego motor and when supplied with more than 6V there seem to be ignored steps). But I have seen examples of motors spinning way faster and encoder discs having way more steps per revolution - and my interrupt routine is even shorter and less complex than the code used in corresponding examples (they often use 2 sensors to also detect rotation direction and need to differenciate that in interrupt triggered method - I just check a rotation direction value and increase or decrease step count accordingly).

The problem does not seem to be related to how I control the motor itself - it is same, when I power it externaly, as when I power it via anduino and h bridge.

What might I be missing? Are all my 3 arduino defect?

I helped someone with reading an encoder. We had a lot of trouble with getting clean edges on the signal so he used Schmitt triggers to clean them up. Your meter won't react fast enough to show a bouncy signal. Maybe your scope should, but it doesn't sound like it.

Many of the low cost hobby sensors have comparators based on the LM393 without any hysteresis feedback, and it is not uncommon to experience multiple interrupts/noise troubles with these basic lm393 comparator modules.

Performance is often improved with a cap between D0 and ground. That solution and other possible circuit mods for hysteresis improved response are discussed here

wildbill:
I helped someone with reading an encoder. We had a lot of trouble with getting clean edges on the signal so he used Schmitt triggers to clean them up. Your meter won't react fast enough to show a bouncy signal. Maybe your scope should, but it doesn't sound like it.

But when I use CHANGE, I get one interrupt per change - shouldn't that be more then, if it is the bouncy signal?

When I debounce the ISR with 20ms-threshold and rotate slowly - I still get 1 FALLING-interrupt per change.

Also I naively thought the ic on the sensor would contain Schmitt triggers - but I should look into the datasheets of that. And will, but tomorrow - too sleepy to read now ^^

Thanks for your input!

Schmitt trigger on the output of the sensor is useless, if the noise is introduced on the signal output by the sensor. The Schmitt needs to be on the INPUT of the device reading the sensor.

Regards,
Ray L.

cattledog:
Many of the low cost hobby sensors have comparators based on the LM393 without any hysteresis feedback, and it is not uncommon to experience multiple interrupts/noise troubles with these basic lm393 comparator modules.

Performance is often improved with a cap between D0 and ground. That solution and other possible circuit mods for hysteresis improved response are discussed here

https://forum.arduino.cc/index.php?topic=342650.0

Thank you for clarifying and helpful link!

I'll try the proposed solution from the last post right now:

bsavelli66:
I was having the same problem and adding the 104 ceramic capacitor from the interrupt pin (d2) to ground completely solved it.

If that works for finer granulated debouncing (that's what this does put simply, does it?) - I'll reply here later.
If not I'll go through that thread - but tomorrow - too sleepy to read much now ^^

But when I use CHANGE, I get one interrupt per change - shouldn't that be more then, if it is the bouncy signal?

When I debounce the ISR with 20ms-threshold and rotate slowly - I still get 1 FALLING-interrupt per change.

I'm a little unclear on the problem statement.

You say

photoelectic sensor with a lm393 and an encoder disc.
When defining FALLING it also triggers on rising input, and vice versa.

Is there a pullup resistor on the output from the sensor?

Can you see an interrupt with the mode set to FALLING? Is that on transition to blocked or transition to clear?

Can you see an interrupt with the mode set to RISING? Is that on transition to blocked or clear?

You say you do not see both edges when the interrupt is set for CHANGE?

Ok - the capacitor seems to do the trick =) counting revolutions exactly now =)

cattledog:
Can you see an interrupt with the mode set to FALLING? Is that on transition to blocked or transition to clear?

Can you see an interrupt with the mode set to RISING? Is that on transition to blocked or clear?

You say you do not see both edges when the interrupt is set for CHANGE?

No, on mode FALLING it seemd, I also got ISR executed on rise. On mode RISING it seemed, I also got trigger on falling.
Sensor blocked -> 4.2V - Sensor not blocked -> 0.1V

CHANGE seemed to work normal. But that is what's strange to me - that I didn't get at least twice as manny recognized impulses. (I'd be happy if someone has an explaination for this)

Now all seems well so far: Rise only triggers when RISING mode and vice versa - CHANGE also seems to work as expected.

Ok - the capacitor seems to do the trick =) counting revolutions exactly now =)

Great. The $2 lm393 modules have certainly given problems. The issue with the capacitor solution is that it may create a problem to respond at high speeds. You then may want to try the circuit with a hysteresis control feedback loop as posted in reply#19 of the earlier cited thread.

Rs.jpg

Thanks for pointing that out!
I'll try with motors spinning at 12v (the day before yesterday I only tested with half-drained batteries).
If I get inconsitent results with maximum rotation speed, I'll implement your suggestion.
Will look into it in any case out of curiosity ^^

I'll set "[solved]". Thank you all for your help!

EDIT:
I tried with 12v (wich is maximum voltage I'll throw at the particular logo motors) and let it turn 4000 times (100 times loop 40 rotations and pause with 1:40 geared down indicator disc) - and got exactly the corresponding step-count. So the capacitor solution is sufficient for my usecase.
But the day when I'd like to control a faster motor, use higher resolution encoder discs, or check rotations of geared up output will occur - good to have a concrete hint, what to do then ^^