www.makeuseof.com Open in urlscan Pro
13.248.142.229  Public Scan

Submitted URL: https://www.bing.com/ck/a?!&&p=5c4428717d29d302JmltdHM9MTcxMDI4ODAwMCZpZ3VpZD0wODlhNDAyNy1jMjY5LTYyODYtMTdiNi01NDFiYz...
Effective URL: https://www.makeuseof.com/tag/add-power-button-raspberry-pi/
Submission: On March 13 via manual from CZ — Scanned from DE

Form analysis 2 forms found in the DOM

GET /search/?q=

<form method="get" class="search-form js-searchBox icon i-search" id="searchform2" action="/search/?q=">
  <input id="js-search-input" type="text" name="q" class="input-text search-input" autocomplete="off" placeholder="Search MUO" autofocus="">
</form>

Name: articleNewsletterFormPOST

<form class="user-static-form user-content" enctype="multipart/form-data" id="articleNewsletterForm" method="post" name="articleNewsletterForm">
  <h2 class="newsletter-widget-title">Upgrade Your Tech IQ With Our Free Newsletters</h2>
  <p class="f-errors" id="f-validation"></p>
  <div class="form-group">
    <input maxlength="4000" id="newsletter-form-email" name="newsletter-form-email" placeholder="Email Address" required="" type="email">
  </div>
  <div class="form-group">
    <input class="main-cta primary-cta brand-cta" id="SubmitButton" name="SubmitButton" type="submit" value="Subscribe">
  </div>
  <div class="form-group">
    <p class="f-errors" id="newsletter-form-validation" style="margin-top: 1em; font-size: small; color: red"></p>
    <div class="g-recaptcha-container newsletter-form-captcha" hidden="">
      <div id="site_captcha_5">
        <div style="width: 304px; height: 78px;">
          <div><iframe title="reCAPTCHA" width="304" height="78" role="presentation" name="a-cmp2bkqy05m6" frameborder="0" scrolling="no"
              sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-top-navigation allow-modals allow-popups-to-escape-sandbox allow-storage-access-by-user-activation"
              src="https://www.google.com/recaptcha/api2/anchor?ar=1&amp;k=6LeXhCsUAAAAAPI3u-Fknyv2O01JI1GEfnRWV4it&amp;co=aHR0cHM6Ly93d3cubWFrZXVzZW9mLmNvbTo0NDM.&amp;hl=de&amp;v=QquE1_MNjnFHgZF4HPsEcf_2&amp;size=normal&amp;cb=lvbrzorwfmbi"></iframe>
          </div><textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response"
            style="width: 250px; height: 40px; border: 1px solid rgb(193, 193, 193); margin: 10px 25px; padding: 0px; resize: none; display: none;"></textarea>
        </div><iframe style="display: none;"></iframe>
      </div>
    </div>
  </div>
  <p class="content-note bottom-note">By subscribing, you agree to our <a href="/page/privacy-policy/">Privacy Policy</a> and may receive occasional deal communications; you can unsubscribe anytime.</p>
</form>

Text Content

MAKEUSEOF

Newsletter
Log in
Trending iPhone Tips ChatGPT TikTok PS5 Scams Windows 11 Emojis Explained
 * * PC & Mobile
     * Windows
     * Mac
     * Linux
     * Android
     * iOS
   * Internet
     * Social Media
     * Security
     * Programming
   * Productivity
     * Work & Career
     * Creative
     * DIY
   * Lifestyle
     * Smart Home
     * Gaming
     * Entertainment
   * Tech Explained
     * Artificial Intelligence
     * Crypto
     * Tech Jargon
     * Networking
     * Electric Vehicles
   * Reviews
   * Buying Guides
 * * More
     * Free Cheat Sheets
     * Deals
     * Podcasts

 * Join Our Team
 * Newsletter
 * Advertise with us
 * Log in

 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 

 * Join Our Content Team
 * Home
 * Contact Us
 * Terms
 * Privacy
 * Copyright
 * About Us
 * Fact Checking Policy
 * Corrections Policy
 * Ethics Policy
 * Ownership Policy
 * Partnership Disclaimer
 * Owned & Operated by Valnet Inc.


Copyright © 2024 www.makeuseof.com


 * Home
 * DIY


HOW TO ADD A POWER BUTTON TO YOUR RASPBERRY PI

By Christian Cawley
Published May 27, 2019


Want an ordered shutdown for your Raspberry Pi? Here's how to add a power switch
and shutdown script to the Raspberry Pi.


Readers like you help support MUO. When you make a purchase using links on our
site, we may earn an affiliate commission. Read More.

It might be a fantastic, flexible little computer, but the Raspberry Pi has one
key flaw: the lack of a power button. Missing a standard on/off switch can lead
to problems; thankfully, you can add your own Raspberry Pi power button.

MUO VIDEO OF THE DAY
Close
Powered By

10



DJI Mavic Air 3 Review: This Drone Finally Makes Sense!


Share


Next
Stay




SCROLL TO CONTINUE WITH CONTENT

Two choices are available: a DIY power button, or one you purchase. Let's look
at how to add a power button to your Raspberry Pi and guarantee a safe, orderly
shutdown.


WHY YOU NEED A RASPBERRY PI POWER BUTTON

Using a Raspberry Pi is simple enough but powering it up and switching off can
lead to problems (see below).



When you first unbox the Raspberry Pi, the absence of a power button is
striking. After all, every device from desktop and tablet computers to a
wireless mouse has a power button or switch. The Raspberry Pi, on the other
hand, does not.

Instead, you're required to connect the USB power cable and wait for it to boot
your chosen Raspberry Pi-compatible operating system (OS). Powering up is
straightforward enough but switching off is another matter entirely. The answer
is to use a typed command or a mouse click to shut down the Pi---but this isn't
always ideal.




THE IMPORTANCE OF SAFELY POWERING DOWN

If the Raspberry Pi crashes, or you're not able to access it remotely or via
keyboard, mouse, and display, then shutting down suddenly becomes a problem. The
only solution is to pull the power cable.

However, this can lead to problems.

If data is being written to the SD card as the power is pulled, the card can be
corrupted. The result of this is often an operating system that won't boot.
While most modern SD cards are robust enough to handle error correction, a new
OS will probably need to be flashed.

This could also mean losing a significant volume of data. Certainly, if you've
been using the Pi for a specific project, or using it to learn to code with
Scratch, you probably don't want to lose your work.

Pulling the power lead and corrupting the card will do this. Cloning the SD card
is a good way to overcome such data loss, but prevention is always better than a
cure.

Safely powering down the Raspberry Pi will enable the OS to complete active
write processes to the SD card. The computer can then shut down without data
loss and the risk of SD card corruption.





MOUNT A RASPBERRY PI OFF SWITCH ON THE GPIO

One way to achieve a safe Raspberry Pi shut down is with a DIY switch connected
to the computer's GPIO. You can build your own with a Python script, and a
single momentary switch. These are low-cost components and can usually be bought
in multiples.

If you can't get hold of any, or want a switch right away, check any old PC
components you have lying around. It's the same type you would find as your PC's
power button.

The momentary switch should be connected to a powered-off Raspberry Pi on GPIO
pins 39 and 40.



After rebooting, it's time to create the Python script and program the GPIO.

The Raspberry Pi Safe Off Switch GitHub project explains how to use the GPIO
Zero library to create a Raspberry Pi off switch. GPIO Zero is preinstalled with
full versions of Raspbian Stretch, but if you're using the Raspbian Lite
version, you'll need to manually install it.



sudo apt update
sudo apt install python3-gpiozero 

Next, create the script in your text editor. We're using nano:

sudo nano shutdown-press-simple.py 

In the text editor, enter or copy and paste the following script:

#!/usr/bin/env python3

from gpiozero import Button

import os

Button(21).wait_for_press()

os.system("sudo poweroff") 

As you can see, this refers to the gpiozero library, specifies the GPIO pin 21
(the internal numbering system for physical pin 40), and initiates the
"poweroff" command when the button press is detected. Save the script with Ctrl
+ X, then Y to confirm.

Back in the main terminal window, make the script executable:

chmod a+x shutdown-press-simple.py 

To ensure this works following a reboot, append /etc/rc.local:

sudo nano /etc/rc.local 

In the final line before the exit statement, add:

echo '~pi/shutdown-press-simple.py' 

Save and exit as before, then try the button out.


SAFELY MOUNTING THE POWER BUTTON

You might notice that it is easy to accidentally hit the button. Several
solutions are available, from careful mounting away from accidental knocks to
requiring a longer press (refer to the GitHub page above for more on this).

Whichever solution you prefer, mounting the button seems sensible once
installed. You may need to reconfigure your case slightly to accommodate the
button. How you do this depends on several factors, such as your Raspberry Pi
model and the type of case.



If possible, it's a good idea to mount the button so it is recessed. This
prevents any type of accidental knock, much like the power switch on a desktop
computer.




TWO RASPBERRY PI POWER BUTTONS YOU CAN BUY

Looking for something different? Several Raspberry Pi power buttons can be
bought online. Here are two popular options.


1. PI SUPPLY POWER SWITCH



Designed to sit between the Raspberry Pi and the mains outlet, this power switch
comes as a kit. You'll need to solder the components to the PCB to get started
and install this code from GitHub before connecting. Three buttons on the device
let you switch on, switch off (the equivalent of a cable pull), and initiate a
graceful shutdown. The third button also enables a reboot with a longer press.




2. IUNIKER RASPBERRY PI SWITCH

An alternative to the Pi Supply device, the iUniker Raspberry Pi Switch is a
prefabricated product with the cables and switch encased. However, there is no
facility to avoid a sudden shutdown, which means this is only suitable for
switching your Raspberry Pi on.

On the other hand, the iUniker switch can also help to avoid wear on your
Raspberry Pi's power connector.




ADD MORE BUTTONS TO YOUR RASPBERRY PI

Despite being released way back in 2012, the Raspberry Pi still ships without a
power button. Of course, this is by design. Matters of cost, and the vast array
of Pi projects (anything from a media center to an embedded IoT project) mean
the power button (or switch) remains an optional extra.

If you need one, install one; if not, carry on!

It isn't just shutting down that can prove troublesome for the Raspberry Pi.
What if you simply need to reboot? Here's how to add a reset switch to your
Raspberry Pi.




UPGRADE YOUR TECH IQ WITH OUR FREE NEWSLETTERS








By subscribing, you agree to our Privacy Policy and may receive occasional deal
communications; you can unsubscribe anytime.

Comments
Share Share Share Share Share
Copy
Email
Share

Share Share Share Share Share
Copy
Email
Link copied to clipboard

Related Topics
 * DIY
 * Raspberry Pi
 * GPIO
 * DIY Project Tutorials

About The Author
Christian Cawley • Deputy Editor (1613 Articles Published)


Former Deputy Editor for Security, Linux, DIY, Programming, and Tech Explained,
Christian has extensive experience as a writer and editor and has been published
in Computer Weekly, Linux Format, Tech Radar, ComputerActive, and other titles.

Christian also produced MakeUseOf's weekly show, the Really Useful Podcast, from
2018-2024.

His current passions are his cigar box guitar, his retro gaming site
(retrogamerbase.com), and his family.


Close



RECOMMENDED ARTICLES

Android Tips

HOW TO FIND A LOST ANDROID PHONE

It's not hard to locate a missing Android phone that's connected to the
internet.


DIY Project Ideas

11 DIY ELECTRONICS PROJECT IDEAS FOR ENGINEERING STUDENTS

These electronics projects are ideal for students looking for a way to develop
their skills.


Documentaries

THE 7 BEST DOCUMENTARY STREAMING SERVICES

Want to watch documentaries? These are the streaming services you need to check
out.


Artificial Intelligence

HOW TO USE NVIDIA'S CHAT WITH RTX AI CHATBOT ON YOUR COMPUTER

Interested in Nvidia's new Chat with RTX AI chatbot? Here's how you can try it
out!


Router

HOW TO TURN AN OLD ROUTER INTO A WIRELESS BRIDGE

Not sure what to do with your old router? Try turning it into a wireless bridge!


Nvidia GeForce Now

NVIDIA LAUNCHES GEFORCE NOW PASSES, BUT IS THERE ANY POINT TO THEM?

GeForce NOW Day Passes are here, but who's buying them?




 * ARTIFICIAL INTELLIGENCE


 * VPN

HOW TO USE MIDJOURNEY TO CREATE AI ART

2 hours ago

HOW TO MAKE MUSIC FROM A TEXT PROMPT USING SUNO AI

22 hours ago

9 GENERATIVE AI TOOLS TO IMPROVE YOUR WORK LIFE

1 day ago
See More

WHAT IS ICLOUD+? UNDERSTANDING APPLE'S CLOUD SUBSCRIPTION SERVICE

1 day ago

VPN WON'T CONNECT? 5 WAYS TO FIX IT

5 days ago

VPN BLOCKED? WHY THIS HAPPENS AND WHAT TO DO ABOUT IT

Feb 19, 2024
See More


TRENDING NOW

5 GOOGLE SEARCH ALTERNATIVES AND THEIR TRADEMARK FEATURES



THE 9 BEST FREE MUSIC DOWNLOAD SITES TO LEGALLY DOWNLOAD MUSIC FOR FREE



THE SPRING INTO SOLAR SALE: ENERGIZE YOUR ADVENTURES AND SAVE BIG WITH BLUETTI



 * Join Our Content Team
 * Home
 * Contact Us
 * Terms
 * Privacy
 * Copyright
 * About Us
 * Fact Checking Policy
 * Corrections Policy
 * Ethics Policy
 * Ownership Policy
 * Partnership Disclaimer
 * Owned & Operated by Valnet Inc.

Copyright © 2024 www.makeuseof.com
✕


Über Google anmelden
user-signalWir schätzen Ihre Privatsphäre
Alles akzeptieren
ZusammenfassungZwecke1532 Partner
Stimmen Sie diesen Maßnahmen zur Datenverarbeitung durch uns und unsere Partner
zu?

Speichern von oder Zugriff auf Informationen auf einem Endgerät

Genaue Standortdaten und Identifikation durch Scannen von Endgeräten

Personalisierte Werbung und Inhalte, Messung von Werbeleistung und der
Performance von Inhalten, Zielgruppenforschung sowie Entwicklung und
Verbesserung von Angeboten

Einige Partner berufen sich möglicherweise auf ihr berechtigtes Interesse. Sie
können hier Einspruch gegen das berechtigte Interesse des Verkäufers einlegen.
Ihre Auswahl auf dieser Website wird für diese Website angewendet. Sie können
Ihre Einstellungen jederzeit ändern, indem Sie Ihre Einwilligung widerrufen,
indem Sie auf das Symbol in der unteren rechten oder linken Ecke klicken.
Cookies, IDs, Browserdaten oder ähnliche Online-Identifikatoren dürfen für einen
oder mehrere der hier genannten Zwecke verarbeitet werden.

Powered By