Once you've finished setting up your QT Py RP2040 with CircuitPython, you can access the code and necessary libraries by downloading the Project Bundle.

To do this, click on the Download Project Bundle button in the window below. It will download as a zipped folder.

# SPDX-FileCopyrightText: 2022 Noe Ruiz for Adafruit Industries
# SPDX-License-Identifier: MIT
# Adafruit nOOds lantern with "analog" (PWM) brightness control using GPIO.
# Uses 6 nOOds, anode (+) to GPIO pin, cathode (-) to ground.
# A current-limiting resistor (e.g. 10 Ohm) can go at either end.

import math
import time
import board
import pwmio
from digitalio import DigitalInOut, Direction, Pull

PINS = (board.SCK, board.MOSI, board.A1, board.A3, board.MISO, board.A2) # List of pins
GAMMA = 2.6  # For perceptually-linear brightness

# Convert pin number list to PWMOut object list
pin_list = [pwmio.PWMOut(pin, frequency=1000, duty_cycle=0) for pin in PINS]

# Button switch set up
switch = DigitalInOut(board.A0)
switch.direction = Direction.INPUT
switch.pull = Pull.UP

# LED set up
led = DigitalInOut(board.TX)
led.direction = Direction.OUTPUT

while True: # Repeat forever...
    # If the button is pressed turn on LED n00ds
    if switch.value:
        for i, pin in enumerate(pin_list): # For each pin...
            # Calc sine wave, phase offset for each pin, with gamma correction.
            # If using red, green, blue nOOds, you'll get a cycle of hues.
            phase = (time.monotonic() - 2 * i / len(PINS)) * math.pi
            brightness = int((math.sin(phase) + 1.0) * 0.5 ** GAMMA * 65535 + 0.5)
            pin.duty_cycle = brightness
        led.value = True # Turn button LED on
    else: # Otherwise turn LED n00ds off
        for i, pin in enumerate(pin_list):
            pin.duty_cycle = 0
        led.value = False # Turn button LED off

    time.sleep(.01)

Upload the Code and Libraries to the QT Py RP2040

After downloading the Project Bundle, plug your QT Py into the computer's USB port with a known good USB data+power cable. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called CIRCUITPY. Unzip the folder and copy the following items to the QT Py RP20402's CIRCUITPY drive. 

  • code.py

Your QT Py RP2040 CIRCUITPY drive should look like this after copying the code.py file.

This guide was first published on Oct 05, 2022. It was last updated on Jun 12, 2024.

This page (Code the LED Noodle Lantern) was last updated on Jun 12, 2024.

Text editor powered by tinymce.