Neopixel Ring and Raspberry Pi 4

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
bobster316
 
Posts: 52
Joined: Tue Nov 24, 2020 10:32 am

Neopixel Ring and Raspberry Pi 4

Post by bobster316 »

I am struggling to get my raspberry pi 4 working with a neopixel ring.
I am still a novice and need some gquidane on how to get the neopixel ring working.
I have the neopixel connected to the following GPIO pins on the Pi
5v
Ground
GPIO 18

I am running the latest raspberry pi os 32 bit
Appreciate if you can help to provide instruction on how to setup
I want to be able to run the strandtest,py which is normally located in the
example sub folder of the rpi_ws281x_python

Thanks

User avatar
bidrohini
 
Posts: 202
Joined: Thu Oct 20, 2022 10:03 am

Re: Neopixel Ring and Raspberry Pi 4

Post by bidrohini »

This one uses a neopixel ring with an older version of the Raspberry Pi. But I think you'll still find many similarities with 4B: https://www.youtube.com/watch?v=0uXjyvZ9JGM

User avatar
bobster316
 
Posts: 52
Joined: Tue Nov 24, 2020 10:32 am

Re: Neopixel Ring and Raspberry Pi 4

Post by bobster316 »

Thanks for the information, when i check the links in the video description, none of them work.

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: Neopixel Ring and Raspberry Pi 4

Post by adafruit_support_carter »

Have you gone through the basic software setup?
https://learn.adafruit.com/neopixels-on ... i/overview

User avatar
bobster316
 
Posts: 52
Joined: Tue Nov 24, 2020 10:32 am

Re: Neopixel Ring and Raspberry Pi 4

Post by bobster316 »

Hi there

Yes I have gone throught the instructions, the issue I am facing is the neopixel ring
starts displaying pattern on boot of the raspberry Pi buth then stops pattern after
approx 3 seconds.

I have the script starting on boot the the pi using crontab -e
@reboot sudo python3 /home/pi/strandtest.py &

The contents of strandtest.py

Code: Select all

#!/usr/bin/env python3
# NeoPixel library strandtest example
# Author: Tony DiCola ([email protected])
#
# Direct port of the Arduino NeoPixel library strandtest example.  Showcases
# various animations on a strip of NeoPixels.

import time
from rpi_ws281x import PixelStrip, Color
import argparse

# LED strip configuration:
LED_COUNT = 12        # Number of LED pixels.
LED_PIN = 18          # GPIO pin connected to the pixels (18 uses PWM!).
# LED_PIN = 10        # GPIO pin connected to the pixels (10 uses SPI /dev/spide
v0.0).
LED_FREQ_HZ = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA = 10          # DMA channel to use for generating signal (try 10)
LED_BRIGHTNESS = 255  # Set to 0 for darkest and 255 for brightest
LED_INVERT = False    # True to invert the signal (when using NPN transistor lev
el shift)
LED_CHANNEL = 0       # set to '1' for GPIOs 13, 19, 41, 45 or 53


# Define functions which animate LEDs in various ways.
def colorWipe(strip, color, wait_ms=50):
    """Wipe color across display a pixel at a time."""
    for i in range(strip.numPixels()):
        strip.setPixelColor(i, color)
        strip.show()
        time.sleep(wait_ms / 1000.0)


def theaterChase(strip, color, wait_ms=50, iterations=10):
    """Movie theater light style chaser animation."""
    for j in range(iterations):
        for q in range(3):
            for i in range(0, strip.numPixels(), 3):
                strip.setPixelColor(i + q, color)
            strip.show()
            time.sleep(wait_ms / 1000.0)
            for i in range(0, strip.numPixels(), 3):
                strip.setPixelColor(i + q, 0)


def wheel(pos):
    """Generate rainbow colors across 0-255 positions."""
    if pos < 85:
        return Color(pos * 3, 255 - pos * 3, 0)
    elif pos < 170:
        pos -= 85
        return Color(255 - pos * 3, 0, pos * 3)
    else:
        pos -= 170
        return Color(0, pos * 3, 255 - pos * 3)


def rainbow(strip, wait_ms=20, iterations=1):
    """Draw rainbow that fades across all pixels at once."""
    for j in range(256 * iterations):
        for i in range(strip.numPixels()):
            strip.setPixelColor(i, wheel((i + j) & 255))
        strip.show()
        time.sleep(wait_ms / 1000.0)


def rainbowCycle(strip, wait_ms=20, iterations=5):
    """Draw rainbow that uniformly distributes itself across all pixels."""
    for j in range(256 * iterations):
        for i in range(strip.numPixels()):
            strip.setPixelColor(i, wheel(
                (int(i * 256 / strip.numPixels()) + j) & 255))
        strip.show()
        time.sleep(wait_ms / 1000.0)


def theaterChaseRainbow(strip, wait_ms=50):
    """Rainbow movie theater light style chaser animation."""
    for j in range(256):
        for q in range(3):
            for i in range(0, strip.numPixels(), 3):
                strip.setPixelColor(i + q, wheel((i + j) % 255))
            strip.show()
            time.sleep(wait_ms / 1000.0)
            for i in range(0, strip.numPixels(), 3):
                strip.setPixelColor(i + q, 0)


# Main program logic follows:
if __name__ == '__main__':
    # Process arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('-c', '--clear', action='store_true', help='clear the di
splay on exit')
    args = parser.parse_args()

    # Create NeoPixel object with appropriate configuration.
    strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED
_BRIGHTNESS, LED_CHANNEL)
    # Intialize the library (must be called once before other functions).
    strip.begin()

    try:

        while True:
            colorWipe(strip, Color(255, 0, 0))  # Red wipe
            colorWipe(strip, Color(0, 255, 0))  # Green wipe
            colorWipe(strip, Color(0, 0, 255))  # Blue wipe
            theaterChase(strip, Color(127, 127, 127))  # White theater chase
            theaterChase(strip, Color(127, 0, 0))  # Red theater chase
            theaterChase(strip, Color(0, 0, 127))  # Blue theater chase
            rainbow(strip)
            rainbowCycle(strip)
            theaterChaseRainbow(strip)

    except KeyboardInterrupt:
        if args.clear:
            colorWipe(strip, Color(0, 0, 0), 10)
 

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: Neopixel Ring and Raspberry Pi 4

Post by adafruit_support_carter »

Try running the script directly (instead of via cron) and see if that helps.

Also post a photo of your setup showing how everything is connected.

User avatar
bobster316
 
Posts: 52
Joined: Tue Nov 24, 2020 10:32 am

Re: Neopixel Ring and Raspberry Pi 4

Post by bobster316 »

I can run the strandtest.py manually and it works normally and does not freeze,
using sudo python3 strandtest.py

The issue I am having is to get this to work automatically when the PI boots up
and for the strandtest.py to keep running in the background wuthout freezing a
few seconds after the pi boots up.

Thanks

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: Neopixel Ring and Raspberry Pi 4

Post by adafruit_support_carter »

Getting things to run via cron can sometimes be confusing. Main sources of issue are accessing software/lib available only for different accounts and dealing with paths. For you it's running for several seconds, so must be working in general.

Is the process still running? After it runs for a few seconds and stops, try running this at a command line:

Code: Select all

ps -ef | grep strandtest
and see if anything is found.

User avatar
bobster316
 
Posts: 52
Joined: Tue Nov 24, 2020 10:32 am

Re: Neopixel Ring and Raspberry Pi 4

Post by bobster316 »

Please see below output from command

pi@raspberrypi:~ $ ps -ef | grep strandtest
root 451 1 0 16:41 ? 00:00:00 sudo python3 /home/pi/strandtest.py
root 456 413 0 16:41 ? 00:00:00 /bin/sh -c sudo python3 /home/pi/strandtest.py
root 471 456 0 16:41 ? 00:00:00 sudo python3 /home/pi/strandtest.py
root 487 451 2 16:41 ? 00:00:01 python3 /home/pi/strandtest.py
root 490 471 2 16:41 ? 00:00:01 python3 /home/pi/strandtest.py
pi 1271 1257 0 16:42 pts/0 00:00:00 grep --color=auto strandtest

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: Neopixel Ring and Raspberry Pi 4

Post by adafruit_support_carter »

Weird. There are numerous processes running. Not sure what spawned those, but they could be fighting each other for access to the NeoPixel pin.

Try rebooting and then running that command again. Don't attempt any other running of the strandtest code. Just let the cron one run for the several seconds. After it "freezes", then run the ps command and see what you get.

User avatar
bobster316
 
Posts: 52
Joined: Tue Nov 24, 2020 10:32 am

Re: Neopixel Ring and Raspberry Pi 4

Post by bobster316 »

I have started again with a new micro sd and raspberry pi os 32 bit

Ran the following scripts

Code: Select all

sudo pip3 install rpi_ws281x adafruit-circuitpython-neopixel
sudo python3 -m pip install --force-reinstall adafruit-blinka scripts
created a python file rainbow.py
with the example and getthe same issue
works fine if run directly

when i check

Code: Select all

pi@raspberrypi:~ $ ps -ef | grep strandtest
pi        1411  1222  0 18:45 pts/0    00:00:00 grep --color=auto strandtest

Code: Select all

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# Simple test for NeoPixels on Raspberry Pi
import time
import board
import neopixel


# Choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D18
# NeoPixels must be connected to D10, D12, D18 or D21 to work.
pixel_pin = board.D18

# The number of NeoPixels
num_pixels = 30

# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed!
# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW.
ORDER = neopixel.GRB

pixels = neopixel.NeoPixel(
    pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER
)


def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if pos < 0 or pos > 255:
        r = g = b = 0
    elif pos < 85:
        r = int(pos * 3)
        g = int(255 - pos * 3)
        b = 0
    elif pos < 170:
        pos -= 85
        r = int(255 - pos * 3)
        g = 0
        b = int(pos * 3)
    else:
        pos -= 170
        r = 0
        g = int(pos * 3)
        b = int(255 - pos * 3)
    return (r, g, b) if ORDER in (neopixel.RGB, neopixel.GRB) else (r, g, b, 0)


def rainbow_cycle(wait):
    for j in range(255):
        for i in range(num_pixels):
            pixel_index = (i * 256 // num_pixels) + j
            pixels[i] = wheel(pixel_index & 255)
        pixels.show()
        time.sleep(wait)


while True:
    # Comment this line out if you have RGBW/GRBW NeoPixels
    pixels.fill((255, 0, 0))
    # Uncomment this line if you have RGBW/GRBW NeoPixels
    # pixels.fill((255, 0, 0, 0))
    pixels.show()
    time.sleep(1)

    # Comment this line out if you have RGBW/GRBW NeoPixels
    pixels.fill((0, 255, 0))
    # Uncomment this line if you have RGBW/GRBW NeoPixels
    # pixels.fill((0, 255, 0, 0))
    pixels.show()
    time.sleep(1)

    # Comment this line out if you have RGBW/GRBW NeoPixels
    pixels.fill((0, 0, 255))
    # Uncomment this line if you have RGBW/GRBW NeoPixels
    # pixels.fill((0, 0, 255, 0))
    pixels.show()
    time.sleep(1)

    rainbow_cycle(0.001)  # rainbow cycle with 1ms delay per step
 

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: Neopixel Ring and Raspberry Pi 4

Post by adafruit_support_carter »

OK. That all looks fine. And if you put back in the cron entry and reboot, what does

Code: Select all

ps -ef | grep strandtest
show?

User avatar
bobster316
 
Posts: 52
Joined: Tue Nov 24, 2020 10:32 am

Re: Neopixel Ring and Raspberry Pi 4

Post by bobster316 »

ok, i tried crontab -e
@reboot python3 /home/pi/rainbow.py
this did not work at all, no leds working

Then tried sudo crontab -e
same issue as before the led pattern starts for a 3 seconds and then stops.

When i check
pi@raspberrypi:~ $ ps -ef | grep rainbow.py
root 417 1 8 08:47 ? 00:00:21 python3 /home/pi/rainbow.py
pi 1599 1572 0 08:51 pts/0 00:00:00 grep --color=auto rainbow.py

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: Neopixel Ring and Raspberry Pi 4

Post by adafruit_support_carter »

Note sure. The process is still showing as running even after it stops after 3 seconds:

Code: Select all

 root 417 1 8 08:47 ? 00:00:21 python3 /home/pi/rainbow.py
Could try setting up a systemd service instead of relying on the cron @reboot behavior.
https://learn.adafruit.com/running-prog ... y-computer

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”