MagicMirror² v2.14.0 is available! For more information about this release, check out this topic.

Python - One momentary switch, LED, and a shut-off timer

  • Working on programming a LED strip lights around the mirror, with a single push button. I am good with the GPIOs and the relay required, but seeking some guidance on the code

    • First button press: Lights activate. Have the option to move to the second button press or if the button is not pressed a second time, then the side lights will turn off automatically after 30 minutes.
    • Second button press: Lights turn off and python is waiting for the first button press (again).

    Below is one version that I been working on where I wanted to divide the lights (side lights (“low”), then side&top lights (“high”), then all off), but at this point I would settle for just all lights at the same time.

    The auto-shutoff is required to ensure they aren’t left on for days at time. 🙂

    It is possible that I need to pull in another module, but I can’t seem to hack my way into figuring out the shutoff timer or where to place it, so it is time to call in a lifeline. Thanks.

    import RPi.GPIO as GPIO
    from time import sleep
    LEDPinLow = 26
    LEDPinHigh = 22
    buttonPin = 5
    # Setup the pin the LED is connected to
    GPIO.setup(LEDPinLow, GPIO.OUT)
    GPIO.setup(LEDPinHigh, GPIO.OUT)
    # Setup the button
    GPIO.setup(buttonPin, GPIO.IN, pull_up_down = GPIO.PUD_UP)
    buttonPress = True
    ledStateLow = False
    ledStateHigh = False
    while (1):
            print("Press it")
            buttonPress = GPIO.input(buttonPin)
            if buttonPress == False and ledStateLow == False and ledStateHigh == False:
                GPIO.output(LEDPinLow, True)
                GPIO.output(LEDPinHigh, False)
                print("LED ON - Side lights only")
                ledStateLow = True
                ledStateHigh = False
            elif buttonPress == False and ledStateLow == True and ledStateHigh == False:
                GPIO.output(LEDPinLow, True)
                GPIO.output(LEDPinHigh, True)
                print("LED ON - Side and Top Lights ")
                ledStateLow = True
                ledStateHigh = True
            elif ledStateLow == True and ledStateHigh == True:
                GPIO.output(LEDPinLow, False)
                GPIO.output(LEDPinHigh, False)
                print("LEDs OFF")
                ledStateLow = False
                ledStateHigh = False

  • Re: Python - One momentary switch

    I found the answer over on stackexchange and figured I would share. The mirror is a college graduation present for my youngest daughter; she requested the light ring (I heard the word make-up followed by blah, blah, blah, blah…, kidding of course.). The 30 minute timer was not a joke, there’s no way I am leaving responsibility of turning off the light to a college grad 🤦🏼

    I am still working on it and will post a thread in the correct forum when it is done.

    The answers are here:

    #!/usr/bin/env python
    import time
    import pigpio
    LEDPin = 26
    buttonPin = 5
    LEDOn = False
    minutesOn = 0
    def callback(gpio, level, tick):
       global minutesOn, LEDOn
       if level == 0: # button press
          if LEDOn:
             print("LED off")
             LEDOn = False
             pi.write(LEDPin, 0)
             print("LED on")
             LEDOn = True
             pi.write(LEDPin, 1)
             minutesOn = 0
       elif level == pigpio.TIMEOUT:
          if LEDOn:
             minutesOn += 1
             print("LED on for {} minutes".format(minutesOn))
             if minutesOn >= 30:
                print("LED off")
                LEDOn = False
                pi.write(LEDPin, 0)
    pi = pigpio.pi()
    if not pi.connected:
    # Setup the pin the LED is connected to
    pi.set_mode(LEDPin, pigpio.OUTPUT)
    # Setup the button
    pi.set_mode(buttonPin, pigpio.INPUT)
    pi.set_pull_up_down(buttonPin, pigpio.PUD_UP)
    pi.set_glitch_filter(buttonPin, 5000)
    pi.set_watchdog(buttonPin, 60000)  # watchdog every minute
    cb = pi.callback(buttonPin, pigpio.EITHER_EDGE, callback)
    while True: # all the work is done in the callback

  • Here’s a pic of the work in progress.
    alt text

Log in to reply