LED Lichterkette für den Weihnachtsbaum


Ich habe seit einigen Jahren eine LED Lichterkette, die früher mal mein Zimmer beleuchtet hat und jetzt eigentlich nur noch rumliegt. Um Weihnachten rum kann sie allerdings perfekt einen Baum beleuchten.

Ein Vorteil gegenüber üblichen Lichterketten ist, das man sie programmieren kann. Allerdings wird mit Strom gearbeitet, deswegen ist Vorsicht geboten! Das Endergebnis ist aber ziemlich schön.

Unten ist ein Bild von der fertigen Konstruktion. Darunter befindet sich der Code, der meine Lichterkette betreibt.

Voraussetzungen

  • LED Lichterkette (bei mir mit WS2801 Chips, die die LEDs steuern. Andere Chips müssen anders angesteuert werden.)
  • Ein Netzteil 5V/10A
  • Ein Steckerverbinder
  • Ein Raspberry Pi
  • ggf. ein Verlängerungskabel und ein Breadboard

Aufbau

LEDRaspberryNetzteil
GNDPin 6/GNDGND
SIPin 19/GPIO 10-
CLKPIN 23/GPIO 11-
5V-5V

Bild

Der fertige Baum. Frohe Weihnachten!
Der fertige Baum. Frohe Weihnachten!

Code

Der Code fällt unter works for me! Ich habe das Ganze vor mittlerweile gut 5 Jahren geschrieben und seitdem nichts daran geändert.

Er läuft auf Python 2.7 mit Adafruit-GPIO 1.0.3 und Adafruit-WS2801 1.0.1, eventuell muss deshalb etwas angepasst werden.

Teile des Codes wurden von tutorials-raspberrypi.de übernommen.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
from __future__ import print_function

# Simple demo of of the WS2801/SPI-like addressable RGB LED lights.
import time
import math
import RPi.GPIO as GPIO
 
# Import the WS2801 module.
import Adafruit_WS2801
import Adafruit_GPIO.SPI as SPI
 
# Configure the count of pixels:
PIXEL_COUNT = 160
 
# Alternatively specify a hardware SPI connection on /dev/spidev0.0:
SPI_PORT   = 0
SPI_DEVICE = 0
pixels = Adafruit_WS2801.WS2801Pixels(PIXEL_COUNT, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE), gpio=GPIO)
 
white = (255, 255, 255)
green = (0, 0, 255)
red = (255, 0, 0)
 
# Define the wheel function to interpolate between different hues.
def wheel(pos):
    if pos < 85:
        return Adafruit_WS2801.RGB_to_color(pos * 3, 255 - pos * 3, 0)
    elif pos < 170:
        pos -= 85
        return Adafruit_WS2801.RGB_to_color(255 - pos * 3, 0, pos * 3)
    else:
        pos -= 170
        return Adafruit_WS2801.RGB_to_color(0, pos * 3, 255 - pos * 3)
 
# Define rainbow cycle function to do a cycle of all hues.
def rainbow_cycle_successive(pixels, wait=0.1):
    for i in range(pixels.count()):
        pixels.set_pixel(i, wheel(((i * 256 // pixels.count())) % 256))
        pixels.show()
        if wait > 0:
            time.sleep(wait)
 
def rainbow_cycle(pixels, wait=0.05):
    for j in range(256):
        for i in range(pixels.count()):
            pixels.set_pixel(i, wheel(((i * 256 // pixels.count()) + j) % 256))
        pixels.show()
        if wait > 0:
            time.sleep(wait)
 
def rainbow_colors(pixels, wait=0.05):
    for j in range(256):
        for i in range(pixels.count()):
            pixels.set_pixel(i, wheel(((256 // pixels.count() + j)) % 256))
        pixels.show()
        if wait > 0:
            time.sleep(wait)
 
def brightness_decrease(pixels, wait=0.01, step=3):
    for j in range(int(256 // step)):
        rSum = 0
        gSum = 0
        bSum = 0
        for i in range(pixels.count()):
            r, g, b = pixels.get_pixel_rgb(i)
            r = int(max(0, r - step))
            g = int(max(0, g - step))
            b = int(max(0, b - step))
            rSum += r
            gSum += g
            bSum += b
            pixels.set_pixel(i, Adafruit_WS2801.RGB_to_color(r, g, b))
        pixels.show()
        if rSum == 0 and bSum == 0 and gSum == 0:
            return
        if wait > 0:
            time.sleep(wait)

def brightness_fast_decrease(pixels, t=16, step=32):
    for j in range(step):
        rSum = 0
        gSum = 0
        bSum = 0
        for i in range(pixels.count()):
            frac = float(j)/float(step)
            r, g, b = pixels.get_pixel_rgb(i)
            r = int(max(0, r - math.ceil(r*frac)))
            g = int(max(0, g - math.ceil(g*frac)))
            b = int(max(0, b - math.ceil(b*frac)))
            rSum += r
            gSum += g
            bSum += b
            pixels.set_pixel(i, Adafruit_WS2801.RGB_to_color(r, g, b))
        pixels.show()
        if rSum == 0 and bSum == 0 and gSum == 0:
            return
        time.sleep(t / step)

def brightness_increase(pixels, wait=0.01, step=3):
    for j in range(int(256 // step)):
        for i in range(pixels.count()):
            r, g, b = pixels.get_pixel_rgb(i)
            if r > 0:
                r = int(min(255, r + step))
            if g > 0:
                g = int(min(255, g + step))
            if b > 0:
                b = int(min(255, b + step))
            pixels.set_pixel(i, Adafruit_WS2801.RGB_to_color(r, g, b))
        pixels.show()
        if wait > 0:
            time.sleep(wait)

def brightness_increase_to_color(pixels, color, step=8, t=0.5):
    pixels.clear()
    for j in range(step):
        for i in range(pixels.count()):
            r = min(255, int((float(color[0])/float(step))*j))
            g = min(255, int((float(color[1])/float(step))*j))
            b = min(255, int((float(color[2])/float(step))*j))
            pixels.set_pixel(i, Adafruit_WS2801.RGB_to_color(r, g, b))
        pixels.show()
        time.sleep(t/float(step))
 
def set_color(pixels, color, mod, n):
    for k in range(pixels.count()):
        if k % mod == n:
            pixels.set_pixel(k, Adafruit_WS2801.RGB_to_color(color[0], color[1], color[2]))

def blink_rwg(pixels, blink_times=3):
    white = (1, 1, 1)
    green = (0, 0, 1)
    red = (1, 0, 0)
    for k in range(blink_times):
        brightness_decrease(pixels)
        pixels.clear()
        set_color(pixels, green, 3, k%3)
        set_color(pixels, red, 3, (k+1)%3)
        set_color(pixels, white, 3, (k+2)%3)
        pixels.show()
        brightness_increase(pixels)
        time.sleep(0.15)

def roll_rwg(pixels, blink_times=20, wait=0.25):
    color = white
    for k in range(blink_times):
        pixels.clear()
        for j in range(pixels.count()):
            if ((k+j) % 6) == 0 or ((k+j) % 6) == 1:
                color = green
            if ((k+j) % 6) == 2 or ((k+j) % 6) == 3:
                color = red
            if ((k+j) % 6) == 4 or ((k+j) % 6) == 5:
                color = white
            
            if ((k+j) % 3) == 0:
                color = green
            if ((k+j) % 3) == 1:
                color = red
            if ((k+j) % 3) == 2:
                color = white
            pixels.set_pixel(j, Adafruit_WS2801.RGB_to_color(color[0], color[1], color[2]))
        pixels.show()
        time.sleep(wait)
 
def appear_from_back(pixels, color=(255, 0, 0)):
    pos = 0
    for i in range(pixels.count()):
        for j in reversed(range(i, pixels.count())):
            pixels.clear()
            # first set all pixels at the begin
            for k in range(i):
                pixels.set_pixel(k, Adafruit_WS2801.RGB_to_color(color[0], color[1], color[2]))
            # set then the pixel at position j
            pixels.set_pixel(j, Adafruit_WS2801.RGB_to_color(color[0], color[1], color[2]))
            pixels.show()
            time.sleep(0.02)

def runAround(pixels):
    x = 32
    dimColor =  (x, 2*x, x)
    brightness_decrease(pixels)
    brightness_increase_to_color(pixels, dimColor, step=x)
    for i in range(pixels.count() -5):
        for k in range(pixels.count()):
            pixels.set_pixel(k, Adafruit_WS2801.RGB_to_color(dimColor[0], dimColor[1], dimColor[2]))
        pixels.set_pixel(i, Adafruit_WS2801.RGB_to_color(red[0], red[1], red[2]))
        pixels.set_pixel(i+1, Adafruit_WS2801.RGB_to_color(red[0], red[1], red[2]))
        pixels.set_pixel(i+2, Adafruit_WS2801.RGB_to_color(white[0], white[1], white[2]))
        pixels.set_pixel(i+3, Adafruit_WS2801.RGB_to_color(white[0], white[1], white[2]))
        pixels.set_pixel(i+4, Adafruit_WS2801.RGB_to_color(green[0], green[1], green[2]))
        pixels.set_pixel(i+5, Adafruit_WS2801.RGB_to_color(green[0], green[1], green[2]))
        pixels.show()
        time.sleep(0.00625)
    for i in reversed(range(pixels.count() -5)):
        for k in range(pixels.count()):
            pixels.set_pixel(k, Adafruit_WS2801.RGB_to_color(dimColor[0], dimColor[1], dimColor[2]))
        pixels.set_pixel(i, Adafruit_WS2801.RGB_to_color(green[0], green[1], green[2]))
        pixels.set_pixel(i+1, Adafruit_WS2801.RGB_to_color(green[0], green[1], green[2]))
        pixels.set_pixel(i+2, Adafruit_WS2801.RGB_to_color(white[0], white[1], white[2]))
        pixels.set_pixel(i+3, Adafruit_WS2801.RGB_to_color(white[0], white[1], white[2]))
        pixels.set_pixel(i+4, Adafruit_WS2801.RGB_to_color(red[0], red[1], red[2]))
        pixels.set_pixel(i+5, Adafruit_WS2801.RGB_to_color(red[0], red[1], red[2]))
        pixels.show()
        time.sleep(0.00625)
    for k in range(pixels.count()):
        pixels.set_pixel(k, Adafruit_WS2801.RGB_to_color(dimColor[0], dimColor[1], dimColor[2]))

def makeWheel(pixels, steps, j, i):
    fraction = float((i+j) % 160)/float(pixels.count())
    fracWith2Pi = fraction * 2 * math.pi
    currSin = abs(math.sin(fracWith2Pi))
    rgbCounter = int(currSin * 255)
    return (rgbCounter, 255 - rgbCounter, 0)

def increase_color_for_Wheel(pixels):
    steps = 256
    pixels.clear()
    for j in range(steps):
        for i in range(pixels.count()):
            color = makeWheel(pixels, 80, 0, i)
            r = min(255, int((float(color[0])/float(steps))*j))
            g = min(255, int((float(color[1])/float(steps))*j))
            b = min(255, int((float(color[2])/float(steps))*j))
            pixels.set_pixel(i, Adafruit_WS2801.RGB_to_color(r, g, b))
        pixels.show()
        time.sleep(0.75/float(steps))

def christmasWheel(pixels, t=3, revolutions=3):
    brightness_fast_decrease(pixels)
    increase_color_for_Wheel(pixels)
    steps = 80
    timeSleep = 0.03
    color = (0,0,0)
    for x in range(revolutions):
        for j in range(steps):
            pixels.clear()
            for i in range(pixels.count()):
                color = makeWheel(pixels, steps, j, i)
                pixels.set_pixel(i, Adafruit_WS2801.RGB_to_color(color[0],color[1],color[2]))
            pixels.show()
            time.sleep(timeSleep)
    brightness_decrease(pixels)
 
if __name__ == "__main__":
    # Clear all the pixels to turn them off.
    while True:
        blink_rwg(pixels, 1)
        roll_rwg(pixels)
        blink_rwg(pixels)
        roll_rwg(pixels)
        runAround(pixels)
        christmasWheel(pixels)

    brightness_decrease(pixels)