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