Created
June 25, 2026 14:54
-
-
Save lesp/16157c4346b8129eef8e84eca1ff7fca to your computer and use it in GitHub Desktop.
Control 50 RGB LEDs using a Qw/ST I2C control pad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| from machine import I2C | |
| from qwstpad import ADDRESSES, QwSTPad | |
| import plasma | |
| from random import randint | |
| # Setup the LED strip | |
| NUM_LEDS = 50 | |
| led_strip = plasma.WS2812( | |
| NUM_LEDS, color_order=plasma.COLOR_ORDER_RGB | |
| ) | |
| led_strip.start() | |
| """ | |
| How to read all of the buttons on QwSTPad. | |
| """ | |
| # Constants | |
| I2C_PINS = {"id": 0, "sda": 20, "scl": 21} # The I2C pins the QwSTPad is connected to | |
| I2C_ADDRESS = ADDRESSES[0] # The I2C address of the connected QwSTPad | |
| SLEEP = 0.1 # The time between each reading of the buttons | |
| # Attempt to create the I2C instance and pass that to the QwSTPad | |
| try: | |
| qwstpad = QwSTPad(I2C(**I2C_PINS), I2C_ADDRESS) | |
| except OSError: | |
| print("QwSTPad: Not Connected ... Exiting") | |
| raise SystemExit | |
| print("QwSTPad: Connected ... Starting") | |
| # Wrap the code in a try block, to catch any exceptions (including KeyboardInterrupt) | |
| try: | |
| # Loop forever | |
| while True: | |
| # Read all the buttons from the qwstpad and print them out | |
| buttons = qwstpad.read_buttons() | |
| for key, value in buttons.items(): | |
| if key == "U" and value == 1: | |
| print("RED") | |
| for i in range(NUM_LEDS): | |
| led_strip.set_rgb(i, 255, 0, 0) | |
| elif key == "D" and value == 1: | |
| print("GREEN") | |
| for i in range(NUM_LEDS): | |
| led_strip.set_rgb(i, 0, 255, 0) | |
| elif key == "L" and value == 1: | |
| print("BLUE") | |
| for i in range(NUM_LEDS): | |
| led_strip.set_rgb(i, 0, 0, 255) | |
| elif key == "R" and value == 1: | |
| print("DISCO") | |
| r = randint(0,254) | |
| g = randint(0,254) | |
| b = randint(0,254) | |
| for i in range(NUM_LEDS): | |
| led_strip.set_rgb(i, r,g,b) | |
| else: | |
| pass | |
| print() | |
| time.sleep(SLEEP) | |
| # Handle the QwSTPad being disconnected unexpectedly | |
| except OSError: | |
| print("QwSTPad: Disconnected .. Exiting") | |
| qwstpad = None | |
| # Turn off all four LEDs if there is still a QwSTPad | |
| finally: | |
| if qwstpad is not None: | |
| qwstpad.clear_leds() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment