Uninterruptible power supply for the Raspberry Pi

Serial Still Works

Typically, corresponding GPIO pins are used to gain access to the serial interface of the Raspberry Pi. I use a so-called serial USB-to-TTL cable, which can be purchased online. Because of the presence of the UPS board, the pins can no longer be reached; however, this is not a problem. You can redirect the serial interface of the Raspberry Pi to the UPiS with a firmware upgrade. Similarly, it is possible to redirect the interface to the micro-USB connection of the UPS. Then, the USB port of the PC should be connected with that of the UPiS board. This solution usually works.

In the example described here, communication took place over the serial USB-to-TTL cable previously mentioned using minicom on the connected Linux PC. It was not necessary to adjust the configuration at all to gain access over the micro-USB connection of the UPiS. This held true for both the Rasp Pi and PC operating systems.

What Happens During a Power Outage?

The first task of a UPS is to protect connected devices from loss of power. The UPS should also be able to deal with situations in which the loss of power lasts longer than the time it takes for the battery to discharge. Typically, the UPS will direct all applications to close automatically and initiate an orderly shutdown of the connected device. The UPiS is equipped to deal with these tasks as well.

Two methods can be used for reading the status of the power supply and initiating suitable actions. Additionally, a hardware button, along with the proper configuration, can start an orderly shutdown of the Raspberry Pi. This switch has appeared as SDWN (Shutdown) during the firmware upgrade. A software implementation available via the UPiS terminal command @sdwn.

Information regarding a desired shutdown is always readable on GPIO pin number 27 for both software- and hardware-initiated shutdowns. Therefore, when the Raspberry Pi is supposed to respond to the press of the hardware button, a program needs to run on the Rasp Pi to read out the status of this pin. The user handbook for the UPS board provides a simple Python script free of charge (Listing 2), which gently shuts down the ARM computer with shutdown -h now.

Listing 2

Shutdown Script

01 #!/usr/bin/env python
02 # Import the access libraries for GPIOs, OS, and time delay
03 import RPi.GPIO as GPIO
04 import time
05 import subprocess
07 # Adapt the pin numbering
08 GPIO.setmode(GPIO.BCM)
10 # We need to pay attention to pin 27
11 GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
13 # A loop for waiting until the button is pressed, which is signaled via pin 27
15 while True:
16 # Check if something came in on pin 27
17   if(GPIO.input(27)==0):
18 # We want to execute the following command
19     cmd="sudo shutdown -h now"
20 # Send this command to the OS
21     pid=subprocess.call(cmd, shell=True)
22     break
23 # Wait for one second
24   time.sleep(1)
25 # That's all

You still need to deal with automatic shutdown. Apparently, there must be a change in the status of the power supply for this to be triggered. As described above, various steps must be undertaken before the Raspberry Pi will notice that it is running on battery power. Another UPiS terminal command comes to the rescue. By issuing @pwrinfo on, the user communicates to the UPS board that it should signal a status change in the power supply to the serial interface (Figure 3). A listener process can then react in the desired fashion.

Figure 3: When it has been configured properly, the UPiS will tell when the status of the power supply changes.

You also can query via @pm regularly for the desired information and then initiate suitable actions. Users who find dealing with the serial interface unwieldy can use the I2C bus for reading out the necessary data. The register address needed to do this is 0x6A 0x00.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Testing a Pi UPS module

    When the Raspberry Pi is connected to a car ignition or the USB port on a TV, you run the risk of data loss with a hard shutdown. The Pi UPS bridges short lapses in the power supply and shuts down your Rasp Pi safely when the power remains off.

  • Extensions for the Raspberry Pi

    The resourcefulness of private and commercial users has led to the creation of many practical and novel extensions for the Raspberry Pi. We provide an overview of some useful supplementary circuit boards.

  • Exploring the new Raspberry Pi Model B+

    The new Raspberry Pi Model B+ is the biggest change to Raspberry Pi since the Model B Rev 2 upgrade two years ago. Find out what's new with the B+ and how it will affect your Rasp Pi adventures.

  • SunRover Part 2 – Solar Power Controller/Power System

    Putting power in your robot is more than just running wires – you'll need to make sure the power supply doesn't cause interference that will disrupt other components. This article explores the problem of electrical noise and describes the design for a multiplexing solar power system.

  • Adding an On/Off switch to your Raspberry Pi

    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.