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
RPI simple test
Test using the Raspberry Pi.
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
Pixel
Example of setting the color of a single pixel.
examples/neopixel_pixel.py
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# This example shows how to create a single pixel with a specific color channel
5# order and blink it.
6# Most NeoPixels = neopixel.GRB or neopixel.GRBW
7# The 8mm Diffused NeoPixel (PID 1734) = neopixel.RGB
8import time
9
10import board
11
12import neopixel
13
14# Configure the setup
15PIXEL_PIN = board.D1 # pin that the NeoPixel is connected to
16ORDER = neopixel.RGB # pixel color channel order
17COLOR = (100, 50, 150) # color to blink
18CLEAR = (0, 0, 0) # clear (or second color)
19DELAY = 0.25 # blink rate in seconds
20
21# Create the NeoPixel object
22pixel = neopixel.NeoPixel(PIXEL_PIN, 1, pixel_order=ORDER)
23
24# Loop forever and blink the color
25while True:
26 pixel[0] = COLOR
27 time.sleep(DELAY)
28 pixel[0] = CLEAR
29 time.sleep(DELAY)
Rainbow
Example of cycling through the colors of the rainbow.
examples/neopixel_rainbowio_simpletest.py
1# SPDX-FileCopyrightText: 2022 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5
6import board
7from rainbowio import colorwheel
8
9import neopixel
10
11NUMPIXELS = 12 # Update this to match the number of LEDs.
12SPEED = 0.05 # Increase to slow down the rainbow. Decrease to speed it up.
13BRIGHTNESS = 0.2 # A number between 0.0 and 1.0, where 0.0 is off, and 1.0 is max.
14PIN = board.A3 # This is the default pin on the 5x5 NeoPixel Grid BFF.
15
16pixels = neopixel.NeoPixel(PIN, NUMPIXELS, brightness=BRIGHTNESS, auto_write=False)
17
18
19def rainbow_cycle(wait):
20 for color in range(255):
21 for pixel in range(len(pixels)):
22 pixel_index = (pixel * 256 // len(pixels)) + color * 5
23 pixels[pixel] = colorwheel(pixel_index & 255)
24 pixels.show()
25 time.sleep(wait)
26
27
28while True:
29 rainbow_cycle(SPEED)
Bouncing ball
Example of a bouncing ball animation.
examples/neopixel_bouncing_ball.py
1# SPDX-FileCopyrightText: 2025 Jose D. Montoya
2# SPDX-License-Identifier: MIT
3
4# This example simulates a ball bouncing on a NeoPixel strip affected by gravity.
5# Most NeoPixels = neopixel.GRB or neopixel.GRBW
6# The 8mm Diffused NeoPixel (PID 1734) = neopixel.RGB
7import time
8from math import ceil
9
10import board
11
12import neopixel
13
14# Configure the setup
15PIXEL_PIN = board.A3 # pin that the NeoPixel is connected to
16ORDER = neopixel.RGB # pixel color channel order
17COLOR = (255, 50, 150) # color to blink
18CLEAR = (0, 0, 0) # clear (or second color)
19DELAY = 0.1 # blink rate in seconds
20
21# Simulation Values.
22gravity = 0.5
23velocity = 2
24energy_loss = 0.6
25
26# Create the NeoPixel object
27num_pixels = 60
28pixel_seg = neopixel.NeoPixel(PIXEL_PIN, num_pixels, pixel_order=ORDER)
29
30# Animation start values
31travel = 0
32going_up = False
33floor_count = 0
34top = 0
35
36# Loop forever and simulate
37while True:
38 # Blink the color
39 pixel_seg[travel] = COLOR
40 time.sleep(0.05)
41 pixel_seg[travel] = CLEAR
42 time.sleep(DELAY)
43 velocity += gravity
44
45 # Check if the ball is at the top to change direction
46 if velocity >= 0 and going_up:
47 velocity = ceil(-velocity)
48 going_up = False
49 top = travel
50
51 # Check if the ball is at the bottom to break the loop
52 if top == num_pixels - 1:
53 floor_count += 1
54 if floor_count == 3:
55 break
56
57 travel = int(travel + velocity)
58
59 # Check if the ball is at the floor to change direction
60 if travel >= num_pixels:
61 travel = num_pixels - 1
62 velocity = ceil(-velocity * energy_loss)
63 going_up = True
64
65# Clear the strip
66pixel_seg.fill(0)