LINEOFRAHL
Posts: 4
Joined: Mon Mar 06, 2017 4:44 pm

Another Power button question, but with a twist

Fri Mar 06, 2020 3:49 am

Hey All,

I'm only in the beginning stages of this, but I'm trying to create a TV like device that harkens back to days of yore. After a lot of painful sd card removal and replacement, adjust config.txt files to get the HDMI working on a 7" touchscreen, I got the Pi4 up and running with Buster, Lakka, and LibreELEC. I have a script I used on a Pi3 that triggers a shutdown and restart using a momentary switch connected to the GPIO. I haven't moved it onto the pi4 yet. However, in this new build, I'm hooking up a couple of 3w speakers that run through a small amplifier with a knob. The amplifier connects to the Gpio for power, and connects to the pi's onboard audio jack.

Question 1
Is there a way to use the volume knob to also be a power switch? Meaning when the knob is turned counter clockwise and clicks off, can it trigger a script to turn the pi off? Then when it turns clockwise and clicks on, it can trigger a wakeup?

Question 2
Is there a way to use a two state knob (sorry I dont know the right term here), essentially a dial that can only go right or left one place from the start, to control the power? I want to create the look of an old TV from back in the day with the dials that turned the set on.

Any help getting me in the right direction, or even help creating a script like this would be very helpful.

Thanks!

W. H. Heydt
Posts: 12645
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Another Power button question, but with a twist

Fri Mar 06, 2020 5:55 am

Old TVs had a variety of ways to turn the power on or off. One very common one was a knob that pushed in or pulled out. That could be combined with something that turned. I'd suggest finding a good, old fashioned, electronics store to see if they stock what you want.

The way your power off on the Pi3 works is probably a momentary contact switch. It probably isn't an actuall power *off*, but just a system shutdown (and, if so, pressing it again--if you used the right pins--will reboot the system). A signal waking up a Pi that is truly off won't work. Dropping power and restoring it will, but for the sake of the well being of your SD card, there should be a proper system shut down first.

You can get control switches with any number of independent controls. Each separate (and separated) control is a "pole". So, for instance, you could have a single throw, double pole switch.

User avatar
rpdom
Posts: 17170
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Another Power button question, but with a twist

Fri Mar 06, 2020 6:44 am

LINEOFRAHL wrote:
Fri Mar 06, 2020 3:49 am
Is there a way to use a two state knob (sorry I dont know the right term here), essentially a dial that can only go right or left one place from the start, to control the power?
I think you mean a Rotary Switch. They come in various configurations, like 3 pole 4 way, 1 pole 12 way, 2 pole 6 way etc, but you can usually pop them open and move a tag around to limit the number of positions used.
Unreadable squiggle

LINEOFRAHL
Posts: 4
Joined: Mon Mar 06, 2017 4:44 pm

Re: Another Power button question, but with a twist

Fri Mar 06, 2020 2:56 pm

W. H. Heydt wrote:
Fri Mar 06, 2020 5:55 am
Old TVs had a variety of ways to turn the power on or off. One very common one was a knob that pushed in or pulled out. That could be combined with something that turned. I'd suggest finding a good, old fashioned, electronics store to see if they stock what you want.

The way your power off on the Pi3 works is probably a momentary contact switch. It probably isn't an actuall power *off*, but just a system shutdown (and, if so, pressing it again--if you used the right pins--will reboot the system). A signal waking up a Pi that is truly off won't work. Dropping power and restoring it will, but for the sake of the well being of your SD card, there should be a proper system shut down first.

You can get control switches with any number of independent controls. Each separate (and separated) control is a "pole". So, for instance, you could have a single throw, double pole switch.
You're correct. The script doesn't completely kill power to the Pi, just runs a safe shutdown script. The momentary switch I have now connects to the GPIO pins and the script is just monitoring for a change from HIGH to LOW and vice versa to either turn the pi on, or initiate a shutdown command.

It sounds like both you, and rpdom, are recommending a multi-pole rotary switch (thanks for getting me to the right name guys!). With any multipole switch, I'm assuming I'm connecting two poles, in my case, to two different pins on the pi, and then creating a script similar to what I indicated above. I'm going to look for HIGH vs LOW on two different sets of pins. Does that sound about right? I'll probably have to do some more digging here, but want to confirm I'm on the right path.

IF I happened to find a pull/push type of switch, would I be able to set it up similar to the way that the momentary switch is setup, essentially using the same script, below? I got this script a while back from someone else and it worked well

Code: Select all

#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import subprocess
''''
GPIO.setmode(GPIO.BOARD)
#using pin numbering on the Pi, instead of the 
#GPIO pin outs
#use the same pin that is used for the reset button

GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP)

loopCounter = 0

while True:
	#get current button state
	buttonState1 = GPIOinput(5)
	
	#check if button has been pushed
	if buttonState1 == False: #Button is down.
		loopCounter = loopCounter + 1 #Add another counter to the loop counter
			#shutdown instruction
			subprocess.call("shutdown -h now", shell=True, stdout = subprocess.PIPE, stderr=subprocess.PIPE)
	else:
		loopCounter = 0 #button hasn't been pressed to trigger the loop counter
	
	time.sleep(.5)

#/usr/bin/python
# shutdown/reboot(/power on) Raspberry Pi with pushbutton

import RPi.GPIO as GPIO
from subprocess import call
from datetime import datetime
import time

# pushbutton connected to this GPIO pin, using pin 5 also has the benefit of
# waking / powering up Raspberry Pi when button is pressed
shutdownPin = 5

# if button pressed for at least this long then shut down. if less then reboot.
shutdownMinSeconds = 3

# button debounce time in seconds
debounceSeconds = 0.01

GPIO.setmode(GPIO.BOARD)
GPIO.setup(shutdownPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

buttonPressedTime = None


def buttonStateChanged(pin):
    global buttonPressedTime

    if not (GPIO.input(pin)):
        # button is down
        if buttonPressedTime is None:
            buttonPressedTime = datetime.now()
    else:
        # button is up
        if buttonPressedTime is not None:
            elapsed = (datetime.now() - buttonPressedTime).total_seconds()
            buttonPressedTime = None
            if elapsed >= shutdownMinSeconds:
                # button pressed for more than specified time, shutdown
                call(['shutdown', '-h', 'now'], shell=False)
            elif elapsed >= debounceSeconds:
                # button pressed for a shorter time, reboot
                call(['shutdown', '-r', 'now'], shell=False)


# subscribe to button presses
GPIO.add_event_detect(shutdownPin, GPIO.BOTH, callback=buttonStateChanged)

while True:
    # sleep to reduce unnecessary CPU usage
    time.sleep(5)
'''


Original
Shutdown
Script
import RPi.GPIO as GPIO
import time
import subprocess

GPIO.setmode(GPIO.BOARD)

GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)

loopCounter = 0

while True:
    buttonState1 = GPIO.input(5)
    if buttonState1 == False:
        loopCounter = loopCounter + 1
        subprocess.call("shutdown -h now", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    else:
        loopCounter = 0
    time.sleep(.5)



W. H. Heydt
Posts: 12645
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Another Power button question, but with a twist

Fri Mar 06, 2020 4:16 pm

LINEOFRAHL wrote:
Fri Mar 06, 2020 2:56 pm
IF I happened to find a pull/push type of switch, would I be able to set it up similar to the way that the momentary switch is setup, essentially using the same script, below? I got this script a while back from someone else and it worked well
Not really. A push-pill switch has two positions, contacts open and contacts closed. A momentary contact switch has one position (either open or closed) and switches to the other state only while being held (that is, there's a return spring). There are--as in relays--two default types, "Normally Open" (NO) and "Normally Closed" (NC). For a Pi with a push button shutdown/restart you use an NO momentary contact. An NC momentary contact *opens* the contracts when pressed and then closes them again when released. You can also get momentary contact toggle switches, where it is common to have three connections, so you get NO or NC, depending on how you wire it. For really elegant set ups, you can combine a momentary toggle with a switch cover so you flip the cover up then push the toggle and release it (and, hopefully, close the cover again).

Return to “Beginners”