LED Ring NeoPixel

From Sketching with Hardware at LMU Wiki
Jump to navigation Jump to search

Description

Text

Image(s)


How to connect it electrically

Text

Image(s)

How to control it in MicroPython

 1 import neopixel
 2 from machine import Pin
 3 
 4 # number of leds
 5 n=8
 6 # connected Pin
 7 p=0
 8 
 9 np = neopixel.NeoPixel(Pin(p),n)
10 
11 # sets color of the led at position 0 to (r,g,b)
12 np[0] = (255,0,0)
13 # colors the leds
14 np.write()

A small Program in MicroPython

 1 import neopixel
 2 from machine import Pin
 3 from time import sleep
 4 
 5 # number of leds
 6 n=8
 7 # connected Pin
 8 p=0
 9 
10 np = neopixel.NeoPixel(Pin(p),n)
11 
12 # colors the led at position in (r,g,b)
13 def color(position, r,g,b):
14   np[position]=(r,g,b)
15   np.write()
16 
17 # remove all colors
18 def clearAll():
19   for i in range(n):
20     color(i,0,0,0)
21 
22 # remove color from led at position
23 def clear(position):
24   color(position,0,0,0)
25 
26 
27 # the color (r,g,b) should run x times like a cycle 
28 def cycle(r,g,b,wait,x): 
29   # before setting new colors, clear the ring
30   clearAll()
31   
32   # the cycle should run x times
33   for j in range(x):
34     for i in range(n):
35       # clear the previous led
36       if(i==0):
37         clear(n-1)
38       else:
39         clear(i-1)
40       
41       # color the current led
42       color(i,r,g,b)
43       
44       # wait, color is displayed this time
45       sleep(wait)
46 
47 
48 cycle(0,255,255,0.5,4)

text

image(s)

Related Tutorial Videos

change to the right video


Background

text

image(s)