Search code examples
pythonraspberry-pi-picoadafruit-circuitpythonneopixeladafruit-circuitpython-neopixel

Adafruit neopixel library not loading correctly onto raspberry pi pico


I've been trying to use the adafruit neopixel library to... well... use neopixels. However, I've been having some problems. I am using a pi pico, thonny as my IDE, and Ubuntu as my OS.

I started off writing the code below, and running it, on circuitpython 8.1.0. I downloaded the neopixel.mpy file from the bundle, and I got the error: Corrupted .mpy file. Upon closer inspection, the neopixel.mpy file is 0 bytes large. Ah! I try getting the file on the raspberry pi pico H board in various different ways, but every time, I either get the error above, or "module" has no attribute Neopixel when I try pixels = neopixel.Neopixel(board.GP0, 20).

I then tried a different pi pico board (this one was not H, it was just standard), and, tried loading on the program and .mpy files using the system file manager instead of the one built in to thonny. This time, the neopixel.mpy file was 1.3k (So the system file manager actually loads some of the file on.). I got the error "module" has no attribute Neopixel again. I have checked my code against the adafruit examples, and nothing seems to be wrong. Here's my code: I consists of three scripts - an animation base class, an animation class called ColourChase and a main file that runs animation classes in sequence.

The animation base class van_animation_base_class.py:

class Animation(object):
    def __init__(self, neopixels, num_pixels):
        self.num_pixels = num_pixels
        self.neopixels = neopixels
        self.counter = 0
    
    def next_frame(self):
        pass

The ColourChase class, colourchase.py:

#

from van_animation_base_class import Animation
import time

class ColourChase(Animation):
    def __init__(self, neopixels, num_pixels):
        super().__init__(neopixels, num_pixels)
        self.colours = [(225, 0, 0), (0, 225, 0), (0, 0, 225)]
        self.current_col = 0
        self.counter += 1
    
    def next_frame(self):
        if self.counter > self.num_pixels:
            self.counter = 1
            self.current_col += 1
            if self.current_col > len(self.colours)-1:
                self.current_col = 0
        
        self.neopixels[self.counter-1] = self.colours[self.current_col]
        print(self.neopixels)
        self.counter += 1
        self.neopixels.show()
        time.sleep(0.5)

And finally, the main file, vanprojmain.py:

import board
from digitalio import *
import time
import neopixel

# import animations
from colourchase import ColourChase

# init neopixels and button
num_pixels = 20
pixels = neopixel.Neopixel(board.GP0, num_pixels)
pixels.brightness = 0.5

button = digitalio.DigitalInOut(board.GP1)
button.direction = board.INPUT

# make an instance of each animaton
animations = [
        ColourChase(pixels, num_pixels)
    ]
num = 0

def button_is_pressed():
    return button.value


while True:
    if button_is_pressed():
        num += 1
        if num > len(animations - 1):
            num = 0
        time.sleep(1)
    animations[num].next_frame()

Thanks, ValleyDragon888.


Solution

  • The module defined in "neopixel.py" or "neopixel.mpy" has a camel case name, so change your pixels instantiation from

    pixels = neopixel.Neopixel(board.GP0, num_pixels)
    

    to

    pixels = neopixel.NeoPixel(board.GP0, num_pixels)
    

    (note the capital 'P').

    But I still can't explain why the library was 0 Bytes in the first place. With your second pico the library was already correct, i.e. 1313 bytes in size.