Expand description
§waveshare-ups-hat-e
A Rust library for monitoring the Waveshare UPS HAT (E) on Raspberry Pi via I2C.
§Example Output
Output from the ups_monitor example demonstrating the type of information available:

§Usage
use waveshare_ups_hat_e::UpsHatE;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut ups = UpsHatE::new();
let battery = ups.get_battery_state()?;
println!("Battery: {}% ({} mV, {} mA)",
battery.remaining_percent,
battery.millivolts,
battery.milliamps);
println!("Remaining: {} mAh, {} min",
battery.remaining_capacity_milliamphours,
battery.remaining_runtime_minutes);
let power = ups.get_power_state()?;
println!("Charging: {:?}, Activity: {:?}",
power.charging_state,
power.charger_activity);
println!("USB-C: {:?}, PD: {:?}",
power.usbc_input_state,
power.usbc_power_delivery);
let vbus = ups.get_usbc_vbus()?;
println!("VBUS: {} mV, {} mA, {} mW",
vbus.millivolts,
vbus.milliamps,
vbus.milliwatts);
let cells = ups.get_cell_voltage()?;
println!("Cells: {:?}", cells);
Ok(())
}§API
| Method | Description |
|---|---|
get_battery_state | Voltage, current, capacity, remaining runtime |
get_power_state | Charging state, USB-C input, power delivery mode |
get_cell_voltage | Individual cell voltages (4 cells) |
get_usbc_vbus | USB-C voltage, current, power |
get_communication_state | Communication status with BQ4050 and IP2368 chips |
get_software_revision | Firmware revision of the UPS microcontroller |
is_battery_low | Check if battery voltage is below threshold |
is_power_off_pending | Check if a power-off has been initiated |
force_power_off | Initiate power-off in 30 seconds (cannot be canceled) |
§License
Licensed under either of Apache License, Version 2.0 or MIT License at your option.
§Copyright
Copyright (c) 2025 Stuart Stock, all rights reserved.
Modules§
Structs§
- Battery
State - Aggregate battery state of the UPS Hat E.
- Cell
Voltage - Voltage readings for each of the four battery cells.
- Communication
State - Ability of the UPS to communicate with the on-board BQ4050 gas gauge chip and IP2368 battery charge management chip.
- Power
State - Represents the composite power state of the UPS Hat E.
- UpsHatE
- Monitor a Waveshare UPS HAT E (Uninterruptible Power Supply model E) for a Raspberry Pi.
- UsbCV
Bus - Voltage and current readings from the USB-C port.
Constants§
- DEFAULT_
CELL_ LOW_ VOLTAGE_ THRESHOLD - The default threshold for low cell voltage, in millivolts. The UPS Hat E low-voltage cutoff is observed to be 3.2V (not documented), using 3.4V for our cutoff so there’s enough power remaining to run a shutdown sequence.
- DEFAULT_
I2C_ ADDRESS - Default I2C address of the Waveshare UPS Hat E
- DEFAULT_
I2C_ DEV_ PATH - The default I2C bus device path to interface with the UPS Hat E
- POWEROFF_
VALUE - Value to write to the [
POWEROFF_REG] register to initiate a power-off, or if read from [POWEROFF_REG], indicates that a power-off is pending.