#QT Py RP 2040 - CircuitPython code only running when connected to PC; otherwise red LED blinks twice

1 messages · Page 1 of 1 (latest)

limpid sandal
#

I have a QT Py RP2040 with CircuitPython. I wrote the code with Mu. There is a Neopixel strip connected via pin A0 and GND and a Neokey 1x4 via STEMMA QT.

The Neopixel strip is connected to an external 5V power source.

When I connect the QT Py to my PC's USB the code runs automatically (independent from running Mu), everything works fine.
When I connect the QT Py to an external power source (either via USB or via the 5V pin with a diod) the onboard LED blinks twice every couple of seconds and the code does not run...

I don't understand what the difference is and why it doesn't work when not connected to my PC. The code is stored on the QT Py as code.py.

Resetting doesn't solve the issue either.

I don't know what to do/change - anyone had similar issues and fixed them successfully?

Thank you!

stable beacon
#

Link to a "gist" with the code? Hard to say without knowing what the code does

twin matrix
#

you can upload your code with the (+) button

stable beacon
#

Oh right

twin matrix
#

the LED blinking red twice every 5 seconds means the code threw an exception

#

it's possible there's something in the code that requires USB

limpid sandal
#

import time
import board
import busio
from adafruit_neokey.neokey1x4 import NeoKey1x4
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

import sys
import neopixel
import random
from time import sleep

######################### PARAMETERS FOR THE QT PY AND NEOKEY #########################

# use STEMMA I2C bus on RP2040 QT Py
i2c_bus = busio.I2C(board.SCL1, board.SDA1)

# Create a NeoKey object
neokey = NeoKey1x4(i2c_bus, addr=0x30)

#  create a keyboard object
keyboard = Keyboard(usb_hid.devices)

#  states for key presses
key_0_state = False
key_1_state = False
key_2_state = False
key_3_state = False

# colors of keys when pressed or off
key_0_color = 0x00FFFF # cyan
key_1_color = 0xFF00FF # magenta
key_2_color = 0xFFFFFF # white
key_3_color = 0xFFFF00 # yellow
key_color_off = 0x0

led_off = (0,0,0) #RGB
led_cyan = (0,255,255)
led_magenta = (255, 0, 255)
led_white = (255,255,255)
led_yellow = (255,255,0)

# turn on the on-board led while the code is running
qtpy_led = neopixel.NeoPixel(board.NEOPIXEL, 1)
qtpy_led.fill(led_white)

start_loop = True

######################### PARAMETERS FOR THE NEOPIXELS #########################

# basic parameters
no_leds = 50
gpio_pin = board.A0
led_br = 1

letters_all = ["","","H","","G","","F","E","","D","C","","B","A","","","I","J","","K","L","","M","N","","O","P","","","","Q","Z","","","","","Y","X","","W","V","U","","T","S","","R","","",""]

leds_color_code = []
leds_color_code.extend(range(no_leds))

# set the initial color palette for leds_color_code
for i in range(0, no_leds,4):
    if i < no_leds-0: leds_color_code[i+0] = led_cyan
    if i < no_leds-1: leds_color_code[i+1] = led_magenta
    if i < no_leds-2: leds_color_code[i+2] = led_white
    if i < no_leds-3: leds_color_code[i+3] = led_yellow

# initialization of the led strip
pixels = neopixel.NeoPixel(gpio_pin, no_leds)
pixels.write()
limpid sandal
twin matrix
#

yeah see, you are setting up USB HID

#

that requires being connected via USB

#

if you don't use it, remove it

#

if later you want to use HID when connected to a PC but not when disconnected, you can have a test at the start to run the whole HID code only if connected to a PC, by testing if supervisor.runtime.usb_connected:

limpid sandal
# twin matrix yeah see, you are setting up USB HID

this was it. it is running now. thank you so much, this was incredibly helpful!
I copy/pasted part of my code from an example code that I found online and didn't realize that it needed USB.

again, thank you! my project is now finished 🙂

twin matrix
#

yeah HID is for simulating a keyboard or mouse