0

Reading a momentary HIGH pin
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Reading a momentary HIGH pin

by Priam on Fri Apr 02, 2021 4:27 am

I'm working with a Trinket M0 running CircuitPython (newest); I'm very new to CircuitPython and the Trinket M0; this is my first project with them.

I have what's probably a very basic question. I see that CircuitPython doesn't support an interrupt handler, but I know there's more than one way to skin a cat.

My Trinket M0 is controlling three NeoPixel rings, and it gets into some pretty long FOR loops with various patterns of light.

And I'd like to change the pattern of lights when it reads voltage on an input pin. However, that voltage is fairly momentary from another circuit (perhaps 0.2 seconds).

Without using an interrupt, what is a good CircuitPython trick to know that input pin went high while the program may have missed it during one of its light pattern FOR loops?

Priam
 
Posts: 15
Joined: Sun Nov 12, 2017 12:44 am

Re: Reading a momentary HIGH pin

by bludin on Fri Apr 02, 2021 6:32 am

Have a look at pulseio.PulseIn, that should do the job.

bludin
 
Posts: 53
Joined: Thu Apr 16, 2020 8:57 am

Re: Reading a momentary HIGH pin

by Priam on Fri Apr 02, 2021 2:42 pm

Nice! Thanks, I'll give that a try.

Priam
 
Posts: 15
Joined: Sun Nov 12, 2017 12:44 am

Re: Reading a momentary HIGH pin

by adafruit_support_mike on Fri Apr 02, 2021 11:09 pm

You can also use a capacitor instead of a pull-down resistor.

The trick is to set the GPIO pin OUTPUT, LOW to discharge the capacitor, then set the pin to INPUT. Closing a switch connected to VCC will charge the capacitor, and the effective resistance of an INPUT pin is high.. usually on the order of 10M. That means it will take a long time (in software terms) for the capacitor to discharge below the voltage that counts as a HIGH input. With a 1uF capacitor, you'd have at least a few seconds.

To see if the switch has been closed, all you have to do is read the pin. Its value can only be HIGH if the switch was closed some time between the GPIO pin being set OUTPUT, LOW and reading the INPUT.

adafruit_support_mike
 
Posts: 62196
Joined: Thu Feb 11, 2010 2:51 pm

Please be positive and constructive with your questions and comments.