members
Raspberry basics: Project 25a Raspberry PI Zero W board - Shutdown and restart buttons
of Lex C. in Raspberry Pi Zero W
Raspberry basics: Project 25a
Project name: Raspberry PI Zero W board - Shutdown and restart buttons
Tags: Raspberry, Raspberry PI Zero W board, vers 1.1, v 1.1, shutdown, switch off, restart, button, momentary switch, RPi.GPIO library, time library, os library
Attachments: shutdownreboot.py
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 2 pcs
14. Jumper cables F-M
General
We will learn how to shutdown or reboot your Raspberry PI Zero W board without LAN or WiFi or SSH.
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 the Raspberry PI Zero W board
Wiring
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.
- Do wiring.
- 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.
- 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).
- Make sure that your monitor or TV is turned on, and that you have selected the right input (e.g. HDMI/DVI, etc).
- 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.
- Plug in your USB mouse and USB keyboard to 4-Port USB 2.0 Hub.
- 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.
- Connect Micro USB power supply to Raspberry PI Zero board micro USB input.
- The Raspberry PI desktop will start up.
- Open Terminal window and type the command: sudo apt-get update
- Then type the command: sudo apt-get upgrade -y
- Adding -y to the end of apt-get commands instructs the program to automatically answer yes to any questions rather than waiting for you to type Y or N.
- Open the Raspberry Pi Configuration tool from the main menu. Go to Raspberry icon-> Preferences -> Raspberry PI configuration
- Select Interfaces and make sure that all settings are enabled so you will not need to come back to these configuration tool again.
- Click on OK button.
- We need to reboot our Raspberry PI. If it will not reboot automatically go to Terminal. Type this command in the Terminal: sudo reboot
2. Making the program
- After restart of Raspberry Pi go to the Terminal and type this command: sudo nano shutdownreboot.py
- Copy and paste the code from shutdownreboot.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.
- Press Ctrl+X, Y, Enter buttons to save the file.
- Type the command in the Terminal: sudo python shutdownreboot.py
- When you press first button your Raspberry Pi will be turned off.
- When you press second button your Raspberry Pi will reboot.
3. Modifying the rc.local
- 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.
- Type this command in the Terminal to open the rc.local file if you need to modify it: sudo nano /etc/rc.local
- Add commands to execute the python program:
- sudo python /home/pi/shutdownreboot.py &
- exit 0
- Press Ctrl+X, Y, Enter buttons to save the file.
- 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.
- Now reboot the Pi to test it: sudo reboot
Summary
We have learnt how to shutdown or reboot your Raspberry PI Zero W board without LAN or WiFi or SSH.
Libraries in use
- RPi.GPIO
- time
- os
Script
- Look for attached code on the begining of this project
Other projects of Lex C.








jobs.viewed