Problem with interrupt handling

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
Mohamed
Posts: 9
Joined: Thu Jun 11, 2020 6:54 pm

Problem with interrupt handling

Post by Mohamed » Thu Jun 11, 2020 7:01 pm

Hello,
I use fork light barrier sensor TCXT2103, 20 slots disk and ESP32 to measure the speed of the motor. When I run my code, I get a huge number of counts for 1 revolution (about 90) . I test tested the sensor with Arduino and got the right count (20)
could you tell me how to solve this problem please?
Thanks in advance

Code: Select all

from machine import Pin
class EncoderCounter:
    def __init__(self,pin_number):
        self.pulse_count = 0
        self.LightB1 = Pin(pin_number,Pin.IN)
        self.LightB1.irq(trigger=Pin.IRQ_RISING, handler=self.M1)
    def M1(self,Pin):
        self.pulse_count += 1
        print(self.pulse_count)

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Problem with interrupt handling

Post by Roberthh » Thu Jun 11, 2020 7:12 pm

If Arduino also means Arduino controller, it could be caused by the input section of the ESP32. The ESP32 may trigger several interrupts, if the input signal has slow rise/fall times. If that is the case, you could either try to detect a double trigger in software, or try to increase the signal ramp by hardware means.
I do not know which load resistor you have at the input, but lowering that could be an option.
Also with the phototransistor, ON times are usually shorter than OFF times, so you could try to trigger on the ON-event.
If that does not help, add a Schmitt-Trigger Gate between the Sensor output and the ESP32 input.

Mohamed
Posts: 9
Joined: Thu Jun 11, 2020 6:54 pm

Re: Problem with interrupt handling

Post by Mohamed » Thu Jun 11, 2020 8:31 pm

Roberthh, thank you for your reply. I use 10kohm resistor on the input. I will try your suggestions and give a feedback.

Mohamed
Posts: 9
Joined: Thu Jun 11, 2020 6:54 pm

Re: Problem with interrupt handling

Post by Mohamed » Fri Jun 12, 2020 10:46 am

well, I detected a double trigger, and lowered the esp frequency to 20MHz and get the right count. the more the frequency is, the higher the counted interrupts are. In the real program I can't use this frequency. so any suggestions?
I tried to connect 5Kohm resistor to the input but nothing interesting happens.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Problem with interrupt handling

Post by Roberthh » Fri Jun 12, 2020 2:55 pm

I looked the results form previous testing. And indeed I have seen double triggers with rise & fall times < 10µs. Since the rise and fall time of the sensor output is in that order, you have two options:

- increase the slew rate with additional hardware. A single 74hc14 gate should do that. Maybe a single transistor like a 2N7000 or 2N2222 is also sufficient.
- try to ignore the second interrupt in the software. What I did once was resetting the IRQ flag in the first call. by the time, the first call gets active, the second interrupt should already be triggered. For a direct clear at the I/O hardware the start is most likely to slow. By the time the scheduler for the first interrupt starts, the second interrupt may already be scheduled. So maybe you could trigger on both edges and consider a second interrupt at the same edge as invalid.

The first option is easier. The second does not require additional hardware.

Mohamed
Posts: 9
Joined: Thu Jun 11, 2020 6:54 pm

Re: Problem with interrupt handling

Post by Mohamed » Fri Jun 12, 2020 3:54 pm

I have already got shmitt trigger. I tried it and it work very well. I don't even have to detect double trigger.
I would like to deeply thank you for your help.
My next step,
- Since I will use these 2 fork light barrier sensors for measuring the travelled distance by the robot, I found out from other users experience that it may miss some slots and it will affect the accuracy of the system.
- I plan to run the interrupt functions on a single core of the dual esp32 cores, and the rest of the program on the other core. Is it a good solution? or should I search for a better one?
- How can I programatically devote a single core for running a specific function?

Post Reply