Raspberry Pi 4B
Latest Raspbian Lite 64-Bit
I swear I searched the forum before I posted this, but I'm pretty new here, and I might not be searching for the right terms. I have a physical switch connected to the GPIO Pin 23 and Ground. I'm trying to control the display with the switch (flip the switch off and the screen goes blank.)
I put the following code together, but when I run it, it just prints "display_power=1" over and over again. I can't figure out what I need to change. Thanks in advance for any help you can offer.
import RPi.GPIO as GPIO
import subprocess
import time
# Set up GPIO using BCM numbering
GPIO.setmode(GPIO.BCM)
# Set up GPIO pin for the switch
switch_pin = 23
GPIO.setup(switch_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Variable to track the previous state of the switch
prev_switch_state = GPIO.input(switch_pin)
# Main loop
try:
while True:
# Check the state of the switch
switch_state = GPIO.input(switch_pin)
# If the switch state has changed, update the display power
if switch_state != prev_switch_state:
if switch_state == GPIO.LOW:
# Switch is off, turn off the display
subprocess.run(['vcgencmd', 'display_power', '0'])
else:
# Switch is on, turn on the display
subprocess.run(['vcgencmd', 'display_power', '1'])
# Update the previous switch state
prev_switch_state = switch_state
# Delay for a short time to avoid high CPU usage
time.sleep(0.1)
except KeyboardInterrupt:
GPIO.cleanup()
Latest Raspbian Lite 64-Bit
I swear I searched the forum before I posted this, but I'm pretty new here, and I might not be searching for the right terms. I have a physical switch connected to the GPIO Pin 23 and Ground. I'm trying to control the display with the switch (flip the switch off and the screen goes blank.)
I put the following code together, but when I run it, it just prints "display_power=1" over and over again. I can't figure out what I need to change. Thanks in advance for any help you can offer.
import RPi.GPIO as GPIO
import subprocess
import time
# Set up GPIO using BCM numbering
GPIO.setmode(GPIO.BCM)
# Set up GPIO pin for the switch
switch_pin = 23
GPIO.setup(switch_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Variable to track the previous state of the switch
prev_switch_state = GPIO.input(switch_pin)
# Main loop
try:
while True:
# Check the state of the switch
switch_state = GPIO.input(switch_pin)
# If the switch state has changed, update the display power
if switch_state != prev_switch_state:
if switch_state == GPIO.LOW:
# Switch is off, turn off the display
subprocess.run(['vcgencmd', 'display_power', '0'])
else:
# Switch is on, turn on the display
subprocess.run(['vcgencmd', 'display_power', '1'])
# Update the previous switch state
prev_switch_state = switch_state
# Delay for a short time to avoid high CPU usage
time.sleep(0.1)
except KeyboardInterrupt:
GPIO.cleanup()
Statistics: Posted by ike-g — Mon Apr 01, 2024 2:08 am