I want to create a script that will turn off my Raspberry Pi Touchscreen if nobody touched the screen in 30 seconds. (In the future I want the option to reset this 30 second timer with other programs - i.e REST API).

This is what I currently have

from pynput import mouse
import time
import rpi_backlight as bl

bl.set_power(False) # Turn Off the Screen Initially 

def on_move(x, y):

def on_click(x, y, button, pressed):

def control_screen():
    if (bl.get_power() == False):   # If the Screen is OFF
        bl.set_power(True)      # Turn On Screen
        bl.set_brightness(200, smooth=True, duration=3)
        time.sleep(30)      # Wait 30 Seconds
        bl.set_power(False)     # Turn Screen Off Again

# Collect events until released
with mouse.Listener(
        on_click=on_click) as listener:

The problem with this code is that the screen sleeps and then immediately wakes up again (maybe the on_click/on_move are triggered when this occurs)? And also I don't think the sleep(30) is the best approach as I want the sleep timer to start over once the screen is touched. Any tips?


  • are you trying to turn off the touch panel or the LCD or both? – jsotola May 16 '19 at 3:07
  • Turn off the LCD. Still want to receive touch – MTG May 16 '19 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.