Last summer, a small guesthouse in Ürgüp asked me to figure out why their Raspberry Pi kept losing power. They run a simple check-in display and a backup Wi-Fi AP. The Pi would reboot randomly, sometimes twice a day. They had a 10,000 mAh USB power bank between the adapter and the Pi, expecting it to act as a UPS. It didn’t. The power bank would cut output when the input dropped, so the Pi lost power anyway. I needed to log voltage and current over time to see the pattern.
I used an INA219 sensor on a Pi Zero W, logging to a CSV every 2 seconds. The sensor sits on the 5V line between the power bank and the Pi. The Pi Zero itself was powered from a separate 5V adapter, so it wouldn’t die when the main line dropped. Here’s the core script:
import csv, time
from ina219 import INA219
ina = INA219(0.1, address=0x40)
ina.configure()
with open('power_log.csv', 'a', newline='') as f:
w = csv.writer(f)
w.writerow(['timestamp', 'voltage', 'current_mA'])
while True:
v = ina.voltage()
i = ina.current()
w.writerow([time.time(), f'{v:.2f}', f'{i:.1f}'])
f.flush()
time.sleep(2)
I ran it for a week. The log showed voltage dipping to 4.6V for 3–5 seconds every time the fridge compressor kicked in. The power bank’s low-voltage cutoff is around 4.5V, so it would shut off. The fix was to put a 5V 2A adapter with a large capacitor (1000µF) on the Pi’s input, and remove the power bank. The Pi now rides through the dips. The trade-off: no battery backup for long outages, but the guesthouse has a generator for those.
This approach fails if the voltage sag is longer than a few seconds or if the current spikes above the INA219’s range (3.2A max). Also, the INA219 needs a stable 3.3V I2C reference; on a Pi Zero, the 3.3V rail can droop if you’re not careful. I’ve seen the sensor report garbage when the Pi’s own supply is marginal. If you need to log during a full outage, you’ll need a separate logger with its own battery. And don’t trust the power bank’s claimed capacity; the cutoff behavior varies wildly between models.
What’s your go-to method for logging power events when the main supply is the thing that’s failing?
0 条评论
还没有评论,写下第一条。