Use Blinkt to show door open or closed

How would I code Blinkt to show red if a door’s closed, and green if it’s open?

I can do the magnetic switch OK, but can’t figure the code to get Blinkt to respond to changes in GPIO status.

Thank you.

Anyone? Must be possible…

You just want a loop continually reading the GPIO status and re-acting accordingly:

import RPi.GPIO as GPIO
from blinkt import *
GPIO.setup(4, GPIO.IN)

while True:
    switch_open = GPIO.input(4)
    if switch_open:
        for i in range(8):
            set_pixel(i , 0, 255, 0)
        for i in range(8):
            set_pixel(i , 255, 0, 0)


was about to post an example too (using gpiozero and a button but same principle).

Note that you can use the set_all() function if you want all pixels to be the same colour.

Brilliant! Thank you, I’ll look at those over the next few days.

No worries, thought there might be a set_all type function, but the blinkt example I grabbed didn’t have an example of it :)

Just be mindful of whether your reed-switch (or equivalent) is wired up as pull-up or pull-down as it will affect the logic in my example.

That works just fine, thank you.

The set_all function is documented here:

How do you make it switch the LEDs off when you kill the script?

Try something like this:

… note there’s a dedicated doc site for a number of Pimoroni libs, with more added as time permits: