How it works...

This time, when we set up the GPIO pin, we define the pin connected to the shutdown button as an input and the pin connected to the LED as an output. We turn the LED on to indicate that the system is running.

By setting the DEBUG flag to True, we can test the functionality of our script without causing an actual shutdown (by reading the terminal messages); we just need to ensure that we set DEBUG to False when using the script for real.

We enter a while loop and check the pin every second to see whether the GPIO pin is set to LOW (that is, to check whether the switch has been pressed); if so, we enter the doShutdown() function.

The program will wait for three seconds and then test again to see whether the button is still being ...

Get Raspberry Pi 3 Cookbook for Python Programmers - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.