I added a button to the Raspberry Pi to shut it down with a long press. | そう備忘録

I added a button to the Raspberry Pi to shut it down with a long press.

Shutdown the Raspberry Pi

Since the Raspberry Pi is a Linux OS, it cannot be turned off suddenly.

It is necessary to connect via ssh or connect a keyboard and monitor and use the shutdown command to shutdown the OS.

However, the Raspberry Pi installed at the manufacturing site for IoT is not always able to shut down properly.

I added a button for shutdown to my Raspberry Pi, and I’ll write an article about it as a reminder.

I added a shutdown button to the Raspberry Pi.

Button

Function

  • To avoid shutting down when a button is touched accidentally, shutdown is now done only when the button is “long pressed”.
  • Selected a button with LED light.
  • The LED light on the button turns on when the program is running and turns off when the program is shutdown by pressing and holding the button.

Model Number

I bought an OMRON pushbutton switch (model number = A165L-JRM-5D-1).

The meaning of the model number is as follows.

A16

The type of the button.

5

IP standard grade of water resistance or protection.

  • None: IP40 (protected from wires, etc., not protected against water ingress)
  • 5: IP66 (completely protected from dust ingress, not harmed by strong direct jets of water from any direction)

L

Whether it is illuminated or not

  • None: Non-illuminated
  • L: Illumination

I wanted a button with an LED light, so I chose the illuminated type.

J

Button shape

  • J: Rectangle
  • A: Square
  • T: Round

R

Button color

  • R: Red
  • Y:Yellow
  • PY: Pure yellow
  • G:Green
  • W:White
  • A: Blue
  • PW: Pure white
  • B:Black (non-illuminated only)

M

Button behavior when pressed

  • M: Momentary (turns on only when the button is pressed)
  • A: Alternate (remains on after the button is pressed and released)

5D

LED voltage

  • None: Non-illuminated
  • 5D: 5V DC
  • 12D: AC/DC12V
  • 24D: AC/DC24V

1

This shows the terminal arrangement.

In terms of circuitry, I would have preferred to use an a-contact, but since the button I had available was a c-contact, I chose a type of button with only one contact.

  • 1: 1c (soldered terminal)
  • 2: 2c (soldered terminal)
  • 1P: 1c (printed circuit board terminal)
  • 2P: 2c (printed circuit board terminal)
  • 2S: 2C (quick connector)

The terminal arrangement for 1c is as follows (from the OMRON manual)

Bottom View

Wiring Diagram

I don’t use NC connector.

It is used as a button with real a-contact.

Add a shutdown button

Tested environment

The following environment was tested.

Raspberry Pi

Raspberry Pi 4B

OS

Raspberry Pi OS 32Bit

Program

I saved the following program in “.local/shutdown_test/”

Source code

shutdown_button.py

# -*- coding: utf-8 -*-
"""
Created on Sat Jun  5 08:24:26 2021

・Press and hold button to shut down
・LED on during program operation, off during shutdown

@author: Souichirou Kikuchi
"""

import os
import RPi.GPIO as GPIO
from time import sleep

PIN_BUTTON_LED = 4 # LED on the button
PIN_BUTTON_SD = 22 # shutdown button

def shut_down(channel): # Long Press to shutdown
    if channel == PIN_BUTTON_SD:
        sw = 0
        for _ in range(15):
            sleep(0.2) # 0.2 seconds x 15 times = 3 seconds
            sw = GPIO.input(channel)
            if sw == 0: # If there is even one LOW out of 15 times, it will not shutdown.
                break
        if sw == 1:
            GPIO.output(PIN_BUTTON_LED, GPIO.LOW) # Button LED off
            os.system('sudo shutdown -h now') # shutdown

GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_BUTTON_LED, GPIO.OUT)
GPIO.setup(PIN_BUTTON_SD, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(PIN_BUTTON_SD, GPIO.RISING, callback=shut_down, bouncetime=200)

if __name__ == "__main__":
    try:
        print('ShutDown Test Program Start') # Program start
        GPIO.output(PIN_BUTTON_LED, GPIO.HIGH) # LED lights on the button
        while True:
            sleep(0.001)
    except:
        pass
    finally:
        GPIO.cleanup()
        print('ShutDown Test Program End') # Program ended

supplementary explanation

I’ve included some comments in the program, but I’ll add a few more.

GPIO.setup(PIN_BUTTON_SD, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

is the GPIO22 connected from the button NO, but I have specified a pull_down resistor.

Also, the

GPIO.add_event_detect(PIN_BUTTON_SD, GPIO.RISING, callback=shut_down, bouncetime=200)

In the same way, when the voltage of GPIO22 rises from LOW to HIGH (RISING), the shut_down function is called.

Since the voltage does not rise in a straight line, the interval of 200 milliseconds between the next call to the shut_down function is left with bouncetime=200 to prevent the function from being called repeatedly.

Execution result

Run the program with the following command.

cd ~/.local/shutdown_test
python shutdown_button.py
Run a shutdown test program.

The LED lights up, and I was able to confirm that long press it (for about 3 seconds) will shut it down.

Long press the button to shutdown the OS.

At the end.

This is the end of the article on shutdown by button.

The reason why Raspberry Pi (Linux OS) can’t be shutdown suddenly is that various files used by the OS are written to the SD card while the OS is running.

If you suddenly turn off the power while writing files, those files may be corrupted, which may affect the next time the OS boots and prevent it from booting.

As a countermeasure for this, I’ve written an article on how to “add a shutdown button”.

Other methods.

  1. Set the file system to Read Only(SD Card)
  2. Relay circuit to give grace until OS shutdown

I’ve heard that there is a way to do the above, and I’d like to try it soon.

Finally.

I hope this article will be useful to someone somewhere.

souichirou kikuchi

I'm Japanese. A reminder to remember what I've done. I'm blogging in the hope that it will be helpful to others who want to do similar things. I mainly write blogs about LEGO, AWS (Amazon Web Services), WordPress, Deep Learning and Raspberry Pi. At work, I'm working on installing collaborative robots and IoT in factories. I passed the JDLA (Japan Deep Learning Association) Deep Learning for GENERAL in July 2019. If you have any questions, please leave them in the comments at the bottom of the article.

You may also like...

comment

Name, Email, and Website are optional.
and, your Email address will not be published.