## 如何用电池驱动 Arduino 持续数年
How to run an Arduino for years on a battery (2021)

原始链接: https://makecademy.com/arduino-battery

## 构建低功耗Arduino系统 本文详细介绍了如何构建一个电池供电的Arduino系统,用于自主项目,例如无线传感器,并显著延长电池寿命。由于始终开启的组件,直接将Arduino Uno连接到电池会迅速耗尽电量。解决方案是使用ATMega328微控制器、晶体振荡器、电容器和电阻器创建一个最小的Arduino设置——全部组装在面包板上。 该过程包括使用标准的Arduino Uno板对微控制器进行编程,然后将其转移到由电池供电的面包板设置(约3V,来自两节AA电池)。初步测试使用“blink”草图来验证功能。 至关重要的是,使用JeeLib库优化功耗,该库允许微控制器在不活动时进入睡眠模式。这大大降低了电流消耗——LED关闭时从6.7mA降至仅43µA——将潜在的电池寿命从几周延长至*几年*,适用于活动频率较低的应用,例如每10秒测量一次温度的温度传感器。这种方法适用于活动时间与睡眠时间相比很短的任何项目。

## 黑客新闻讨论:Arduino 长期电池供电 最近黑客新闻上出现了一场讨论,围绕着一篇2021年的文章,该文章详细介绍了如何让Arduino用电池运行数年。 讨论很快深入到低功耗技术和相关库的具体细节,特别是 **JeeLib**,一些用户认为它的文档不完善,并且可能已被废弃,并链接到了它的GitHub和存档网站。 用户强调了利用像ATmega328(Arduino常用的芯片)这样的微控制器中提供的深度睡眠模式的重要性,以及像MSP430和STM32这样的替代方案,它们可以实现低至1µA的电流。 优化低功耗不仅仅是关于微控制器;高效的电压调节器至关重要。 讨论还涉及了Arduino在高级低功耗项目中的局限性,一些人认为它最适合初学者。 更有经验的开发者通常更喜欢使用AVR-libc直接编程,以获得更大的控制权和效率。 添加像WiFi这样的功能会显著增加功耗,使得实现数年的电池寿命变得困难。 最后,出现了一个怀旧话题,讨论了像TRS-80 Model 100这样的设备,以其卓越的电池续航能力而闻名,以及现代同类产品的潜力。
相关文章

原文

If you found this article after doing a search on Google, welcome! On this website you will find plenty of content around DIY home automation using open-source hardware. Enjoy the article!

For most of the Arduino tutorials you will find on this website, power is usually not an issue as the Arduino is powered by the USB cable coming from the computer. However, sometimes you want to build systems that are going to be autonomous and powered by a battery.

For example, you want to power a wireless motion detector just by using a set of batteries. The first idea would be to connect directly an Arduino board like the Arduino Uno R3 to a battery. Easy, right ? Well, it would work, but your battery would be depleted in a matter of days because some components like voltage regulators are always sucking power. So we need something better.

The first thing we need is to build our own Arduino system with just the minimal set of components. The second part is to optimise the software so that the system only consumes power when it is actually doing something. And that’s exactly what we will see in this article.

Hardware & Software Requirements

You need several components to build you own Arduino system. The main piece of this project is of course the microcontroller that will run your Arduino sketches, like the ATMega328. You will need a chip with the Arduino bootloader. Buy your chip with it, it will make your life easier.

In a previous project I used a FTDI breakout board to program the Arduino chip directly on the breadboard. But for this project I won’t have any external power running on the breadboard so I will just use an Arduino Uno board to program the microcontroller.

To power the Arduino, you will need a battery. The best is to power the Arduino directly from the battery, so you don’t have to use any voltage regulators that will suck some power. I used a set of two AA batteries (1.5V each) with a battery holder thus powering the microcontroller with around 3V, which is fine according to the ATmega328 documentation.

You will also need several components around the chip. You will need one 10uF capacitortwo 22pF capacitorsone 10K Ohm resistorone 220 Ohm resistorone green LED, and one 16MHz crystal clock.

Finally, you will need a breadboard and some jumper wires.

This is the list of the components that were used in this article:

On the software side, you just need the usual Arduino IDE. Note that in this tutorial, we will only cover a tiny part of the possibilities offered by the Arduino platform.

Hardware Configuration

This project is a bit complex to build, so be sure to follow the instructions. This is how it should look like at the end:


First, put the microcontroller in the center of the breadboard. You can then take care about the power: connect power lines on each side, connect the negative power rail to the two GND pins of the microcontroller, and the positive power rail to VCC, AVCC, and AREF. Also, add the 10uF capacitor between two power rails. Finally, add the battery to the system.

You also have to add the crystal between the X1 and X2 pins, with 22pF capacitors going from each pin to the ground. Also, you need to connect the RST pin to the positive power rail using a 10K Ohm resistor. To see if the system is working, connect the green LED in series with a 220 Ohm resistor to the digital pin 13 of the Arduino board, the other side going to the ground.

This is the complete schematics of the project:


Testing your Arduino system

It’s now time to test if the hardware part is working. What I did in this project is to use the Arduino Uno board to program the chip, and then I just “transplanted” the chip on the breadboard. You can just use the default “blink” sketch to program the microcontroller. After this is done, just replace the chip on the breadboard, and plug your battery (my battery pack even has a nice on/off switch). The LED should just goes on and off every second as expected.

Optimizing for Low-power

So now, we have an autonomous Arduino system. But it still consuming way too much power. Indeed, even when the LED is off, the Arduino chip is still active and consumes power. But there are functions on the microcontroller to put it to sleep during the time it is inactive, and re-activate the chip when we need to change the state of an output or to perform some measurements. I tested many solutions to really reduce the power to the lowest value possible, and the best I found is the JeeLib library. You can just download it and install it by placing the folder in your Arduino/libraries/ folder.

This is the sketch I used:

#include <JeeLib.h;>
ISR(WDT_vect) { Sleepy::watchdogEvent(); }
Sleepy::loseSomeTime(5000);

You basically just have to include the JeeLib library with:

#include <JeeLib.h;>

Then initialize the watchdog with:

ISR(WDT_vect) { Sleepy::watchdogEvent(); }

Finally, you can put the Arduino to sleep for a given period of time with:

Sleepy::loseSomeTime(5000);

Upload the sketch with the Arduino IDE and replace the chip on the breadboard. You should see your Arduino having the same behavior as before (with 5 seconds intervals). But the difference is that now when the LED is off, the Arduino chip doesn’t use a lot of power. To finish this article, I wanted to actually quantify the power consumption of the system we just built. You can do the exact same by placing a multimeter between one of the power lines. For example, I connected the positive pin of the battery to one pin of my multimeter, and the other pin to the positive power rail of the breadboard. Here are the results:

  • LED off, without the JeeLib library: 6.7 mA
  • LED on, without the JeeLib library: 8.8 mA
  • LED off, with the JeeLib library: 43 uA (!)
  • LED on, with the JeeLib library: 2.2mA

From these results, we can see that our breadboard-Arduino consumes 6.7 mA when doing nothing without caring about putting it to sleep. For information, that will drain your two batteries in about a month. Which is actually not so bad, but we can do better. With the sleep functions, this can be reduced to 43 uA, which is a 150x improvement.

Let’s do some calculations to see how it will impact a real project, for example a temperature sensor. It takes about 500 ms to perform a measurement, at about 2.5 mA of current. Then, the systems sleeps for 10 seconds and the loop starts again. The “mean” is then 0.16 mA over a complete loop. With batteries rated at 2500 mAh, it means in theory the system will last … nearly 2 years without changing the batteries! Of course, some other effects will actually modify this number, but it gives you an idea.

How to Go Further

Really, you can adapt this idea to every system where the active time is small compared to the sleep time, and make your Arduino last for years without changing the battery!

联系我们 contact @ memedata.com