Pico LTE button, LED, PWM, Qwiic and NeoPixel

MicroPython examples for the components of the Pico LTE: blink the user LED, read the button, dim the LED with PWM, read a Qwiic sensor and drive the NeoPixel LED.

Examples for the board’s own components: the user LED and button, PWM, a Qwiic sensor and the NeoPixel LED. They need no cellular connection. Run them in Thonny as in the quickstart.

The user LED is on GP22; HIGH turns it on. led.toggle() switches it every second:

import machine
import utime

led = machine.Pin(22, machine.Pin.OUT)

while True:
    led.toggle()
    utime.sleep(1)

Turn LED on with button

The user button is on GP21 and reads 0 while pressed. The LED is on while the button is pressed; the 0.1 s pause debounces the button.

import machine
import utime

led = machine.Pin(22, machine.Pin.OUT)  # We set up the pin to control the light.
button = machine.Pin(21, machine.Pin.IN, machine.Pin.PULL_DOWN)  # We set up the pin for the button.

while True:
    if button.value() == 0:  # If the button is pressed (value is 0):
        led.on()  # Turn on the light.
    else:  # If the button is not pressed (value is 1):
        led.off()  # Turn off the light.
    utime.sleep(0.1)  # Wait for a short moment before checking again.

Dim LED with PWM

PWM (pulse width modulation) sets the LED’s brightness through the duty cycle: duty_u16() takes 0 (off) to 65535 (full). The loop brightens and dims the LED in steps of 1024; change the range and the step to change the speed.

import machine
import utime

led = machine.Pin(22, machine.Pin.OUT)  # Set up a pin to control the LED.
pwm_led = machine.PWM(led)  # Initialize PWM (Pulse Width Modulation) for the LED.

while True:
    for duty_cycle in range(0, 65535, 1024):  # Increase LED brightness.
        pwm_led.duty_u16(duty_cycle)  # Set the LED brightness level.
        utime.sleep(0.01)  # Pause to observe the change.
    for duty_cycle in range(64511, 0, -1024):  # Decrease LED brightness.
        pwm_led.duty_u16(duty_cycle)  # Set the LED brightness level.
        utime.sleep(0.01)  # Pause to observe the change.

Count button presses

Each press adds one to the counter and prints it. The program waits for the release before counting again, so a press counts once however long it lasts.

import machine
import utime

button = machine.Pin(21, machine.Pin.IN, machine.Pin.PULL_DOWN)
counter = 0

while True:
    if button.value() == 0:
        counter += 1
        print("Counter:", counter)
        while button.value() == 0:
            pass  # Wait for the button to be released
        utime.sleep(0.1)

Read Qwiic sensor

The two Qwiic connectors share I²C on GP12 (SDA) and GP13 (SCL): a Qwiic sensor connects without soldering. This example reads the room temperature from a SparkFun TMP102 digital temperature sensor (Qwiic) every second.

import machine
import time

# Establishing the I2C connection. '0' corresponds to the I2C driver.
# The 'SDA' and 'SCL' parameters respectively specify the SDA and SCL pins.
# The 'freq' parameter determines the I2C communication speed.

i2c = machine.I2C(0, scl=machine.Pin(13), sda=machine.Pin(12), freq=100000)
tmp102_address = 0x48  # TMP102 I2C address (default set to 0x48).

# Temperature reading function
def read_temperature():
    data = i2c.readfrom(tmp102_address, 2)  # Read two bytes of data
    raw_temp = (data[0] << 8) | data[1]    # Get the raw data combined
    temperature = (raw_temp >> 4) * 0.0625 # Convert raw data to temperature value
    return temperature

while True:
    temperature = read_temperature()
    print("Temperature: {:.2f} °C".format(temperature)) # Print the temperature value
    time.sleep(1) # Slow down the loop by waiting 1 second

NeoPixel LED

The NeoPixel LED is on GP15. The examples drive a strip of 8 LEDs (NUM_LEDS = 8); the board’s own LED is the first of them.

Color cycle

Red, green and blue in turn, 100 ms each:

import machine
import neopixel
import utime

NUM_LEDS = 8
pin = machine.Pin(15)
np = neopixel.NeoPixel(pin, NUM_LEDS)

def color_cycle(wait):
    for i in range(NUM_LEDS):
        np[i] = (255, 0, 0)  # Set LED color to red
    np.write()
    utime.sleep_ms(wait)  # Wait for a short duration
    
    for i in range(NUM_LEDS):
        np[i] = (0, 255, 0)  # Set LED color to green
    np.write()
    utime.sleep_ms(wait)  # Wait for a short duration
    
    for i in range(NUM_LEDS):
        np[i] = (0, 0, 255)  # Set LED color to blue
    np.write()
    utime.sleep_ms(wait)  # Wait for a short duration

while True:
    color_cycle(100)  # Call the color cycle function with a delay of 100 milliseconds

Rainbow

A smooth rainbow: wheel() gives the color of each step, rainbow_cycle() moves it along the LEDs.

import machine
import neopixel
import utime

NUM_LEDS = 8
pin = machine.Pin(15)
np = neopixel.NeoPixel(pin, NUM_LEDS)

# Function for rainbow effect
def rainbow_cycle(wait):
    for j in range(255):
        for i in range(NUM_LEDS):
            rc_index = (i * 256 // NUM_LEDS) + j
            np[i] = wheel(rc_index & 255)  # Set LED color using wheel function
        np.write()
        utime.sleep_ms(wait)

def wheel(pos):
    if pos < 85:
        return (255 - pos * 3, pos * 3, 0)  # Red to Green transition
    elif pos < 170:
        pos -= 85
        return (0, 255 - pos * 3, pos * 3)  # Green to Blue transition
    else:
        pos -= 170
        return (pos * 3, 0, 255 - pos * 3)  # Blue to Red transition

while True:
    rainbow_cycle(20)  # Call the rainbow effect function with a delay of 20 milliseconds