Pin interrupts triggering errantly?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
BobRyan
Posts: 3
Joined: Sun Nov 06, 2016 9:36 pm

Pin interrupts triggering errantly?

Post by BobRyan » Sun Nov 06, 2016 9:47 pm

I've got an application which takes input from a SPDT switch connected to pins 0 and 2. I'm seeing both interrupts triggered (almost) every time one should. I've checked the inputs on my scope and with a logic analyzer and while there's a tiny bit of bounce there's no action on the other pin.

I assume this must be a software problem so I tried a few different releases of micropython but I have the same behavior with each.

Perhaps someone could shed some light on this or perhaps test it in their environment?

I've added the print() and disable/enable_irq calls while debugging this and they seem to make no difference.

Thanks -Bob

apologies, code formatting doesn't seem to be working. code is indented as expected.

Code: Select all

from time import sleep_us
from machine import Pin
from machine import disable_irq, enable_irq

def up_swcb(p):
	irq_state = disable_irq()
	global direction, trigger
	trigger = 0
	direction = 1
	print('up switch callback')
	enable_irq(irq_state)

def down_swcb(p):
	irq_state = disable_irq()
	global direction, trigger
	trigger = 0
	direction = -1
	print('down switch callback')
	enable_irq(irq_state)
		
up_sw = Pin(2, Pin.IN, Pin.PULL_UP)
down_sw = Pin(0, Pin.IN, Pin.PULL_UP)

up_sw.irq(trigger=Pin.IRQ_FALLING, handler=up_swcb)
down_sw.irq(trigger=Pin.IRQ_FALLING, handler=down_swcb)

while True:
	sleep_us(1000)
and my results with the following switch sequence: up, up, down

Code: Select all

MicroPython v1.8.3-dirty on 2016-11-06; ESP module with ESP8266
Type "help()" for more information.
>>> 
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== from time import sleep_us
=== from machine import Pin
=== from machine import disable_irq, enable_irq
=== 
=== def up_swcb(p):
===     irq_state = disable_irq()
===     global direction, trigger
===     trigger = 0
===     direction = 1
===     print('up switch callback')
===     enable_irq(irq_state)
=== 
=== def down_swcb(p):
===     irq_state = disable_irq()
===     global direction, trigger
===     trigger = 0
===     direction = -1
===     print('down switch callback')
===     enable_irq(irq_state)
=== 
=== up_sw = Pin(2, Pin.IN, Pin.PULL_UP)
=== down_sw = Pin(0, Pin.IN, Pin.PULL_UP)
=== 
=== up_sw.irq(trigger=Pin.IRQ_FALLING, handler=up_swcb)
=== down_sw.irq(trigger=Pin.IRQ_FALLING, handler=down_swcb)
=== 
=== while True:
===     sleep_us(1000)
=== 
<IRQ>
<IRQ>
up switch callback
up switch callback
down switch callback
up switch callback
down switch callback
up switch callback
down switch callback
up switch callback
down switch callback
up switch callback
down switch callback
up switch callback


MicroPython v1.8.4 on 2016-11-06; ESP module with ESP8266
Type "help()" for more information.
>>> 
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== from time import sleep_us
=== from machine import Pin
=== from machine import disable_irq, enable_irq
=== 
=== def up_swcb(p):
===     irq_state = disable_irq()
===     global direction, trigger
===     trigger = 0
===     direction = 1
===     print('up switch callback')
===     enable_irq(irq_state)
=== 
=== def down_swcb(p):
===     irq_state = disable_irq()
===     global direction, trigger
===     trigger = 0
===     direction = -1
===     print('down switch callback')
===     enable_irq(irq_state)
=== 
=== up_sw = Pin(2, Pin.IN, Pin.PULL_UP)
=== down_sw = Pin(0, Pin.IN, Pin.PULL_UP)
=== 
=== up_sw.irq(trigger=Pin.IRQ_FALLING, handler=up_swcb)
=== down_sw.irq(trigger=Pin.IRQ_FALLING, handler=down_swcb)
=== 
=== while True:
===     sleep_us(1000)
=== 
<IRQ>
<IRQ>
down switch callback
up switch callback
down switch callback
up switch callback
up switch callback
down switch callback
up switch callback
down switch callback
up switch callback
down switch callback
up switch callback
down switch callback
up switch callback
down switch callback
up switch callback



MicroPython v1.8.5 on 2016-11-06; ESP module with ESP8266
Type "help()" for more information.
>>> 
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== from time import sleep_us
=== from machine import Pin
=== from machine import disable_irq, enable_irq
=== 
=== def up_swcb(p):
===     irq_state = disable_irq()
===     global direction, trigger
===     trigger = 0
===     direction = 1
===     print('up switch callback')
===     enable_irq(irq_state)
=== 
=== def down_swcb(p):
===     irq_state = disable_irq()
===     global direction, trigger
===     trigger = 0
===     direction = -1
===     print('down switch callback')
===     enable_irq(irq_state)
=== 
=== up_sw = Pin(2, Pin.IN, Pin.PULL_UP)
=== down_sw = Pin(0, Pin.IN, Pin.PULL_UP)
=== 
=== up_sw.irq(trigger=Pin.IRQ_FALLING, handler=up_swcb)
=== down_sw.irq(trigger=Pin.IRQ_FALLING, handler=down_swcb)
=== 
=== while True:
===     sleep_us(1000)
=== 
<IRQ>
<IRQ>
down switch callback
up switch callback
up switch callback
down switch callback
up switch callback
down switch callback
up switch callback
up switch callback
up switch callback
up switch callback
down switch callback
up switch callback
down switch callback
up switch callback


with sdk 2.0
MicroPython v1.8.5-122-g8f068e8 on 2016-11-06; ESP module with ESP8266
Type "help()" for more information.
>>> 
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== from time import sleep_us
=== from machine import Pin
=== from machine import disable_irq, enable_irq
=== 
=== def up_swcb(p):
===     irq_state = disable_irq()
===     global direction, trigger
===     trigger = 0
===     direction = 1
===     print('up switch callback')
===     enable_irq(irq_state)
=== 
=== def down_swcb(p):
===     irq_state = disable_irq()
===     global direction, trigger
===     trigger = 0
===     direction = -1
===     print('down switch callback')
===     enable_irq(irq_state)
=== 
=== up_sw = Pin(2, Pin.IN, Pin.PULL_UP)
=== down_sw = Pin(0, Pin.IN, Pin.PULL_UP)
=== 
=== up_sw.irq(trigger=Pin.IRQ_FALLING, handler=up_swcb)
=== down_sw.irq(trigger=Pin.IRQ_FALLING, handler=down_swcb)
=== 
=== while True:
===     sleep_us(1000)
=== 
<IRQ>
<IRQ>
up switch callback
down switch callback
up switch callback
down switch callback
up switch callback
up switch callback
down switch callback
up switch callback
down switch callback
up switch callback
down switch callback

tknp
Posts: 5
Joined: Sun Feb 19, 2017 6:26 am

Re: Pin interrupts triggering errantly?

Post by tknp » Sun Feb 19, 2017 6:29 am

Just echoing that I am seeing the same behavior as you are. It looks like the interrupts are being triggered not just when the specific interrupt state is listed but by any change.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Pin interrupts triggering errantly?

Post by dhylands » Sun Feb 19, 2017 6:34 pm

I highly recommend that anybody dealing with real physical switches read this:
http://www.ganssle.com/debouncing.htm

tknp
Posts: 5
Joined: Sun Feb 19, 2017 6:26 am

Re: Pin interrupts triggering errantly?

Post by tknp » Sun Feb 19, 2017 11:10 pm

That was an informative read. Thanks for that. Looking forward to implementing some debouncing strategies now.

Post Reply