Simple test

Ensure your device works with this simple test.

examples/neopixel_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import time
 5import board
 6import neopixel
 7
 8
 9# On CircuitPlayground Express, and boards with built in status NeoPixel -> board.NEOPIXEL
10# Otherwise choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D1
11pixel_pin = board.NEOPIXEL
12
13# On a Raspberry pi, use this instead, not all pins are supported
14# pixel_pin = board.D18
15
16# The number of NeoPixels
17num_pixels = 10
18
19# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed!
20# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW.
21ORDER = neopixel.GRB
22
23pixels = neopixel.NeoPixel(
24    pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER
25)
26
27
28def wheel(pos):
29    # Input a value 0 to 255 to get a color value.
30    # The colours are a transition r - g - b - back to r.
31    if pos < 0 or pos > 255:
32        r = g = b = 0
33    elif pos < 85:
34        r = int(pos * 3)
35        g = int(255 - pos * 3)
36        b = 0
37    elif pos < 170:
38        pos -= 85
39        r = int(255 - pos * 3)
40        g = 0
41        b = int(pos * 3)
42    else:
43        pos -= 170
44        r = 0
45        g = int(pos * 3)
46        b = int(255 - pos * 3)
47    return (r, g, b) if ORDER in (neopixel.RGB, neopixel.GRB) else (r, g, b, 0)
48
49
50def rainbow_cycle(wait):
51    for j in range(255):
52        for i in range(num_pixels):
53            pixel_index = (i * 256 // num_pixels) + j
54            pixels[i] = wheel(pixel_index & 255)
55        pixels.show()
56        time.sleep(wait)
57
58
59while True:
60    # Comment this line out if you have RGBW/GRBW NeoPixels
61    pixels.fill((255, 0, 0))
62    # Uncomment this line if you have RGBW/GRBW NeoPixels
63    # pixels.fill((255, 0, 0, 0))
64    pixels.show()
65    time.sleep(1)
66
67    # Comment this line out if you have RGBW/GRBW NeoPixels
68    pixels.fill((0, 255, 0))
69    # Uncomment this line if you have RGBW/GRBW NeoPixels
70    # pixels.fill((0, 255, 0, 0))
71    pixels.show()
72    time.sleep(1)
73
74    # Comment this line out if you have RGBW/GRBW NeoPixels
75    pixels.fill((0, 0, 255))
76    # Uncomment this line if you have RGBW/GRBW NeoPixels
77    # pixels.fill((0, 0, 255, 0))
78    pixels.show()
79    time.sleep(1)
80
81    rainbow_cycle(0.001)  # rainbow cycle with 1ms delay per step
examples/neopixel_rpi_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4# Simple test for NeoPixels on Raspberry Pi
 5import time
 6import board
 7import neopixel
 8
 9
10# Choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D18
11# NeoPixels must be connected to D10, D12, D18 or D21 to work.
12pixel_pin = board.D18
13
14# The number of NeoPixels
15num_pixels = 30
16
17# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed!
18# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW.
19ORDER = neopixel.GRB
20
21pixels = neopixel.NeoPixel(
22    pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER
23)
24
25
26def wheel(pos):
27    # Input a value 0 to 255 to get a color value.
28    # The colours are a transition r - g - b - back to r.
29    if pos < 0 or pos > 255:
30        r = g = b = 0
31    elif pos < 85:
32        r = int(pos * 3)
33        g = int(255 - pos * 3)
34        b = 0
35    elif pos < 170:
36        pos -= 85
37        r = int(255 - pos * 3)
38        g = 0
39        b = int(pos * 3)
40    else:
41        pos -= 170
42        r = 0
43        g = int(pos * 3)
44        b = int(255 - pos * 3)
45    return (r, g, b) if ORDER in (neopixel.RGB, neopixel.GRB) else (r, g, b, 0)
46
47
48def rainbow_cycle(wait):
49    for j in range(255):
50        for i in range(num_pixels):
51            pixel_index = (i * 256 // num_pixels) + j
52            pixels[i] = wheel(pixel_index & 255)
53        pixels.show()
54        time.sleep(wait)
55
56
57while True:
58    # Comment this line out if you have RGBW/GRBW NeoPixels
59    pixels.fill((255, 0, 0))
60    # Uncomment this line if you have RGBW/GRBW NeoPixels
61    # pixels.fill((255, 0, 0, 0))
62    pixels.show()
63    time.sleep(1)
64
65    # Comment this line out if you have RGBW/GRBW NeoPixels
66    pixels.fill((0, 255, 0))
67    # Uncomment this line if you have RGBW/GRBW NeoPixels
68    # pixels.fill((0, 255, 0, 0))
69    pixels.show()
70    time.sleep(1)
71
72    # Comment this line out if you have RGBW/GRBW NeoPixels
73    pixels.fill((0, 0, 255))
74    # Uncomment this line if you have RGBW/GRBW NeoPixels
75    # pixels.fill((0, 0, 255, 0))
76    pixels.show()
77    time.sleep(1)
78
79    rainbow_cycle(0.001)  # rainbow cycle with 1ms delay per step