On-Off graceful switch project Raspberry Pi 2

Possible next project.

Instructions for On-Off Switch for the Raspberry Pi 2

The following is from the above URL,

Pulling the plug on your Pi without an orderly shutdown can corrupt the SD card. Also, many users prefer a convenient switch to clicking icons and entering shutdown commands. We show you some options for starting, stopping, and powering down.

MagPi

This article was written in partnership withThe MagPi magazine: http://www.themagpi.com

As most of you know by now, the Raspberry Pi is a fantastic little Linux box with many wonderful features. Over the last year and a bit that the Raspberry Pi has been out in the wild, users have discovered that this little computer can do almost anything – and without crippling your wallet!

However, Raspberry Pi’s very small price tag and form factor mean that the Rasp Pi has had to do without some of the home comforts normally found on a laptop or desktop computer – one of these comforts is a power-on and -off switch. The only safe way to shut down the Raspberry Pi is to do it manually using the graphical desktop interface – either with the LXDE power management window or by opening a terminal window and typing in a shutdown or reset command.

Although an orderly shutdown is not a huge amount of work, it often gets overlooked or forgotten, and some users just pull out the microUSB power plug while the system is running. Doing this repeatedly could very easily lead to a corrupted SD card, a completely broken SD card, or even damage to the Raspberry Pi itself.

Why risk the annoyance and cost of a broken SD card when you can very easily employ a number of power management options? In this article, I describe some options for adding a power switch to your Raspberry Pi. I’ll show you how to implement

  • a reset switch – use this switch to start the system or reboot in case of a crash;
  • a safe shutdown switch – equivalent to shutting down the GUI or entering the shutdown command in the terminal window, the switch triggers a Python script that initiates an orderly shutdown for the system;
  • a full On/Off power switch – a more complete solution that shuts down the system and also shuts off power to the Raspberry Pi. I’ll describe the hardware configuration then show you how to use the switch to trigger a script that initiates a safe shutdown.

Reset Switch

Parts and Tools for Reset Switch

The reset switch will only work with the Rev 2 Raspberry Pi boards. You will need [5]:

  • One-row, two-way male header, 2.54mm (RS part 251-8086 or similar)
  • Reset switch from an old computer (Maplin A19GL, or similar)
  • Soldering iron and solder

When the Revision 2 Raspberry Pi Model B board was released around the end of 2012, it introduced a number of improvements and changes to the Revision 1 board. Among these changes was the introduction of the P6 header, which is located beside the HDMI port on Raspberry Pi Rev 2 boards. (All Model A boards are Rev 2 boards also.)

The purpose of the P6 header is to enable a reset of the Raspberry Pi’s CPU. The reset switch can restart the system, but it does not provide the safe shutdown described in the next section; therefore, it shouldn’t be used to shut down or reboot a system that is already running. To perform a reset, all you have to do is short the two connections of the header together. Although you have a number of ways to do this, in my opinion, the best way is with some header pins and a momentary switch.

As you can see in Figure 1, the header is not populated with any header pins by default (because things like header pins would add cost); however, it is very simple to add these pins with some basic soldering. All you need is a standard two-way, one-row 2.54mm male header (Figure 2). Place the pins into the holes of the P6 header, grab your soldering iron, and secure them in place.

Abbildung 1: The P6 header on Raspberry Pi Rev 2 supports a handy reset feature.
Abbildung 2: Connect to the P6 with a standard two-way, one-row male header.

If you haven’t soldered for a while and are feeling a bit uneasy about soldering directly on your Raspberry Pi, you will find a number of great guides online; my personal favorites are the “Soldering is Easy” comic [1] or the SparkFun soldering guide [2].

Once you have successfully soldered the pins to the board, you are ready to connect the reset switch and test it out (Figure 3).

Abbildung 3: Attach the switch cable to the header to test the reset switch.

You now have a fully functional reset switch (Figure 4). All you need to do is apply a short press and the Rasp Pi will perform a soft reset. An added bonus of this switch is that when the Rasp Pi is in the shutdown or “halt” state (plugged in with power applied to board, but after having been shutdown from within the OS), you can also use this switch to wake it up.

Abbildung 4: Use this simple switch to start or reboot your Raspberry Pi.

Safe Shutdown Switch

Parts for Safe Shutdown Switch

To build the safe shutdown switch, you will need [5]:

  • 1kohm resistor
  • 10kohm resistor
  • Momentary switch
  • Breadboard
  • Jumper wires, both male-to-male and male-to-female

The reset switch is a very useful feature of the Rev 2 board; however, it does not allow for a safe shutdown of the Rasp Pi. A safe shutdown is very important to avoid causing SD card errors. Fortunately, it is possible to create a GPIO-connected switch, which, at the press of a button, allows you to ensure that the Rasp Pi has fully and correctly shut down before you pull the power plug. Tapping a safe shutdown switch is a far easier and cooler way to turn off a computer than opening a terminal window and typing:

sudo shutdown -h now

The schematic in Figure 5 shows the layout for a safe shutdown switch for use with the Raspberry Pi. You might be wondering why you can’t just connect the 3.3V pin, via a switch, to a GPIO pin set as an input; however, if you were to do this with no resistors or other circuitry, the circuit would either be very unreliable or not function at all.

Abbildung 5: Schematic for the safe shutdown switch.

The problem is that, when the input pin is not connected to anything (because the switch is open), it will “float” – its value will change a lot because of interference from the mains (and other noise) or similar phenomena. Thus, the input state will not remain at a defined voltage level. The Rasp Pi would not be able to detect reliably whether the input was in a high or low state.

You can address this problem with either a pull-up or pull-down resistor. In this example, I have used a pull-down resistor. As you can see, the GPIO4 pin is connected to ground via a 10kohm resistor (R1). Ten kiloohms is a large enough value that the 3.3V signal source is able to overcome the pull-down resistor and continue on its way to GPIO4. If the interference and noise became larger, then a smaller resistor would be needed, but don’t go too low, or the signal source will not be able to defeat the pull-up/down resistor.

For this reason, when implementing pull-up/down resistors, it is typical to use a resistor value that is around 10 times the source impedance – in most Rasp Pi applications, 10kohm should be suitable. The resistor R2 is a current-limiting resistor used to protect the GPIO pin and make the circuit safer, which is important because if you accidentally were to set GPIO4 as an output, pushing the button would cause a short circuit directly between 3.3V and ground. To make it easier for you to assemble your project on a breadboard, I have included a simple breadboard layout. The complete breadboard configuration is shown in Figure  6.

Abbildung 6: Safe shutdown breadboard configuration.

A press of the switch will initiate a safe shutdown of the Raspberry Pi, which will eventually end up in thehalt state. If you want to start the Raspberry Pi up again, it is possible to wake it from a shutdown by simply pressing the reset switch described in the preceding section. You’ll need one of the safe shutdown scripts in the Code Examples section for the safe shutdown switch to work

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: