Raspberry Pi Pico
Digital OUT - LEDs

For our first CircuitPython project, we will get some LEDs going. That covers how we get the Pico to write digital signals. Here is a photograph of my circuit. I have some LEDs and resistors hooked up to pins 10, 11 and 12.

Pico Circuit

The following diagram shows more clearly how these connections are made.

Pico Circuit

For the first program, we will just get the LEDs working. We need a few import statements. We need to set the direction for the pin. When we set the value of the LED to high and low, it is slightly different to the way it is done in MicroPython.

import board 
from time import sleep 
from digitalio import DigitalInOut, Direction 

red = DigitalInOut(board.GP10) 
red.direction = Direction.OUTPUT 

yellow = DigitalInOut(board.GP11) 
yellow.direction = Direction.OUTPUT 

green = DigitalInOut(board.GP12) 
green.direction = Direction.OUTPUT 
 
while True: 
    green.value = 0 
    red.value = 1 
    sleep(0.5) 
    red.value = 0 
    yellow.value = 1 
    sleep(0.5) 
    yellow.value = 0 
    green.value = 1 
    sleep(0.5) 

When you have multiple components of the same type, it is worth using lists to make the processing a little more efficient. That is what I have done in the next program,

import board 
from time import sleep 
from digitalio import DigitalInOut, Direction 

# define the pins as a list 
led_pins = [board.GP10, board.GP11, board.GP12] 

# list comprehension to set up the pins 
leds = [DigitalInOut(p) for p in led_pins] 

# set the direction using a loop 
for l in leds: 
    l.direction = Direction.OUTPUT 
 
# all off 
def allOff(lights): 
    for p in lights: 
        p.value = 0 
 
# turn on/off individually, list, index, value 
def light(lights, p, v): 
    lights[p].value = v 

while True: 
    for i in range(len(leds)): 
        allOff(leds)
        sleep(0.5) 
        light(leds, i, 1) 
        sleep(0.5)