Book Image

Raspberry Pi cookbook for Python programmers

Book Image

Raspberry Pi cookbook for Python programmers

Overview of this book

Table of Contents (18 chapters)
Raspberry Pi Cookbook for Python Programmers
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

A controlled shutdown button


The Raspberry Pi should always be shut down correctly to avoid the SD card being corrupted (by losing power when writing to the card). This can pose a problem if you don't have a keyboard or screen connected (if you are running an automated program or controlling it remotely over a network and forget to turn it off) as you can't type the command or see what you are doing. By adding our own buttons and LED indicator, we can easily command a shutdown, reset, and start up again to indicate when the system is active.

Getting ready

You will need the following equipment:

  • 3x Dupont female to male patch wires

  • Mini breadboard (170 tie points) or a larger one

  • Push button

  • Standard LED (for example, a red one)

  • 2x 470 ohm resistors

  • Breadboarding wire (solid core)

The entire layout of the shutdown circuit will look as shown in the following figure:

The controlled shutdown circuit layout

How to do it…

Create the shtdwn.py script as follows:

#!/usr/bin/python3
#shtdown.py
import time
import...