Interrupts example problem using external led and button


#1

The example works in this way: when I push the button the led has to turn on for 100ms and I get a print "touched!"

When I do this with the integrated hardware (button and led) it works reliably.

But when I use an external led and button it’s not reliable. After releasing the button the led stays on for a while and I get more then one “touched!” print.

I’m thinking it’s because it require an additional pullup resistor and the internal pullup isn’t enough. So I added a 10K ohm resistor between the pin linked to the button and the 3.3V but this doesn’t improve the reliability.

################################################################################

Interrupt Basics

Created by ZERYNTH Team 2015 CC

Authors: G. Baldi, D. Mazzei

################################################################################

import streams

create a serial port stream with default parameters

streams.serial()

define where the button and the LED are connected

in this case BTN0 will be automatically configured according to the selected board button

change this definition to connect external buttons on any other digital pin

buttonPin=D0
ledPin=D4 # LED0 will be configured to the selected board led

configure the pin behaviour to drive the LED and to read from the button

pinMode(buttonPin,INPUT_PULLUP)
pinMode(ledPin,OUTPUT)
i=0

define the function to be called when the button is pressed

def pressed():
print("touched = ",i)
digitalWrite(ledPin,HIGH) # just blink the LED for 100 millisec when the button is pressed
sleep(1000)
digitalWrite(ledPin,LOW)
i=i+1

attach an interrupt on the button pin and call the pressed function when it falls

being BTN0 configured as pullup, when the button is pressed the signal goes to from HIGH to LOW.

opposite behaviour can be obtained with the equivalent “rise” interrupt function: onPinRise(pin,fun)

hint: onPinFall and onPinRise can be used together on the same pin, even with different functions

onPinFall(buttonPin,pressed)

```

#2

any news ?


#3
cman said:
any news ?
nope. You had the same problem?

#4

yes. I have.
I read on the web that is it possible to solve this problem by hardware solutions (by capacitor), but i don’t have tried …




#5

Form the software point of view, I am adding a “debounce” option to the onPinRise and onPinFall functions, so that every pin transition that lasts less than debounce time will be ignored.

However, since this could also be a hardware related problem I will ask @Daniele and @Lorenzo to check the issue 


#6

here, an interesting article about debouncing:

http://coder-tronics.com/switch-debouncing-tutorial-pt1/


#7

debounce time now supported by onPinRise/Fall  :slight_smile: