The main goal of this project is to have an easy way to safely power on and power off the raspberry pi with a physical button. The button used in this project has an RGB LED to show the current state of the Raspberry Pi to the user. This project has only been tested using Ubuntu Server 22.04 LTS (64-bit) and the hardware specified in the hardware section.
To use this program, please recreate the circuit using the hardware shown below then install the files found in the source (src) folder.
Color | Process |
---|---|
No Color | Operating System Off |
Red | Operating System Starting Up |
Green | Start Up Complete; Button Ready to Use |
Yellow | Operating System Restarting |
Action | Outcome |
---|---|
Button Press & Release (< 1 seconds) | Shutdown |
Button Press & Release (> 1 seconds) | Restart |
- Hardware
- Circuit & Setup
- Install & Uninstall
- Demonstration
- Final Thoughts
- Acknowledgments
- Resources
- License
The following material was used to complete the project:
Item | Price |
---|---|
Raspberry Pi 3 B+ | $ 35 |
16GB Micro SD Card | $ 9 |
USB Micro Power Supply | $ 10 |
Dupont Wires | $ 7 |
Resistors | $ 11 |
Spade Connectors | $ 9 |
Wire Crimper | $ 27 |
LED Power Button | $ 17 |
18 AWG Solid Wire | $ 17 |
Breadboard | $ 10 |
Wire Cutters | $ 20 |
Note: The links provided are only suggestions. They are not the only options for purchasing these specific products. I do not have any affiliation with the companies selling these products. The prices shown now may not be up to date.
Note: A Micro SD to USB A converter may be needed if the computer you're going to use to flash the operating system does not have a SD card slot.
Notes:
- The LED in the power button is a common anode RGB. This means that the three LEDs share a positive connection.
- From the LED Power Button, the wire going to GPIO 3 and the wire going to ground (GND) are both for the button. They need to be connected to the normally open (NO) spade connectors on the button, which are the two outermost connectors. A drawing can be found here or on the LED Power Button product webpage.
Note: The first step assumes that the user has already installed Ubuntu 22.04 LTS (64-bit) on their Raspberry Pi micro SD card. I recommend using the Raspberry Pi Imager.
Also, please complete the circuit above before beginning this section.
- First, please update your operating system using the following commands:
sudo apt update && sudo apt upgrade
Note: This can take some time depending on how updated your operating system is currently. Consider restarting after this command is finished.
- In order to install the installation file, please install curl using the following command:
sudo apt-get install curl
- The program uses Python 3 and the GPIO Zero module Please run the next two commands to install both of them:
sudo apt-get install python3
sudo apt-get install python3-gpiozero
- To install the program, please run the installation file:
curl https://raw.githubusercontent.com/josephgalloway321/raspberry_pi_power_button/main/src/install.sh | bash
Note: If everything installed correctly, the shell will print "Installation complete. Please restart your device." The LED will also turn green.
- Please restart your device using the following command:
reboot
Once the operating system completes restarting, the system should be ready to use.
Run the following command to see if the service is actively running:
systemctl status power_button.service
To Uninstall, please run the uninstallation file:
curl https://raw.githubusercontent.com/josephgalloway321/raspberry_pi_power_button/main/src/uninstall.sh | bash
There are three demonstration videos of how the operating system should behave depending on how the button is pressed. The start up and restart videos are sped up, but each should take around two minutes total for each process. The shutdown video is in real time. Please download and watch each video found in the videos folder.
I learned so much completing this project. In addition to adding a convenient power button to my Raspberry Pi, I learned about bash scripting, systemd, and mechatronics. I intend on using this project in combination with my future robotics projects. I also plan on updating the project to work on different operating systems, different hardware, and a YouTube video walking viewers through recreating the project themselves.
Please let me know if this project helped in any way! I'd really appreciate any feedback or how someone improved upon this design. I would also really appreciate credit if this project helped.
The credit for all of the information in this project goes to the people who made the content in the resources section below. They were all excellent resources and guides for this project. I'm very grateful and could not have done it without their help.
- Raspberry Pi 3B+ Image Source
- How to Add a Power Button to Your Raspberry Pi
- Run a Program On Your Raspberry Pi At Startup
- How to Run a Bash Script on Startup!!
- Python Tutorial: Calling External Commands Using the Subprocess Module
- Python (RPi.GPIO) API
- Python Subprocess Documentation
- Python Wait For Edge Function Documentation
- Demystifying Linux: The purpose of /bin, /usr/bin and /usr/local/bin
- How to make a file executable in linux?
- Shebang (Unix) Wiki
- Bash Scripting – Case Statement
- How to pass and use arguments in shell script
- Running Programs Automatically on Your Tiny Computer
- How to Make a Raspberry Pi Program Start on Boot (systemd)
- Install RPi.GPIO Python Library
- How to Set Static IP in Ubuntu Server 22.04
- Using Common Cathode and Common Anode RGB LED with Arduino
- Current Sourcing and Sinking
- New "gpio" config command
- raspberry-gpio-python Wiki
- gpiozero
- API - Input Devices for gpiozero
- fire1ce version
- Accessing config.txt
- Bash: Append to File
- ShellHacks
- From the terminal verify if python 3 is installed
- Redirection in Linux - Linux Tutorial 8
- Stackoverflow help
- How can I check if a package is installed and install it if not?
- FAQ gpio zero
- GPIO cleanup via terminal
- What is the state of the GPIO Pins during Boot Up?
- How To Check If a Directory Exists In a Shell Script
- Using Markdown for Formatting Content
- How do I center an image in the README.md file on GitHub?
- Is there a way to add a gif to a Markdown file?
- What are GitHub Licenses? How To Add a License? Why You Should Use a License?