Digital Output
Let's blink a LED!
Here's the bread board layout. The resistor can be something around 1kOhm. We don't need to make the LED super bright.
And here's a complete blink program you can run to make the LED blink forever.
import time import board import digitalio led = digitalio.DigitalInOut(board.GP17) led.direction = digitalio.Direction.OUTPUT while True: led.value = True time.sleep(0.5) led.value = False time.sleep(0.5)
Digital Input
Let's read a button!
The cool thing here is that the Pico has internal pull up resistors. Therefore we don't need to add any additional external resistors, which you might see in some other wiring diagrams. The equivalent resistor is inside the Pico!
Here's the breadboard layout.
Here's the code to run. It will continuously print the button state.
-
True
= not pressed -
False
= pressed
import board import digitalio button = digitalio.DigitalInOut(board.GP16) button.direction = digitalio.Direction.INPUT button.pull = digitalio.Pull.UP while True: print(button.value)
Digital Input and Output
Ok, let's put those two together and make the button turn on the LED. So we'll use two digital pins - one will be an input (button) and one will be an output (LED).
Here's the bread board layout.
And here's the code. Note how the code uses not
to invert the button logic.
import board import digitalio led = digitalio.DigitalInOut(board.GP17) led.direction = digitalio.Direction.OUTPUT button = digitalio.DigitalInOut(board.GP16) button.direction = digitalio.Direction.INPUT button.pull = digitalio.Pull.UP while True: led.value = not button.value
Text editor powered by tinymce.