STFN

Powering Raspberry Pi Pico with AA batteries (Weather Station Part III)

15 minutes

Continuing on what we did in part II of making a RPi Pico Weather Station, now it’s time to untether the Pico from the PC.

The simplest way is to connect the Pico to a USB charger or a powerbank via its USB port, it does not require any additional hardware and works out of the box, but it has some drawbacks.

The main downside of powering a Pico from a powerbank is that such devices often have a minimal required power draw for them to work, and if the connected device uses less energy, the powerbank assumes it was disconnected and turns off. While this causes no problems for phones etc, it can lead to bad consequences when using a microcontroller like a Pico. When a Pico goes to sleep (for example when waiting during time.sleep()) it draws so little energy that the powerbank may assume there is nothing connected, turns off, and the Pico never wakes up.

That’s way a dumber energy source will be a better energy source for our needs. In this episode I will show how to power a Pico from 4 AA batteries.

Going back to the Pico Pinout diagram, the pin that interests us is VSYS, the second pin on the right when looking at the Pico with the USB port at the top.

To quote the Pico datasheet:

VSYS is the main system input voltage, which can vary in the allowed range 1.8V to 5.5V, and is used by the on-board SMPS to generate the 3.3V for the RP2040 and its GPIO.

What we need to carry out from that sentence is that the Pico can be powered by providing positive voltage to that pin and that voltage needs to be between 1.8V and 5.5V. This means that rechargeable AA batteries will be perfect for the job, as fully charged their voltage is around 1.3V, and four of them in series should provide us with 5.2V, well within the acceptable range.

Only use rechargeable batteries for this project, Alkaline (li-ion) AAs have a higher full charge voltage of around 1.5V, four of them will amount to 6V which can be tragic for the Pico. Be careful and if unsure, check the voltage with a multimeter before connecting them to the board.

As the microcontroller runs, the batteries will slowly lose their charge and their voltage will drop. Once the combined voltage reaches 1.8V, the board will turn off and the batteries will need to be recharged.

Connecting batteries to Raspberry Pi Pico

To connect the battery holder, I used an ARC screw connector that I soldered to the board and place short cables between its leads and between it and the Pico’s VSYS and GND pins. Always double, triple check the polarity! The red cable from the battery pack is the positive that needs to go to VSYS and the black one needs to be connected to GND.

The battery holder is a simple four AA holder. The batteries are connected in series, so their voltages add up, and the whole pack has the capacity of a single battery.

Before connecting the holder cables to the Pi, make sure the battery holder is turned off.

Making the code run without a PC connection

There is a few things that we need to change with our code that we created in Part II. This is the first time in the series where filenames become important. Raspberry Pi Pico with Micropython has two special filenames: boot.py and main.py.

boot.py is always run when the Pico is powered up. There is no need to call it from an IDE like Thonny, it will run automatically. This is where we can do some initial setup, like connecting to a network. Once boot.py finishes successfully, the execution is passed to main.py, in which there will be our main program loop. With every cycle of that loop, the Pico will measure the environment data, send it to the MQTT broker, and fall asleep for a certain amount of time, and the loop will go into another cycle. Forever, or until the power dies.

Let’s go with code. First the booting part, with no changes to what was in the previous episode.

boot.py

import time
import network
import machine

led = machine.Pin("LED", machine.Pin.OUT)

ssid = "NETWORK-SSID"
password = "NETWOTK-PASSWORD"

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)

max_wait = 10
while max_wait > 0:
    led.on()
    time.sleep(0.1)
    led.off()
    if wlan.isconnected():
        break
    max_wait -= 1
    time.sleep(1)

if not wlan.isconnected():
    for _ in range(5):
        led.on()
        time.sleep(0.1)
        led.off()
        time.sleep(0.1)
        led.on()
        time.sleep(0.1)
        led.off()
        time.sleep(0.7)
    time.sleep(1)
    machine.reset()
else:
    status = wlan.ifconfig()
    led.on()
    time.sleep(3)
    led.off()

And then the main part. Compared to the previous version of the code, the one major change is that the measuring and sending is now inside an endless loop. I’ve set the sleep time between measurements to 30 seconds, as I find a reasonable resolution for doing temperature readings. Reducing the sleep time will increase the data resolution, increasing it will extend the battery life.

main.py

import json
import time

import bme280
import dht
import machine
import network
from umqtt.simple import MQTTClient

led = machine.Pin("LED", machine.Pin.OUT)

SCL_PIN = machine.Pin(1)
SDA_PIN = machine.Pin(0)
DHT_PIN = machine.Pin(15)

i2c = machine.I2C(0, scl=SCL_PIN, sda=SDA_PIN)
bme_sensor = bme280.BME280(i2c=i2c)
d_sensor = dht.DHT11(DHT_PIN)

wlan = network.WLAN(network.STA_IF)

while True:
    if not wlan.isconnected():
        machine.restart()
    temperature, pressure, _ = bme_sensor.read_compensated_data()
    temperature = temperature / 100
    pressure = pressure / 256 / 100

    d_sensor.measure()
    humidity = d_sensor.humidity()

    payload = {
        "temperature": temperature,
        "pressure": pressure,
        "humidity": humidity,
    }

    qt = MQTTClient("umqtt_client", "192.168.0.176", keepalive=3600)
    qt.connect()
    qt.publish(b"sensors", json.dumps(payload))
    qt.disconnect()
    led.on()
    time.sleep(0.5)
    led.off()
    time.sleep(30)

Fire it up!

Now it’s the moment of truth. Make sure both files are saved on the Pico. Unplug it from the PC. If you haven’t done it already, connect the battery holder to the Pico. Once again, double check the polarity and whether the cables are connected to the right pins.

Turn on the battery holder. The Pico LED should start blinking slowly as it’s connecting to the Wifi. If successful, there should be a long blink confirming it has connected and sending data. If there are double quick blinks, it means it has failed connecting and will restart and try again. If there were a few unsuccessful retries, turn of the batteries, connect it back to the PC and check everything again, pay special attention to the Wifi credentials.

And that’s it, you are done and can boast your own, battery operated, Raspberry Pi Pico Weather Station!

How long will it last

The Pico just turned off with batteries depleted.

I did several tests of how long the Pico can run from AA batteries. With the 2450mAh IKEA LADDE batteries, I’ve been getting uptimes of around 50 hours maximum with the environment sensing code seen above, in temperatures betwenn 10 and 0C.

Of course the timespan will depend on a lot of factors:

And that’s it, you should now have a Raspberry Pi Pico that can independently of a wall socket or you computer. In the future I am planning to try running a Pico from a li-ion 3.7V battery together with a TP4056 battery charging module, and compare it with using AA batteries. We’ll see how it turns out!

If you found this article useful, please consider to