On/Off switch for the Raspberry Pi touch display

On/Off switch for the Raspberry Pi touch display

The 7″ touch screen for the Raspberry Pi is a practical thing for your own projects. But often there are problems with the screensaver. Sometimes the display is simply switched off when you actually want to touch it. Sometimes it is on when it should be off. Ways to improve and control this yourself can be found in this article.

 In many blog articles on the internet you find how to turn off the Raspbian screensaver. For many cases that is already a sufficient solution. This keeps the screen always on. But there are also cases in which the display should be switched off and on again. Then you need an additional control option for the display.

Switching on and off is quite easy with a few commands. The finished files can be found at the end of the article under Download.

Switch background light on and off

On the original Raspberry Pi 7 “display, it is best to create two files on and off. The two files contain only a 1 (off) or a 0 (on). Depending on the status, one of the files will be copied to /sys/class/backlight/rpi_backlight/bl_power.

For the display, this means switching the display backlight on or off.

Display OFF – file display_off:

sudo cp off /sys/class/backlight/rpi_backlight/bl_power

Display ON – file display_on:

sudo cp on /sys/class/backlight/rpi_backlight/bl_power

If the screen is so off, but there is no way to activate it again by touch. This makes it useless for anyone. I would like to show you 3 ways to solve this problem.

Reactivation with button

One possibility could be to install a hardware button and switch on the display by pressing it. You could realize this with only a few parts.

Schematic diagram for the wiring of the mechanical button

You get the state of the button via a python program. If this program is loaded as a background process, every other application could use this function directly.

Excerpt from the python test program: test.py

#Bounce time in seconds

#GPIO-Pin of the button


while True:

    if GPIO.input(PinButton)==0:
        #Button is pressed, ==0 because of the pull-up resistor
        DisplayOn=not DisplayOn
        if DisplayOn==True:
            print("Screen ON")
            print("Screen OFF")

        #Wait bounce time



Reactivation via sensor button

Often there is no space available to install a button. In these cases, it would be beneficial to have an extremely flat one. A solution could be a sensor button. It does not consist of mechanical components, but only wires for activation and an electronic part.

Schematic diagram for the wiring of the sensor button

The electronics of the button consists of a Darlington pair. The two transistors amplify a low current flow through the finger of the touching person and this act as a tactile signal for the Raspberry.

Circuit board
Circuit board of the sensor button

For the function, it is sufficient to attach two bare wires to a housing. This requires extremely little space inside the housing. For the attachment 4 holes are sufficient, through which the wires are mounted. To secure it is recommended to fix them on the inside with hot glue.

Wires of the sensor mounted on a housing

To control the sensor button the same python program is used as above.

Reactivation with a PIR

The use is more comfortable without pressing or touching buttons. This can be achieved with a PIR element – a motion detector. The components are usually available in 2 designs.

PIR-Module HC-SR502 and HC-SR505

The larger version can be set via potentiometers in the behavior of detection and activation time. The small version has fixed values. The connection to the Pi is easy.

There are only 3 contacts: VCC, Data and GND.

Schematic diagram for the wiring of  a PIR

The output of the motion detector is programmed with a python program, very similar to the example above. With motion detection, however, more meaningful extensions can be programmed. For example, a follow-up time with automatic switch-off when no more motion takes place.

Excerpt from the python program: automatic.py


#wait time in seconds
DISPLAY_ON_TIME=12 # + 8 seconds for the small PIR-version = total time


while True:
    if i==10:
        if DisplayOn==True:
            print("Time-Out: "+str(DISPLAY_ON_TIME-round(time.time()-KeypressButton)))

    if GPIO.input(PinButton)==1:
        #save timestampe of buttonpress
        if DisplayOn==False:

    if DisplayOn==True:
        if time.time()-KeypressButton>DISPLAY_ON_TIME:
            #time is over, screen off



Other displays

With this code you can also control other display types. If you want to use an HDMI screen, these commands are possible.

Display OFF:

xset dpms force on

Display ON:

xset dpms force off

This is already prepared in the SetDisplay function. Simply call the function with the appropriate parameters.

  • ON: SetDisplay(“HDMI”,True)
  • OFF: SetDisplay(“HDMI”,False)
def SetDisplay(DisplayKind,DisplayOn):
    if DisplayKind=="HDMI": 
        if DisplayOn==True:
            cmd="xset dpms force on"
            cmd="xset dpms force off"
    if DisplayKind=="RD7": #Raspberry Pi Touch-Display 7"
        if DisplayOn==True:
            cmd="bash -c \"/home/pi/display/display_on\""
            cmd="bash -c \"/home/pi/display/display_off\""
    if cmd<>"":
        print("Display-Status: "+str(DisplayOn))
        #wait 10 seconds, cause of the detection time of the PIR


Example mediaplayer

In my case, the PIR sensor is installed in a media player. For every detection of a movement, the display should be turned on and then remain on for 20 seconds. If there is a movement in the meantime, we reset the 20 seconds. When the 20 seconds are over, the display will turn off automatically.

However, the display is built into a standard housing. But, this does not provide enough space to place a motion detector.

That’s why I opted for a variant from the 3D printer. The PIR housing can be attached directly to the display on the camera holder.

Mounted PIR on the Raspberry Pi
Mounted PIR on the Raspberry Pi

The housing is available for both types, the large and the small PIR version. The models can also be downloaded under Links as a stl file.

For me, the small version was the better choice. Here it only has to be considered that a fixed time duration has been set for the motion detection. In my case, that’s 8 seconds. The detection program must take this time into our python program.

With these solutions, you can now hopefully turn your display on or off more targeted and thus achieve a comfort gain for your project.



There you find all files of the project for a download:
Python-Programs for mechanical button and sensor button
3D model for the housing of the PIR sensors
Schematic diagrams for Fritzing


Wikipedia article of the Darlington pair:


Information of motion detection:


My articles of the media player:

TACTbox – My Media Player


Dieser Beitrag hat 2 Kommentare

Kommentar verfassen

Menü schließen