On/Off switch for the Raspberry Pi touch display

Sie betrachten gerade 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.

Schaltplan
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
KEY_BOUNCE_TIME=2

#GPIO-Pin of the button
PinButton=14

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(PinButton,GPIO.IN)

DisplayOn=True
while True:
    time.sleep(0.1)
    print(GPIO.input(PinButton))

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

GPIO.cleanup()
sys.exit()

 

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.

Schaltplan
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
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
PIR-Module HC-SR502 and HC-SR505

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

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

Schaltplan
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

#PIR-Kontakt
PinButton=21

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

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(PinButton,GPIO.IN)

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

    if GPIO.input(PinButton)==1:
        #save timestampe of buttonpress
        KeypressButton=time.time()
        if DisplayOn==False:
            DisplayOn=True
            SetDisplay("RD7",True)

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

GPIO.cleanup()
sys.exit()

 

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):
    cmd=""
    if DisplayKind=="HDMI": 
        if DisplayOn==True:
            cmd="xset dpms force on"
        else:
            cmd="xset dpms force off"
    if DisplayKind=="RD7": #Raspberry Pi Touch-Display 7"
        if DisplayOn==True:
            cmd="bash -c \"/home/pi/display/display_on\""
        else:
            cmd="bash -c \"/home/pi/display/display_off\""
    if cmd<>"":
        print("Display-Status: "+str(DisplayOn))
        print(cmd)
        os.system(cmd)
        #wait 10 seconds, cause of the detection time of the PIR
        time.sleep(10)

 

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.

Downloads

symbol_download

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

Links

Wikipedia article of the Darlington pair:

https://en.wikipedia.org/wiki/Darlington_transistor

Information of motion detection:

https://en.wikipedia.org/wiki/Motion_detector

My articles of the media player:

TACTbox – My Media Player

TACTbox+


Diesen Artikel in Deutsch lesen

Dieser Beitrag hat 2 Kommentare

Kommentar verfassen