Cheap UPS for Raspberry Pi 4
by R4iNER in Circuits > Raspberry Pi
324 Views, 2 Favorites, 0 Comments
Cheap UPS for Raspberry Pi 4
This UPS solution offers a cheaper alternative to the HAT UPS
available on the market, while maintaining similar functionality.
The primary trade-off is the necessity for some modifications, which require soldering skills and tools. If you are not comfortable with soldering, you might need help from a skilled friend.
The HAT option was unsuitable for my needs because I wanted to mount the batteries separately from the Raspberry Pi. This setup allows for the batteries to be enclosed in a protective case, with connections made via wires.
The UPS module used employs the TC4056A IC, which provides automatic charge protection and battery voltage monitoring. However, it only features a "STAND BY" pin/LED to indicate when the battery is fully charged.
To ensure the Raspberry Pi shuts down when the battery is too low, I incorporated the INA226 module. This module enables voltage monitoring, current measurement, and communication over the I2C bus, providing the necessary data to manage power effectively.
For power, I use the original Raspberry Pi wall charger to charge the batteries and supply power to the Pi. This charger connects to the USB-C port of the UPS module.
Features
- battery protection: overcharge/discharge protection, over current protection, trickle charge and balancing
- I2C bus communication with INA226, monitoring the battery voltage and current draw in real time
- battery temperature alert (optional with NTC thermistor)
- power loss sensing and automatic switching to UPS mode
- automatic shutdown if the batteries are below minimum voltage
- 5V output up to 2.5-3A
The bad and the ugly
- no "Off switch" for the UPS module, batteries need to be removed (or slide a thin plastic between the terminals)
- no Battery polarity protection, mount them backwards and the chip fries :|
- no way to determine the real remaining capacity (battery capacity degrades and fluctuates, the load is not constant)
Disclaimer
Please note that this project involves working with electrical components and batteries, which can be dangerous if not handled properly. Ensure you follow all safety precautions and double-check your work. I am not responsible for any damage, injury, or loss that may occur as a result of following this guide. Proceed at your own risk and always prioritize safety.
SAFETY CAUTIONS!
- Lithium-ion (Li-ion) and lithium-polymer (Li-po) batteries are inherently unstable and may cause fires, personal injury, or property damage if not properly charged or used.
- Do not mix old and new batteries or use batteries from different brands.
- When purchasing lithium batteries, confirm that the battery specifications are compatible with the expansion board. Choose batteries from reputable manufacturers and verify their stability and safety through aging tests.
- Ensure the correct polarity when charging or discharging the battery. Avoid using substandard chargers or charging panels.
- Lithium batteries have a limited cycle life and will degrade over time. Replace them when they reach their maximum cycle life or after two years of use, whichever comes first.
- Store batteries carefully and properly, keeping them away from flammable and explosive materials, out of reach of children, to prevent safety accidents from careless storage.
- Do not expose battery pack to direct sunlight (heat) for extended periods.
- Always monitor charging process to assure batteries are being charged properly.
Supplies
Not all the parts are needed if you have some components left from other projects: resistors, MLC capacitors, wires or 18650 batteries.
- INA226 module
- Type-C 15W 3A Fast Charge UPS Power Supply
- SMD Alloy Resistor 2W R008
- MLC capacitor 1uf (SMD 0805) and 22uf (SMD 1206)
- 1.8k/3.3k resistors or 1k/2k for R1/R2 divider
- Ribbon cable 20cm M-F Jumper Breadboard Wire Colorful GPIO Ribbon
- 22AWG (0.64mm) for 5V power
- 2x18650 Li-Ion batteries
Fixing the UPS Filtering, Replacing Ina226 Shunt
The electrolytic capacitors on the output need to be replaced with MLC ones otherwise the output is too noisy. For more details about the noise problem/fix you can check this Great Scott video.
To be able to measure current up to 3A the Ina226 module needs a shunt resistor R008 (0.008Ohm), the board comes with a R100 shunt which is not enough for the RASPi current draw. The next step is to swap the original shunt with the R008 SMD resistor.
Creating a Voltage Divider
Another piece of the puzzle is to add a notification system for when the power from the wall charger is disconnected. Since the Raspberry Pi's GPIO pins are not tolerant of 5V signals, it is essential to shift the signal down to 3.3V. This can be achieved using a level shifter module, a voltage divider, a potentiometer etc.
I used a voltage divider made with two resistors soldered between 5V+ and 5V- on the UPS module board, the soldered resistors were protected by some heat shrink tubes.
The R1 is 1.8k and R2 3.3k with R1 connected to the 5V+, you can use any other combination of values for resistors to get the voltage from ~5V to 3.3V.
A voltage divider calculator comes in handy to calculate the resistor values.
Connecting All the Wires
The first image is the connection schematic between the Ina226 module, UPS module and RASPi.
The Vin+ and Vin- wires should be sized for ~3A load so I used 22AWG (0.64mm) wires.
For the SDA,SCL,3.3V and Gnd I used a ribbon cable to minimize the tangling but any wires can be used for those connections.
Vbus is soldered to the battery + pin on the UPS board, the orange wire in capacitor fix image.
Python Code
Enable I2C Interface
Make sure the I2C interface is enabled:
sudo raspi-config
Select Interfacing Options -> I2C ->yes to start the i2C kernel driver
Reboot the system.
Clone the repo and follow the instructions from "Install and usage" : pi4UPS repository
python3 powerUPS.py
After configuring and running the command above you should get the output from the last image. This indicates that the software is functioning correctly.
You can now proceed to the "To install as a service" section to install the script as a service.
Once the service starts successfully, the script will run in the background, monitoring for power outages. You can simulate a power outage by unplugging the USB-C cable.
Upon reconnecting the cable, the UPS module will resume charging the batteries to full capacity, and these events will be logged in the "events.log" file.
If power is not restored, the UPS will automatically shut down the Raspberry Pi when the battery voltage drops below 3.2V.
Troubleshoting
If the INA226 module is not detected you'll have to check the module address:
You can check the address with the following commands, the default address used is 0x40.
sudo apt-get install i2c-tools
sudo i2cdetect -y 1
If the address is different than 0x40 you'll have to add "address=0xNUMBER" in ups_daemon.py (pi4UPS.py line 36) file at line 31:
ina = INA226(busnum=1, address=0xNUMBER, max_expected_amps=max_amps, shunt_ohms=shunt, log_level=logging.ERROR)
Congratulations
Congratulations on successfully assembling your very own UPS for the Raspberry Pi 4! You've skillfully navigated through this Instructable to create a smart and functional UPS device.
Thank you for following along with this guide. I hope you found it informative and inspiring. Keep building, experimenting, and pushing the boundaries of what you can achieve!