Insert your Raspberry Pi Pico into a breadboard.
00:00
A standard momentary push button does not have polarity. Insert the pushbutton into the breadboard. One end will be connected to GPIO2 and the other end into GND.

00:00
Previously, we've seen how the machine.Pin class could be used to turn an LED on and off. This was done by driving a GPIO high and low. Likewise, we'll need it here. So import the machine.Pin class.
00:00
Next, create a new variable called p2 and assign it a new instance of Pin. We do this with the code
p2 = Pin(2, Pin.IN, Pin.PULL_UP)
00:00
Let's break down what's happening in this line. We have a variable called p2
00:00
When creating the p2 variable, we're making it a Pin. Pin here needs three arguments. 
00:00
The first argument is 2 and this is the GPIO pin we want to control.
00:00
The second argument we're providing is Pin.IN. This tells our Pico that we want pin 2 to be an INPUT. We need to tell the Pico that we want the pin to be an Input because our pins are GPIOs (or General Purpose Input/Outputs).
00:00
The third argument we're providing is Pin.PULL_UP. This enables the pull-up resistor for pin 2
00:00
To attach an IRQ to the button pin, add the code p2.irq(trigger= Pin.IRQ_FALLING, handler=lambda pin: print("Hello, Pico!")). Let's talk about what's happening here. We've defined an interrupt that kicks in if the input changes. 

  • The first parameter is the trigger. This is the event that will trigger the handler (ISR) to run. 
  • The second parameter is the handler. This is the function (or lambda) that will be run when the trigger event happens.
In this simple function, it prints out "Hello, Pico!" when GP2 has a falling edge. 

So in summary, we've defined a simple function to handle the interrupt and assigned it to a type of change on a specific pin.
00:00
main.py
xxx
Terminal
screen /dev/tty.usbmodem0000000000001 115200
>>> led = Pin(25, Pin.OUT)
>>> from machine import Pin
Breadboard
Breadboard
Mark
© 2022 Chick Commerce Pty Ltd