Easy Project 19n Raspberry PI Zero W board - turn off by button

of Acoptex.com in Raspberry Pi Zero W

Raspberry basics: Project 19n

Project name: Raspberry PI Zero W board -  turn off by button

Tags: Raspberry, Raspberry PI Zero W board, vers 1.1, v 1.1, push button, momentary switch, button, RPi.GPIO library, turn off by button, switch off by button, time library, os library

Attachments: switchoffbutton.py

Raspberry Pi
Monitor or TV
HDMI cable
USB keyboard
USB mouse
Power supply
8GB SD card

In this project, you needed these parts (Dear visitors. You can support our project buy clicking on the links of parts and buying them or donate us to keep this website alive. Thank you):

1. Raspberry PI Zero W board 1 pc

2. Micro SD card with NOOBS and SD card adapter 1 pc

3. Micro USB power supply (2 A 5V or 5V 3A) 1 pc

4. USB keyboard 1 pc

5. USB mouse 1 pc

6. TV or PC monitor 1 pc

7. HDMI cable 1 pc

8. T-Cobbler Breakout and GPIO Cable 1 pc

9. Micro USB 2.0 OTG Cable 1 pc

10. Mini HDMI to HDMI Adapter (HDMI to Mini HDMI Adapter) 1 pc

11. 4-Port USB 2.0 Hub 1 pc

12. Breadboard 1 pc

13. Momentary switch 1 pc

14. Jumper cables F-M


We will learn how to turn off by button Raspberry PI Zero W board, without a monitor or keyboard or LAN or WiFi.

Understanding the momentary switch (button)

You can read more about it here.

Understanding the Raspberry PI Zero W board

You can read more about it here.

Signals and connections of momentary switch (button)

You can read more about it here.

Signals and connections of the Raspberry PI Zero W board


Step by Step instruction

We recommend using a high-performance SD card for increased stability as well as plugging your device into an external display to see the default application booting up.

1. Setup and preparation

We assume that you have Windows 10 installed on your PC and Raspbian OS installed on your Raspberry Pi Zero W board. 

  1. Do wiring.
  2. Insert your micro SD card with Raspbian OS into the TF card slot on the Raspberry Pi Zero W board. It will only fit one way.
  3. Connect Raspberry PI Zero W board mini HDMI port to your TV or Monitor HDMI (DVI) port (use HDMI cable and mini HDMI to HDMI adapter and/or HDMI to DVI adapter).
  4. Make sure that your monitor or TV is turned on, and that you have selected the right input (e.g. HDMI/DVI,  etc).
  5. Plug in micro USB 2.0 OTG Cable to USB data port of Pi Zero and 4-Port USB 2.0 Hub to micro USB 2.0 OTG Cable. 
  6. Plug in your USB mouse and USB keyboard to 4-Port USB 2.0 Hub.
  7. If you intend to connect your Raspberry Pi Zero vers 1.2 or vers 1.3 to the internet, connect a WiFi dongle to one of the 4-Port USB 2.0 Hub ports.
  8. Connect Micro USB power supply to Raspberry PI Zero board micro USB input.
  9. The Raspberry PI desktop will start up.
  10. Open Terminal window and type the command: sudo apt-get update
  11. Then type the command: sudo apt-get install 

2. Making the program

  1. Type this command in the Terminal: sudo nano switchoffbutton.py

  2. Copy and paste the code from switchoffbutton.py to opened window. Since it is a Python code, you need to be careful with the Tab characters as it is important to group the instruction as blocks in Python.

  3. Press Ctrl+X, Y, Enter buttons to save the file.
  4. Type the command in the Terminal: sudo python switchoffbutton.py

  5. When you press the button your Raspberry Pi will be turned off.

3. Modifying the rc.local

  1. One of the methods to run a script on your Raspberry Pi at startup - use the file rc.local. In order to have a command or program run when the Raspberry Pi boots, you can add commands to the rc.local file. This is especially useful if you want to power up your Raspberry Pi in headless mode (that is without a connected monitor), and have it run a program without configuration or a manual start.
  2. Type this command in the Terminal to open the rc.local file if you need to modify it: sudo nano /etc/rc.local

  3. Add commands to execute the python program:
  4. sudo python /home/pi/switchoffbutton.py &
  5. exit 0

  6. Press Ctrl+X, Y, Enter buttons to save the file.
  7. The Raspberry Pi will run this script/program at bootup, and before other services are started.  If you don’t include the ampersand and if your program runs continuously, the Raspberry Pi will not complete its boot process. The ampersand allows the command to run in a separate process and continue booting with the main process running.
  8. Now reboot the Pi to test it: sudo reboot



In the Python program, first we have imported packages.

The first important function of the RPi.GPIO Module is the setmode(). Using GPIO.setmode(), we can select either GPIO Numbering of the Pins or Physical Numbering. By using GPIO.setmode(GPIO.BOARD), we are selecting the Physical Numbering Scheme.

NOTE: For GPIO Numbering, you can use GPIO.setmode(GPIO.BCM).

The next function is the setup(pin,mode). This function will allow us to set the pin as either input (GPIO.IN) or as output (GPIO.OUT). 

button = 21. Button attched to GPIO21.

GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP) will set button pin mode as input, and pull up to high level (3.3V)

Each GPIO pin in Raspberry Pi has software configurable pull-up and pull-down resistors. When using a GPIO pin as an input, you can configure these resistors so that one or either or neither of the resistors is enabled, using the optional pull_up_down parameter to GPIO.setup

If it is set to GPIO.PUD_UP , the pull-up resistor is enabled; if it is set to GPIO.PUD_DOWN , the pull-down resistor is enabled. 

if (not input_state) will check whether the button is pressed or not. By default input_state is True.

The last function is the GPIO.cleanup(). With the help of this function, we can make a clean exit from the program as it will clean all the resources that are used in the program.


We have learnt how to turn off by button Raspberry PI Zero W board.

Libraries in use

  • RPi.GPIO
  • time
  • os


  • Look for attached code on the begining of this project

Other projects of Acoptex.com
Medium Basics: Project 083w Sipeed Maixduino board - Using PlatformIO IDE of Acoptex.com in Sipeed Maixduino 08-08-2019
Medium Basics: Project 083e Sipeed Maixduino board - Uploading MaixPy of Acoptex.com in Sipeed Maixduino 04-08-2019
Medium Basics: Project 083f Sipeed Maixduino board - Using MycroPython of Acoptex.com in Sipeed Maixduino 04-08-2019

Published at 31-12-2018
Viewed: 1237 times