top of page
Writer's pictureShubham Ubale

NodeMCU GPIO Interrupts with ESPlorer IDE

Introduction

An interrupt is an event that occurs randomly in the flow of continuity. It is just like a call you have when you are busy with some work and depending upon call priority you decide whether to attend or neglect it.

NodeMCU based ESP8266 has an interrupt feature on its GPIO pins. This function is available on D0-D8 pins of NodeMCU Dev Kit.

We can set the rising edge, falling edge, both edges, low level, and high level interrupt modes on GPIO pins of Kit.

We need to use the below functions to initialize interrupt and mode of interrupt for specific GPIO pin.

 

gpio.mode()

This function is used to initialize pin to GPIO modes.


Syntax:

gpio.mode(pin, mode [, pullup])


Parameters:

Pin: pin no. to configure.

Mode: one of gpio.OUTPUT, gpio.OPENDRAIN, gpio.INPUT, or gpio.INT (interrupt mode)

Pullup: gpio.PULLUP enables the weak pull-up resistor. By default it is gpio.FLOAT


Returns: null


Example:

gpio.mode(1, gpio.INT)    -- Setting 1st (D1) pin as GPIO interrupt pin

 


gpio.trig()

This function is used to trigger or clear a callback function to run on interrupt occurred on a specific pin. This function is available only if GPIO_INTERRUPT_ENABLE is defined at compile time.


Syntax:

gpio.trig(pin, [type [, callback_function]])


Parameters:

Pin: 1-12 pins can be used for trigger on interrupt. pin 0 does not support interrupts.

Type: "up", "down", "both", "low", "high", which represent rising edge, falling edge, both edges, low level, and high level trigger modes respectively. If the type is "none" or omitted then the callback function is removed and the interrupt is disabled.

callback_function(level, when): callback function to be called when an interrupt occurs. The level of the specified pin at the interrupt passed as the first parameter to the callback function. The timestamp of the event is passed as the second parameter. This is in microseconds and has the same base as for tmr.now(). This timestamp is grabbed at the interrupt level and is more consistent than getting the time in the callback function. The previous callback function will be used if the function is omitted.


Returns: null

 

Example

Let’s write a Lua script to set a rising edge interrupt on the 2nd GPIO pin of NodeMCU. Here we connect the switch on 2nd GPIO pin to generate a rising edge interrupt on it. Also, we are going to print the level on interrupt pin and time (in microseconds) of interrupt occurred.

 

Lua Script for GPIO Interrupt

GPIO_PIN = 2

gpio.mode(GPIO_PIN,gpio.INT)    -- Set GPIO interrupt mode for pin 

function interrupt(level, stamp)-- callback function while interrupt
gpio.trig(GPIO_PIN)             -- disable interrupt for that pin
tmr.delay(700000)               -- wait 700 ms
    print('level:',level)       -- print level of on pin
    print('stamp(us):',stamp)   -- print timestamp while interrupt occur
print('interrupt on pin:', GPIO_PIN)--print interrupt pin no.
gpio.trig(GPIO_PIN,"up", interrupt)--re-enable interrupt on pin while exit
end

gpio.trig(GPIO_PIN,"up", interrupt)-- set interrupt type up i.e. rising edge

Output Serial Window



Components Used:-






0 views0 comments

Comments


bottom of page