[EN] An Interrupt in MicroPython

This article describes the interrupt and intercept principle with MicroPython, experimented with ESP8266 and ESP32 as a guide to programming an external event response without waiting for the work in progress completed first.

(Figure. 1 Interrupt intercepting range detection experiment)

Interrupt

Typically, programming for use in embedded systems or detecting, such as checking for normal things that come close (Figure 1). When drawing a diagram of the work, it will be as shown in Figure 2. There is a loop to repeat the work endlessly and in each loop check for abnormalities, if found, will report to you but if not found or finished reporting, there will be a delay of 1 second to wait for the next cycle. We found that during the 1-second waiting period something unusual can occur without the system detected due to the delay command. Even if the delay order is removed but when adding other functions, the system must always wait for verification before doing other tasks, so how to make the program or the system work normally? and when something unusual is found, it will give a warning. To reduce the time of unavailable and eliminate the need to waste time checking the status of every cycle.

(Figure. 2 Self-detection operation)

In Figure 3, it can be seen that when an interrupt interception is set, the operation cycle does not need to check for the status of the fault. But the design of the system to send interrupt signals to the processor will know when an interrupt occurs, the processor stops running instruction and reports it. When it’s done, it’s back to where it left off.

(Figure. 3 Detection with interrupt interception)

Instruction

The command to define an interrupt interception in Micropython is done by creating a callback function to be called upon interruption. The format of the created function is as follows.

def fname( pin ):
global interrupt_pin
interrupt_pin = pin
what to do when interrupt happen.

The command to determine the intercepting interrupts from the signal of the GPIO pin requires two steps:

  1. Create a variable for accessing the interrupt receiver pin. By setting the function of the pin to Pin.IN
  2. Use the irq() command in the following format.

pin_variale.irq( trigger=interrupt_period, handler= interrupt_response_function)

Interrupt interception

This is because a digital signal has four signal states: a period that remains 0, a period that remains 1 and a period that changes from 0 to 1 and from 1 to 0, as shown in Figure 4.

(Figure. Interrupt period)

MicroPython supports three types of interrupt interception:

  1. Pin.IRQ_RISING for intercepting the level changes from 0 to 1.
  2. Pin.IRQ_FALLING for intercepting the level changes from 1 to 0.
  3. For both changes.

Example Code

The following example code checks the approaching distance of an object. If it is near an object, it will interrupt and display Alert!!!. The sensor is IR Reflects by connecting pin Vcc to 3V3, GND to GND and pin OUT to D1.

(Figure. 5 Status light when objects are far away)
(Figure. 6 Status light when objects are near)
import machine as mc
alertPin = mc.Pin(5, mc.Pin.IN) # D1

def alert_interrupt(pin):
    global interrupt_pin
    interrupt_pin = pin
    print("Alert!!!")

if __name__=="__main__":
    alertPin.irq(trigger=mc.Pin.IRQ_RISING, handler=alert_interrupt)
    while True:
        pass

Conclusion

From this article, we hope that it will be a guide for the reader’s programming to write an interrupt intercept program with Python on the MicroPython system. Design a circuit to input data as 0 or 1, and then create a function that responds to changes from 0 to 1 or from 1 to 0, or both so that the interrupt-response function runs. This gives the processor time to perform other instructions without having to spend time checking every cycle. Finally, have fun with programming.

If you want to discuss or talk with us, feel free to leave comments below!

(C) 2020-2021, By Jarut Busarathid and Danai Jedsadathitikul
Updated 2021-10-07