Difference between revisions of "LED Ring NeoPixel"

From Sketching with Hardware at LMU Wiki
Jump to navigation Jump to search
Line 30: Line 30:
 
= A small Program in MicroPython =
 
= A small Program in MicroPython =
 
<syntaxhighlight lang="python" line='line'>
 
<syntaxhighlight lang="python" line='line'>
# todo
+
import neopixel
# code goes here
+
from machine import Pin
 +
from time import sleep
 +
 
 +
# number of leds
 +
n=8
 +
# connected Pin
 +
p=0
 +
 
 +
np = neopixel.NeoPixel(Pin(p),n)
 +
 
 +
# colors the led at position in (r,g,b)
 +
def color(position, r,g,b):
 +
  np[position]=(r,g,b)
 +
  np.write()
 +
 
 +
# remove all colors
 +
def clearAll():
 +
  for i in range(n):
 +
    color(i,0,0,0)
 +
 
 +
# remove color from led at position
 +
def clear(position):
 +
  color(position,0,0,0)
 +
 
 +
 
 +
# the color (r,g,b) should run x times like a cycle
 +
def cycle(r,g,b,wait,x):
 +
  # before setting new colors, clear the ring
 +
  clearAll()
 +
 
 +
  # the cycle should run x times
 +
  for j in range(x):
 +
    for i in range(n):
 +
      # clear the previous led
 +
      if(i==0):
 +
        clear(n-1)
 +
      else:
 +
        clear(i-1)
 +
     
 +
      # color the current led
 +
      color(i,r,g,b)
 +
     
 +
      # wait, color is displayed this time
 +
      sleep(wait)
 +
 
 +
 
 +
cycle(0,255,255,0.5,4)
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 12:30, 19 August 2020

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)