Pi Power

The Raspberry Pi is a low cost, single board computer with reasonable performance and relatively low power consumption, typically a version of the Linux operating system, often Raspbian.

The Pi family of boards have turned out to be very useful machines for standalone and/or portable projects such as remote environment monitoring, cameras, etc.

But to be truly portable, a system needs to include a power source and a way to control that power - such as a rechargeable battery, a charger, an on/off switch and some way to monitor battery status.

What I want is something equivalent to the way my iPhone works

  • To power it up from a cold state, press a button for a few seconds
  • To power it off, press the same button for a few seconds
  • Indicate how much power remains in the battery
  • Provide an alert when that is running really low
  • Shut down safely without any data corruption if the battery does run out
  • To recharge the battery, just plug in a cable from a USB charger

This project provides one approach to reaching this goal, building on the LiPoPi project from Daniel Bull.


The system consists of some relatively simple circuitry that links the Pi with a LiPoly battery charger and two Python scripts that handle monitoring and control.

The hardware consists of three subsystems:

1: A Power On / Power Off circuit taken from LiPoPi

Power On / Power Off - schematic

2: An Analog-Digital Converter that monitors the battery and USB voltages on the Power Boost

Battery monitor ADC - schematic

3: Two status LEDs (or an RGB LED)

Status LEDs - schematic

The software consists of two python scripts:

pi_power.py monitors the battery voltage and handles shutdown of the system when the battery runs out, or when the user pushes a button. It writes the current battery status to a file.

pi_power_leds.py checks the status file and sets a red or green led according to that.

How it works

Starting with a powered down system, press the pushbutton and hold for 3 or more seconds. The green activity LED on the Pi will start flickering to show that the system is booting up. At that point you can release the pushbutton. You'll see the boot messages if you have a screen attached.

The two pi_power scripts will start up in background and the power status LEDs will indicate the current status. These will change according to whether or not the USB power cable is connected and the current battery state.

You can safely shutdown the system manually by pressing the pushbutton again. The Pi will shutdown and the PowerBoost will stop providing power to the Pi (the blue PowerBoost LED will go out). If the USB cable is connected, the battery will continue to recharge.

If you let the battery run out, the Red LED will warn you by flashing and when the fraction remaining reaches a low, but safe, level the system will shutdown. The shutdown level is set conservatively to ensure the battery does not drain completely and cause the Pi to lose power.

This plot shows the battery voltage as it drains over times under a load, along with the LED modes.

Voltage graph

When the USB power cable is connected to the PowerBoost and the battery is fully charged, the battery voltage is around 4.15V. When that cable is removed, the battery becomes the power source and the voltage drops immediately to around 4.05 V (I'm not sure why...) and then decays down to around 3.7V at which point it can not longer power the Pi.

pi_power sets 4.05V as the 100% power level and 3.75V as 0%, which gives a bit of a buffer at the low end. Between the two levels the voltage decline is close enough to being linear that we can use predict the fraction of battery remaining from the voltage. It's not totally accurate but it is good enough for our purposes here.


The system uses a Adafruit PowerBoost 1000 Charger - Rechargeable 5V Lipo USB Boost @ 1A - 1000C to provide regulated 5V power from a LiPoly battery or a USB power supply. When a USB power supply is attached, the PowerBoost not only powers the RasPi but also recharges the battery. It is a great little device for this sort of project. Adafruit sell it for around $20 and they have a detailed tutorial available.

There are three parts to the hardware side

Power On / Power Off Switch

A momentary pushbutton switch is used to power up the Pi from a cold start and to trigger an orderly shutdown of the system. This machinery is taken from the LiPoPi project from Daniel Bull, which I contributed to. Here is a description of how that works.

The specifics depend on whether you have a RasPi model 3 or earlier.

###Pre-RasPi 3 circuit

Power On / Power Off - schematic

To power up from a cold start using the pushbutton:

  • The pushbutton pulls the PowerBoost Enable pin high
  • The PowerBoost delivers power to the Pi which starts to boot
  • GPIO14 on the Pi goes high without any software being run
  • GPIO14 keeps the PowerBoost Enable pin high when the pushbutton is released

To shutdown safely using the pushbutton:

  • The pi_power.py script monitors GPIO26
  • Pressing the pushbutton sends this pin high and the script triggers a safe shutdown
  • Shutdown means that GPIO14 is no longer high and so the PowerBoost stops sending power to the Pi
  • The diodes isolate the power up and power down functions
  • The two diodes in series serve to drop the battery voltage to a safe level for the Pi GPIO pin

Note that it leaves out the low battery component of the LiPoPi project as we can monitor that as part of the battery voltage in the next section.

Note the addition of a 0.1uF ceramic capacitor between GPIO26 and Ground. Without this, GPIO26 would trigger a shutdown whenever I plugged the USB power cable into the PowerBoost. I don't why this happens but it probably because of relatively long wires on the breadboard acting as radio antennae and picking up some sort of spike when the USB cable was connected. This was easily solved, however, by adding the capacitor to smooth out the line to GPIO26.

Try it in your configuration and if you don't need it then leave it out.

###RasPi 3 circuit

Pi3 Power On / Power Off - schematic

The RasPi model 3 changed the way GPIO14 operates. There are two small but important changes to the approach:

Pre-RasPi3 the serial console that uses GPIO14 used a hardware UART and that is what led GPIO14 to be high. RasPi 3 uses a software UART which does not do this. But we can emulate this by enabling the serial console in raspi-config and adding a capacitor across the 100K resistor to smooth the voltage on GPIO14 and keep the PowerBoost Enable pin high.

  1. Check you have the correct configuration in raspi-config - see Installation below
  2. Place a 100uF electrolytic capacitor in parallel to the 100K resistor, as shown in this schematic.

I used a 100uF 16V capacitor

This solution was figured out by Daniel Bull and Simon Claessen in the LiPoPi project - all the details are here.

The 100uF capacitor should not be used in the pre-Pi 3 circuit.

Voltage Monitoring with an ADC

To assess how much power is left in a battery we can use the pins on the PowerBoost1000C. The Pi does not have an Analog to Digital Converter (ADC) itself so we need to add an external one in the form of a MCP3008, which is an 8-Channel 10-Bit ADC With SPI Interface. We are only using two channels so this is a bit of an overkill, but they are not expensive (around $4).

The USB pin on the PowerBoost has the voltage of the input USB connection. When the cable is not connected this is 0V and around 5.2V when connected. Nominally, a USB power supply provides 5V but manufacturers often bump this up a little bit to counteract any voltage drop over the supply cables.

The measured USB voltage is used to determine if the cable is attached or not. Doing this via an ADC is overkill but as we need the ADC for the battery voltage, it makes sense to use it.

The Battery voltage on the Bat pin is what really tells us the current battery status. This has the output voltage of the LiPoly battery, which varies from 3.7V when depleted to around 4.2V when fully charged.

The maximum input voltage for the analog side of the ADC is 3.3V so we need to reduce the PowerBoost voltages using a pair of resistors acting as a Voltage Divider. In this case a combination of 6.8K and 10K resistors brings the maximum input voltages into the desired range.

The ADC uses the SPI interface on the RaspberryPi side which uses four GPIO pins. The code for this interface was written by the Adafruit team and manages SPI in software, as opposed to the Pi's hardware SPI interface, so you can choose other GPIO pins if necessary.

Battery monitor ADC - schematic

Note that this is a schematic and does not correspond to the actual chip pinout.

Battery Status LEDs

The system needs a way to tell the user how much power remains in the battery. One of the simplest ways is to control a Red and Green LED from the RasPi to implement a simple interface - green is good, red means low battery.

Even better is to use an RGB LED and control the red and green components - you can even get yellow. Most RGB LEDs are Common Anode which means you need to use this circuit.

Status LEDs - schematic

But you can use individual LEDs and wire them like this

Status LEDs - schematic

If you do this you will want to edit the pi_power_leds.py and comment/uncomment the relevant lines - see the comments in the code.

Note the breadboard diagram shows two individual LEDs in this 'common cathode' arrangement.

Putting it all together

Wiring all this up on a breadboard gets you something like this:


Here is a larger version of this layout

And here is what my actual breadboard looks like - not pretty, but functional

Breadboard Photo

The breadboard layout was created to make the circuit easy to understand (you may argue whether or not I succeeded...). One consequence of that is that a number of the jumper wires are relatively long, compared to a compact circuit board. This may have a couple of unwanted side effects.

Long wires attached to RasPi GPIO pins may act as radio aerials. Even though the code uses internal pull-down resistors to avoid the pins from floating, I did have a problem with the shutdown pin (GPIO26 in the circuit) getting triggered when I plugged the PowerBoost 1000C input USB cable back in, after it had been disconnected. I solved that by placing a 0.1uF ceramic capacitor between GPIO26 and Ground.

In addition, there can be voltage drop over longer wires and the breadboard tracks. The Adafruit guide to the PowerBoost 1000C mentions this. Shorter wires are better

Please do not just wire your circuit from the breadboard diagram - understand the circuit first - you may be able to come up with a neater layout and, more importantly, I may have made a mistake in creating the diagram.

Here is a photo of another breadboard layout where I've gone for a more compact layout. Note that the wires are much shorter and I did not need the 0.1uF capacitor on the power down circuit.

Breadboard Photo 2


The software end of Pi Power is split into two components.

pi_power.py does all the work. It monitors the battery, handles the power down process as well as shutting down the Pi is the battery runs out. It checks the battery every minute and records the current status in the file /home/pi/.pi_power_status.

In general it is intended to run as a background process that starts up automatically when the Pi boots up.

For testing you can run it from a shell. It can take three options:

  • --debug will output the battery and USB voltages every time it checks (once a minute)
  • --log outputs the same information to the file pi_power_log.csv which you can use to generate a plot of voltage over time
  • --safe delays system shutdown by 2 minutes. See the installation section for how this can be used.

There are many ways to inform the user about the power status. You might have a numeric display on a screen, or a bar graph like you get on some phones. Separating the monitoring and display components makes it easier for others to build different displays.

I have chosen the simple approach here of using a Red and Green LED to indicate the general power status. pi_power_leds.py checks the status file and sets either led according to that. It is easy to change the specific LED patterns but the default ones are:

  • Green, Blinking - USB power source is connected - Battery is charging
  • Green, Constant - Battery - more than 25% of battery remains
  • Red, Constant - Battery - more than 15% of battery remains
  • Red, Blinking - Battery - more than 10% of battery remains
  • Red, Blinking Fast - Battery - less than 10% of battery remains - system will shutdown soon

Here is a plot of battery voltage over time, marked to show how the LED modes correspond to voltage (RasPi Zero and 2700mAh battery)

Status LEDs - schematic

The script checks the status file every 30 seconds so there can be a delay between, say, plugging in a USB supply and the LEDs updating. Reducing the poll interval would improve this.



The Power Up function relies on GPIO14 which is used for the RasPi UART serial interface. Before installing the pi_power scripts, you need to configure this as follows:

If you have a RasPi pre-model 3:

Run sudo raspi-config and under "Advanced Options" select "Serial" followed by No. This prevents the Pi using GPIO 14 for the console (which would shut off the power).

If you have a RasPi model 3:

Run sudo raspi-config and under "Advanced Options" select "Serial" followed by Yes. This should be the default

Python on the Pi - if you do not have Python and the Rpi.GPIO library installed on your Pi then you will need to do the following

sudo apt-get update
sudo apt-get dist-upgrade

sudo apt-get install python-dev

sudo apt-get install python-setuptools
sudo easy_install rpi.gpio

pi_power scripts

The following assumes you have the hardware set up to use the GPIO pins as shown above

Boot your Pi and download pi_power.py and pi_power_leds.py and make them executable with chmod a+x pi_power.py etc.

Run the scripts in a terminal window, using the --debug option for pi_power.py.

$ ./pi_power_leds.py &
$ ./pi_power_debug.py --debug

Make sure eveything works as expected - the LEDs show the correct status, you can shutdown and restart the system, etc. You want to test all the functions:

  • Power on from the pushbutton
  • Power down a running system from the pushbutton
  • Check all LED modes with and without USB cable, allowing the system to drain the battery
  • Allow the battery to drain and trigger a low battery shutdown

Depending on the size of your battery, this can take some time but you need to test it thoroughly before making the scripts start on boot.

Run the scripts on Boot

Both scripts are intended to be run as silent background processes that start up when the Pi boots.

NOTE The Pi boot process is different from other Linux systems in that there is no single-user mode. That makes it difficult to fix problems in start up scripts like this as you have no easy way to run the system up to, but not including, the problem script. So... test everything really well before you put them in a system start up script

To do this you want to add the full paths to the scripts to /etc/rc.local.

/home/pi/pi_power_leds.py &
/home/pi/pi_power.py --safe &

Note the --safe option. Use this while you are testing. It delays the system shutdown for two minutes. Certain errors in your circuit can trigger an immediate shutdown. If you run into that, this delay gives you two minutes to edit rc.local and comment out the lines while you fix the problem.

Once you have tested your system and you are confident that all is well, you can remove the --safe option.

/home/pi/pi_power_leds.py &
/home/pi/pi_power.py &

And that's it... hope you like it...


Pi Power has been tested successfully on these boards:

  • Raspberry Pi 2 Model B V1.1 (2014)
  • Raspberry Pi Zero v1.2 (2015)
  • Raspberry Pi 3 Model B V1.2 (2015) - using the Pi 3 modification shown above

Please take a look at the Wiki for more background on battery charging, power usage by the Pi etc.

The power on / power off machinery is taken from the LiPoPi project from Daniel Bull, which I have contributed to. If you want a simpler solution without the voltage monitoring machinery then LiPoPi might be just what you need.

Currently there is no way for the software to know when the battery is fully charged. On the PowerBoost, the yellow charging LED changes to Green, so the information is available. There is just no easy way to get it to the Pi. Accurately estimating it from the voltage does not seem to work well in practice.

Adafruit also sell the PowerBoost 500C charger. This can output a maximum current of 500mA as opposed to the 1A of the 1000C. More importantly, the 500C can power its output or charge the battery, but not both. The 1000C can charge the battery while powering the Pi, so this is the one to use.

One downside of the power on / power off mechanism is that you can no longer reboot the machine using the shutdown -r now command. The system will shutdown the PowerBoost which cuts power for the reboot. Simply shutdown and hit the pushbutton to get the same effect.

For a mobile system you may be tempted to skip the bulky USB cable between the PowerBoost and the Pi and just wire the output 5V and Ground to the corresponding pins on the GPIO header. This does work but is not recommended as it appears to bypass a power control circuit on the Pi. With a HDMI display plugged into the Pi with this configuration, the Blue output LED on the PowerBoost flickers dimly, suggesting some current path back to the PowerBoost from the HDMI display. I have not been able to troubleshoot this further.

Pi Power

Raspberry Pi 是一款低成本,单板计算机,性能合理,功耗相对较低 消费,通常是Linux操作系统的版本,通常是 Raspbian

Pi系列板已经证明是非常有用的机器,用于独立和/或 便携式项目,如远程环境监控,摄像机等。

但是,要真正实现便携式,系统需要包括电源和控制方式 这种电源 - 例如可充电电池,充电器,开/关开关和一些 监控电池状态的方法。


  • 要从冷态启动电源,请按一下按钮几秒钟
  • 要关闭电源,请按相同的按钮几秒钟
  • 指示电池剩余电量
  • 当运行的时候提供一个警报
  • 如果电池电量不足,请安全关闭数据损坏
  • 要为电池充电,只需从USB充电器插入电缆

该项目通过 LiPoPi 项目,为实现这一目标提供了一种方法 来自Daniel Bull。


该系统由一些相对简单的电路组成,将Pi与 LiPoly电池充电器连接 和两个处理监视和控制的Python脚本。


1:从 LiPoPi 获取的开机/关机电路

2:模拟数字转换器,用于监视Power Boost上的电池和USB电压

3:两个状态LED(或RGB LED)


pi_power.py 可监控电池电压,并处理系统关机 当电池电量耗尽时,或用户按下按钮时。它将当前电池状态写入文件。

pi_power_leds.py 检查状态文件,并根据该设置设置一个红色或绿色LED。

How it works

从掉电系统开始,按下按钮并按住3秒钟以上。 Pi上的绿色活动LED将开始闪烁显示 系统启动。此时您可以释放按钮。如果您附有屏幕,您将看到引导消息。

两个pi_power脚本将在后台启动,电源状态指示灯将指示当前状态。 这些将根据USB电源线是否连接和当前电池状态而改变。

您可以通过再次按下按钮来手动安全关闭系统。 Pi将关闭,PowerBoost将停止提供电源 到Pi(蓝色PowerBoost LED将熄灭)。如果USB电缆已连接,电池将继续充电。

如果让电池电量耗尽,红色LED将会闪烁警告,当剩余部分达到低电平但安全的电平时,系统将会 关掉。关闭电平保持置位,以确保电池不会完全耗尽并导致Pi失去电力。


当USB电源线连接到PowerBoost并且电池已充满电时,电池电压约为4.15V。 当电缆被拆除时,电池变成电源,电压立即下降到4.05 V左右(我不知道为什么…) 然后衰减到3.7V左右,此时它不能再为Pi供电。

pi_power将4.05V设置为100%功率电平,3.75V为0%,这在低端有一点缓冲。两级之间 电压下降足够接近线性,我们可以使用预测电池剩余电量的分数。 这不是完全准确的,但这对我们这里的目的来说是非常好的。


系统使用a Adafruit PowerBoost 1000充电器 - 可充电5V Lipo USB Boost @ 1A - 1000C 从LiPoly电池或USB电源提供稳压的5V电源。当连接USB电源时,PowerBoost没有 只能为RasPi供电,但也可以为电池充电。对于这样的项目来说,这是一个很好的设备。 Adafruit卖掉 约为20美元,并且具有详细的教程


/ svg> 开机/关机开关

瞬时按钮开关用于从冷启动开启Pi,并触发系统的有序关闭。 该机器采用 LiPoPi 项目 来自我贡献的丹尼尔·布尔(Daniel Bull)。这是一个如何工作的描述。

具体细节取决于您是否具有RasPi 3或更早版本。

### Pre-RasPi 3电路


  • 按钮将PowerBoost启用引脚拉高
  • PowerBoost为Pi开始启动
  • 提供电源
  • Pi上的GPIO14高,没有运行任何软件
  • 当释放按钮时,GPIO14将PowerBoost启用引脚保持为高电平


  • pi_power.py脚本监视GPIO26
  • 按下按钮将此引脚置为高电平,脚本触发安全关机
  • 关机意味着GPIO14不再高电平,因此PowerBoost停止向Pi发送电力
  • 二极管隔离电源并断电功能
  • 串联的两个二极管用于将电池电压降低到Pi GPIO引脚
  • 的安全电平
请注意,它会排除LiPoPi项目的低电量组件,因为我们可以将其作为其一部分进行监控 电池电压在下一节。

注意在GPIO26和地之间添加一个0.1uF的陶瓷电容。没有这个,GPIO26会在任何时候触发关机 我将USB电源线插入PowerBoost。我不是为什么会发生这种情况,但可能是因为相对而言 当连接USB电缆时,面包板上的长导线作为无线电天线拾取某种尖峰。 然而,这很容易解决,通过添加电容器来平滑线路到GPIO26。


### RasPi 3电路

RasPi 3型改变了GPIO14的运行方式。该方法有两个很小但重要的变化:

使用GPIO14的串行控制台的Pre-RasPi3使用硬件UART,这就是使GPIO14达到高电平的目的。 RasPi 3使用不这样做的软件UART。但是我们可以通过在raspi-config中启用串行控制台来模拟这个 并在100K电阻上添加一个电容,以平滑GPIO14上的电压,并将PowerBoost使能引脚保持在高电平。

  1. Check you have the correct configuration in raspi-config - see Installation below
  2. Place a 100uF electrolytic capacitor in parallel to the 100K resistor, as shown in this schematic.

我使用了 100uF 16V电容器

此解决方案由 Daniel Bull Simon Claessen LiPoPi 项目 - 所有的细节都是这里

100μF电容器不应用于Pre-Pi 3电路。


为了评估电池剩余的电量,我们可以使用PowerBoost1000C上的引脚。 Pi没有模数转换器(ADC) 本身,因此我们需要以 MCP3008 的形式添加一个外部的,它是一个8通道10位ADC使用SPI接口。 我们只使用两个频道,所以这有点过分,但是它们并不昂贵(约4美元)。

PowerBoost上的 USB 引脚具有输入USB连接的电压。当电缆未连接时,为0V,连接时约为5.2V。 标称地,USB电源提供5V,但制造商经常会碰到这一点以抵消任何电压下降 供电电缆。

测量的USB电压用于确定电缆是否连接。通过ADC进行此操作是过度的,但是 因为我们需要ADC的电池电压,所以使用它是有意义的。

蝙蝠引脚上的电池电压真正告诉我们当前的电池状态。这具有LiPoly电池的输出电压, 当充满电时,其从耗尽时的3.7V变化到约4.2V。

ADC的模拟端的最大输入电压为3.3V,因此我们需要使用以下功能降低PowerBoost电压 一对电阻充当电压分配器。在这种情况下,6.8K和10K电阻的组合 使最大输入电压达到所需范围。

ADC使用了四个GPIO引脚的RaspberryPi侧的SPI接口。该接口的代码由Adafruit编写 团队和管理SPI软件,而不是Pi的硬件​​SPI接口,因此您可以根据需要选择其他GPIO引脚。



系统需要一种告诉用户电池剩余电量的方法。最简单的方法之一是控制红色和绿色LED 从RasPi实现一个简单的界面 - 绿色是好的,红色意味着电池电量不足。

更好的是使用RGB LED并控制红色和绿色组件 - 甚至可以变黄。大多数RGB LED都是普通阳极,这意味着你 需要使用这个电路。


如果您这样做,您将需要编辑pi_power_leds.py并注释/取消注释相关行 - 请参阅代码中的注释。



这是我实际的面包板看起来像 - 不漂亮,但功能

创建面包板布局以使电路易于理解(您可能会争论我是否成功…)。 其一个结果是与紧凑型电路板相比,多个跳线相对较长。 这可能会有一些不必要的副作用。

连接到RasPi GPIO引脚的长导线可以充当无线电天线。即使代码使用内部下拉电阻来避免引脚浮动, 当我插入PowerBoost 1000C输入USB时,我确实有关闭引脚(电路中的GPIO26)遇到触发问题 电缆断开后,电缆返回。 我解决了,通过在GPIO26和Ground之间放置一个0.1uF的陶瓷电容。

另外,在更长的电线和面包板轨道上可能会有电压降。 PowerBoost 1000C的Adafruit指南提到了这一点。

请不要仅仅将电路从面包板图中连接起来 - 首先了解电路 - 您可能会想出更整洁的布局, 更重要的是,我可能在创建图表时犯了错误。

这是另一个面板板布局的照片,我已经去了一个更紧凑的布局。请注意,电线短得多,我没有 需要断电电路上的0.1uF电容。


Pi Power的软件端分为两部分。

pi_power.py 完成所有工作。它监视电池,处理掉电过程 就像关机一样,电池电量耗尽。它每分钟检查电池并记录当前状态 在/home/pi/.pi_power_status文件中。



  • - 调试将在每次检查(每分钟一次)时输出电池和USB电压
  • - 日志将相同的信息输出到文件 pi_power_log.csv ,可用于生成电压随时间的曲线
  • - 安全延迟系统关机2分钟。请参阅安装部分了解如何使用。

有很多方法可以通知用户电源状态。您可能在屏幕或条形图上显示数字显示 像你在一些手机上。分离监视和显示组件可以使其他人更容易构建 不同的显示器。

我选择了使用红色和绿色LED指示一般电源状态的简单方法。 pi_power_leds.py 检查状态文件,并根据此设置led。 很容易改变具体的LED模式,但默认情况是:

  • 绿色,闪烁 - USB电源已连接 - 电池正在充电
  • 绿色,恒定电池 - 超过25%的电池剩余
  • 红色,恒定电池 - 超过15%的电池剩余
  • 红色,闪烁 - 电池 - 超过10%的电池剩余
  • 红色,闪烁快速 - 电池 - 少于10%的电池剩余 - 系统将很快关闭

以下是电池电压随时间的曲线图,标示显示LED模式对应于电压(RasPi Zero和2700mAh电池)

脚本每隔30秒检查一次状态文件,以便在USB电源插头和LED之间存在延迟 更新。减少轮询间隔会改善这一点。



上电功能依赖于用于RasPi UART串行接口的GPIO14。在安装pi_power脚本之前,您需要 配置如下:


运行 sudo raspi-config ,在高级选项下选择串行,然后选择。 这样可以防止Pi使用GPIO 14作为控制台(这将关闭电源)。


运行 sudo raspi-config ,在高级选项下选择串行,然后选择。这应该是默认的

Python上的Python - 如果您没有Python,并且您的Pi上安装了Rpi.GPIO库,那么您将需要执行以下操作。

sudo apt-get update
sudo apt-get dist-upgrade

sudo apt-get install python-dev

sudo apt-get install python-setuptools sudo easy_install rpi.gpio



引导您的Pi并下载pi_power.py和pi_power_leds.py,并使其可执行 chmod a + x pi_power.py 等。

使用pi_power.py的 - debug 选项在终端窗口中运行脚本。

$ ./pi_power_leds.py &
$ ./pi_power_debug.py –debug

确保eveything按预期工作 - LED显示正确的状态,您可以关闭并重新启动系统等。 你想测试所有的功能:

  • 从按钮开机
  • 从按钮
  • 关闭正在运行的系统
  • 使用和不使用USB电缆检查所有LED模式,允许系统排出电池
  • 让电池耗尽电量并触发低电量关机





注意 Pi引导过程与其他Linux系统不同,因为没有单用户模式。那 使得很难在启动脚本中修复问题,因为您没有简单的方法来运行系统 直到但不包括问题脚本。所以…在把它们放在一个之前,一切都很好 系统启动脚本

要执行此操作,您需要将脚本的完整路径添加到 /etc/rc.local

/home/pi/pi_power_leds.py &
/home/pi/pi_power.py –safe &

请注意 - 安全选项。在测试时使用。它会延迟系统关机两分钟。 电路中的某些错误可能会立即关机。如果你遇到这种情况,这个延迟给你两个 分钟编辑 rc.local ,并在解决问题时注释掉这些行。

一旦你测试了你的系统,你确信一切都很好,你可以删除 - 安全选项。

/home/pi/pi_power_leds.py &
/home/pi/pi_power.py &




  • Raspberry Pi 2型号B V1.1(2014)
  • Raspberry Pi Zero v1.2(2015)
  • Raspberry Pi 3型号B V1.2(2015) - 使用上述Pi 3修改

请查看维基,了解有关电池充电的更多背景知识, Pi等。

The power on / power off machinery is taken from the LiPoPi project from Daniel Bull, which I have contributed to. If you want a simpler solution without the voltage monitoring machinery then LiPoPi might be just what you need.

目前,软件无法知道电池何时完全充电。在PowerBoost上,黄色充电指示灯变为绿色,因此 信息可用。没有什么简单的方法可以得到它。从电压准确估计它似乎不起作用 在实践中。

Adafruit还出售PowerBoost 500C充电器。这可以输出500mA的最大电流,而不是1000C的1A。更重要的是,500C 可以为其输出供电或为电池充电,但不能同时充电。在为Pi供电时,1000C可以为电池充电,所以这是使用的。

电源开/关机制的一个缺点是您不能再使用 shutdown -r now 命令重新启动机器。 该系统将关闭PowerBoost,该PowerBoost可以重新启动电源。只需关闭并按下按钮即可获得相同的效果。

对于移动系统,您可能会试图跳过PowerBoost和Pi之间的笨重USB电缆,只需将输出5V和地线 到GPIO头上的相应引脚。这样做不起作用,因为它似乎绕过了Pi上的电源控制电路。 使用这种配置插入Pi的HDMI显示器,PowerBoost上的蓝色输出LED闪烁,暗示了一些 当前路径从HDMI显示器返回到PowerBoost。我未能进一步解决问题。