How to make Blinkt! slowly fade on and off?


I have gotten pretty comfortable with Blink! on the really basic stuff, but want to learn more. I want to start messing around with turning LED’s on and off, and would like to have it slowly turn off and on the LED’s. kind of a soft glow fading in and out. I have tried to look at the examples or even modify them and nothing seems to work. it just results in crazy LED flashing. Does anyone know what the code to make all pixels fade in and out a certain color?


It’s probably easier to start with a while loop and increment a number that corresponds to the brightness you’re going for.

If you use the HSV colour space ( documented somewhat here: ) you can control the V (Value, or Brightness) to fade in and out without changing your colour.

For example this will generate 100 steps of linear brightness for the colour red (360 degrees around the hue wheel, or 1.0 hue):

import colorsys
for x in range(NUM_STEPS):
    r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(1.0, 1.0, float(x) / NUM_STEPS)]
    print(r, g, b)

This is a good place to start. But the downside of this approach is timing. If you use time.sleep(DELAY_TIME) or similar to slow down your colour transition, you will end up with an animation that will run faster/slower depending on how heavily loaded your Pi is, or what type of Pi you run it on.


That makes sense, with the loop. Is there a way to takw this code and have them fade left to right or anything like that? Im excited to give this a try. All i have done so far is the nornal r,g,b stuff.



You can fade from left-to-right and various other transitions. There are dozens of ways to accomplish this but one of the easiest is to use the same loop as above, but run through setting one pixel at a time.

You can rely on Blinkt! to remember each pixel as you set them, so you can run through one at a time and leave the previous pixel at its final value on each step.

Again, this is probably not technically the correct way to do this, since you should aim to create a function that - when given a time - sets every pixel on Blinkt! to be the correct colour/brightness for your animation effect at that given time. This is where mathematical functions come in super useful.

For example math.sin can be used fairly easily by just throwing your time value into it, and it’ll output a number that goes continuously from -1 to +1. You may imagine from this, how you might create a light that fades in/out over time.

I’ll let you go the easy way first, then perhaps we can work toward the more advanced methods :D


Thanks! i have a couple ideas that i want to try. This thing has been a lot of fun. I got it to go along with a Retro Pie setup and I have spent more time learning about coding than playing old games…