|
1 | | -# MicroPython BQ27441 Library |
2 | | - |
3 | | -This library is a port of the [SparkFun BQ27441-G1A LiPo Fuel Gauge Arduino Library](https://github.com/sparkfun/SparkFun_BQ27441_Arduino_Library). |
4 | | - |
5 | | -# Features |
6 | | - |
7 | | -* I²C communication |
8 | | -* battery voltage measurement |
9 | | -* state of charge (SoC) |
10 | | -* remaining and full capacity |
11 | | -* average, standby and max current |
12 | | -* average power measurement |
13 | | -* state of health (SoH) |
14 | | -* battery and internal temperature |
15 | | -* configurable design capacity |
16 | | -* power management (shutdown / wake-up) |
17 | | -* GPOUT interrupt configuration (SOC_INT / BAT_LOW) |
| 1 | +# BQ27441 MicroPython Driver |
| 2 | + |
| 3 | +MicroPython driver for the **Texas Instruments BQ27441-G1A** LiPo fuel gauge. |
| 4 | + |
| 5 | +This library is a port of the [SparkFun BQ27441 Arduino Library](https://github.com/sparkfun/SparkFun_BQ27441_Arduino_Library). |
18 | 6 |
|
19 | | -# I2C Address |
| 7 | +## Supported Device |
20 | 8 |
|
21 | | -* **Default address:** `0x55` |
| 9 | +| Parameter | Value | |
| 10 | +| -------------------- | ---------------- | |
| 11 | +| Device | BQ27441-G1A | |
| 12 | +| Interface | I²C | |
| 13 | +| Default I²C address | `0x55` | |
| 14 | +| Operating voltage | 2.5–4.5 V | |
| 15 | +| Default capacity | 650 mAh | |
22 | 16 |
|
23 | | -**Important:** |
24 | | -The BQ27441 only responds on I²C when a **battery is connected**. |
| 17 | +**Note:** The BQ27441 only responds on I²C when a battery is connected. |
25 | 18 |
|
26 | | -# Basic Usage |
| 19 | +## Features |
| 20 | + |
| 21 | +* Battery voltage measurement (mV) |
| 22 | +* State of charge (%) |
| 23 | +* Remaining and full capacity (mAh) |
| 24 | +* Average, standby, and max current |
| 25 | +* Average power |
| 26 | +* State of health (%) |
| 27 | +* Battery and internal temperature |
| 28 | +* Configurable design capacity |
| 29 | +* Power management (shutdown / wake-up) |
| 30 | +* GPOUT interrupt configuration (SOC_INT / BAT_LOW) |
| 31 | +* Seal / unseal device control |
| 32 | + |
| 33 | +## Basic Usage |
27 | 34 |
|
28 | 35 | ```python |
29 | 36 | from machine import I2C |
30 | 37 | from bq27441 import BQ27441 |
31 | 38 |
|
32 | 39 | i2c = I2C(1) |
33 | | - |
34 | 40 | bq = BQ27441(i2c) |
35 | 41 |
|
36 | | -voltage = bq.voltage_mv() # mV |
37 | | -soc = bq.state_of_charge() # % |
| 42 | +print("Voltage:", bq.voltage_mv(), "mV") |
| 43 | +print("Charge:", bq.state_of_charge(), "%") |
| 44 | +print("Current:", bq.current_average(), "mA") |
| 45 | +``` |
| 46 | + |
| 47 | +## API Reference |
38 | 48 |
|
39 | | -print("Voltage:", voltage, "mV") |
40 | | -print("State of Charge:", soc, "%") |
| 49 | +### Initialization |
| 50 | + |
| 51 | +```python |
| 52 | +bq = BQ27441(i2c, address=0x55) |
| 53 | +``` |
| 54 | + |
| 55 | +### Quick Measurements |
| 56 | + |
| 57 | +* `bq.voltage_mv()` — battery voltage in mV |
| 58 | +* `bq.state_of_charge()` — battery level in % (filtered) |
| 59 | +* `bq.capacity_remaining()` — remaining capacity in mAh |
| 60 | +* `bq.capacity_full()` — full charge capacity in mAh |
| 61 | +* `bq.current_average()` — average current in mA |
| 62 | +* `bq.state_of_health()` — battery health in % |
| 63 | +* `bq.power()` — average power in mW |
| 64 | + |
| 65 | +### Advanced Measurements with Type Constants |
| 66 | + |
| 67 | +Several methods accept a type parameter for different measurement modes. The type constants are defined in `bq27441.device`: |
| 68 | + |
| 69 | +```python |
| 70 | +from bq27441.device import CurrentMeasureType, CapacityMeasureType |
| 71 | +from bq27441.device import SocMeasureType, SohMeasureType, TempMeasureType |
41 | 72 | ``` |
42 | 73 |
|
43 | | -# API |
| 74 | +#### Current |
44 | 75 |
|
45 | | -## Measurements |
| 76 | +```python |
| 77 | +bq.current(CurrentMeasureType.AVG) # Average current (default) |
| 78 | +bq.current(CurrentMeasureType.STBY) # Standby current |
| 79 | +bq.current(CurrentMeasureType.MAX) # Max current |
| 80 | +``` |
46 | 81 |
|
47 | | -### Voltage & Power |
| 82 | +#### Capacity |
48 | 83 |
|
49 | | -* `voltage_mv()` — battery voltage in mV |
50 | | -* `power()` — average power |
| 84 | +```python |
| 85 | +bq.capacity(CapacityMeasureType.REMAIN) # Remaining (default) |
| 86 | +bq.capacity(CapacityMeasureType.FULL) # Full charge |
| 87 | +bq.capacity(CapacityMeasureType.AVAIL) # Available |
| 88 | +bq.capacity(CapacityMeasureType.AVAIL_FULL) # Full available |
| 89 | +bq.capacity(CapacityMeasureType.REMAIN_F) # Remaining filtered |
| 90 | +bq.capacity(CapacityMeasureType.REMAIN_UF) # Remaining unfiltered |
| 91 | +bq.capacity(CapacityMeasureType.FULL_F) # Full filtered |
| 92 | +bq.capacity(CapacityMeasureType.FULL_UF) # Full unfiltered |
| 93 | +bq.capacity(CapacityMeasureType.DESIGN) # Design capacity |
| 94 | +``` |
51 | 95 |
|
52 | | -### Current |
| 96 | +#### State of Charge |
53 | 97 |
|
54 | | -* `current_average()` — average current |
55 | | -* `current(type)` — current (AVG, STBY, MAX) |
| 98 | +```python |
| 99 | +bq.soc(SocMeasureType.FILTERED) # Filtered SoC (default) |
| 100 | +bq.soc(SocMeasureType.UNFILTERED) # Unfiltered SoC |
| 101 | +``` |
56 | 102 |
|
57 | | -### Capacity |
| 103 | +#### State of Health |
58 | 104 |
|
59 | | -* `capacity_remaining()` — remaining capacity (mAh) |
60 | | -* `capacity_full()` — full charge capacity (mAh) |
61 | | -* `capacity(type)` — advanced capacity readings |
| 105 | +```python |
| 106 | +bq.soh(SohMeasureType.PERCENT) # Health percentage (default) |
| 107 | +bq.soh(SohMeasureType.SOH_STAT) # Health status bits |
| 108 | +``` |
62 | 109 |
|
63 | | -### Charge & Health |
| 110 | +#### Temperature |
64 | 111 |
|
65 | | -* `state_of_charge()` — battery level (%) |
66 | | -* `soc(type)` — filtered / unfiltered SoC |
67 | | -* `state_of_health()` — battery health (%) |
68 | | -* `soh(type)` — detailed SoH |
| 112 | +```python |
| 113 | +bq.temperature(TempMeasureType.BATTERY) # Battery temperature |
| 114 | +bq.temperature(TempMeasureType.INTERNAL_TEMP) # Internal IC temperature |
| 115 | +``` |
69 | 116 |
|
70 | | -### Temperature |
| 117 | +**Note:** Temperature is returned as a raw register value (units of 0.1 K). To convert to °C: `temp_c = raw / 10.0 - 273.15`. |
71 | 118 |
|
72 | | -* `temperature(type)` — battery or internal temperature |
| 119 | +### Configuration |
73 | 120 |
|
74 | | -## Configuration |
| 121 | +```python |
| 122 | +# Set battery design capacity |
| 123 | +bq.set_capacity(650) |
75 | 124 |
|
76 | | -* `set_capacity(mAh)` — set battery design capacity |
77 | | -* `enter_config(user_control)` — enter config mode |
78 | | -* `exit_config(resim=True)` — exit config mode |
| 125 | +# Configuration mode (required for some settings) |
| 126 | +bq.enter_config(user_control=True) |
| 127 | +# ... modify settings ... |
| 128 | +bq.exit_config(resim=True) |
| 129 | +``` |
79 | 130 |
|
80 | 131 | ### GPOUT / Alerts |
81 | 132 |
|
82 | | -* `set_gpout_function(type)` — SOC_INT or BAT_LOW |
83 | | -* `set_gpout_polarity(active_high)` |
84 | | -* `set_soc1_thresholds(set, clear)` |
85 | | -* `set_socf_thresholds(set, clear)` |
86 | | -* `pulse_gpout()` |
| 133 | +```python |
| 134 | +from bq27441.device import GpoutFunctionType |
| 135 | + |
| 136 | +# Configure GPOUT function |
| 137 | +bq.set_gpout_function(GpoutFunctionType.SOC_INT) # SoC change interrupt |
| 138 | +bq.set_gpout_function(GpoutFunctionType.BAT_LOW) # Battery low alert |
| 139 | + |
| 140 | +# GPOUT polarity |
| 141 | +bq.gpout_polarity() # Read current polarity |
| 142 | +bq.set_gpout_polarity(active_high=True) |
| 143 | + |
| 144 | +# GPOUT pin direction |
| 145 | +bq.configure_gpout_input() |
| 146 | +bq.configure_gpout_output() |
| 147 | + |
| 148 | +# SoC thresholds |
| 149 | +bq.set_soc1_thresholds(set_soc=15, clear_soc=20) |
| 150 | +bq.set_socf_thresholds(set_socf=5, clear_socf=10) |
| 151 | + |
| 152 | +# Read thresholds |
| 153 | +bq.soc1_set_threshold() |
| 154 | +bq.soc1_clear_threshold() |
| 155 | +bq.socf_set_threshold() |
| 156 | +bq.socf_clear_threshold() |
| 157 | + |
| 158 | +# SoC flags and delta |
| 159 | +bq.soc_flag() # SoC1 threshold crossed |
| 160 | +bq.socf_flag() # SoCF threshold crossed |
| 161 | +bq.soci_delta() # SoC interrupt delta |
| 162 | +bq.set_soci_delta(delta=1) |
| 163 | + |
| 164 | +# Pulse GPOUT |
| 165 | +bq.pulse_gpout() |
| 166 | +``` |
87 | 167 |
|
88 | | -## Power Management |
| 168 | +### Power Management |
89 | 169 |
|
90 | | -* `power_on()` — wake device |
91 | | -* `power_off()` — enter shutdown |
92 | | -* `enable_shutdown_mode()` |
93 | | -* `enter_shutdown_mode()` |
94 | | -* `disable_shutdown_mode()` |
| 170 | +* `bq.power_on()` — wake device from shutdown |
| 171 | +* `bq.power_off()` — enter shutdown mode |
| 172 | +* `bq.enable_shutdown_mode()` — enable shutdown via GPOUT |
| 173 | +* `bq.enter_shutdown_mode()` — enter shutdown |
| 174 | +* `bq.disable_shutdown_mode()` — disable shutdown mode |
95 | 175 |
|
96 | | -## Device Info & Control |
| 176 | +### Device Control |
97 | 177 |
|
98 | | -* `device_id()` — read device ID |
99 | | -* `is_valid_device()` — check device identity |
100 | | -* `flags()` — read status flags |
101 | | -* `reset()` — full reset |
102 | | -* `soft_reset()` — soft reset |
| 178 | +* `bq.device_id()` — read device type ID |
| 179 | +* `bq.is_valid_device()` — check if device is BQ27441 |
| 180 | +* `bq.flags()` — read status flags register |
| 181 | +* `bq.reset()` — full device reset |
| 182 | +* `bq.soft_reset()` — soft reset |
103 | 183 |
|
104 | | -# Examples |
| 184 | +### Seal / Unseal |
105 | 185 |
|
106 | | -| Example file | Description | |
107 | | -| --------------- | ------------------------------------------------ | |
108 | | -| `fuel_gauge.py` | Basic battery monitoring (voltage, SoC, current) | |
| 186 | +* `bq.sealed()` — check if device is sealed |
| 187 | +* `bq.seal()` — seal the device (restrict configuration access) |
| 188 | +* `bq.unseal()` — unseal for configuration |
109 | 189 |
|
110 | | -Run with: |
| 190 | +### Operation Config |
111 | 191 |
|
112 | | -```sh |
| 192 | +* `bq.op_config()` — read OpConfig register |
| 193 | +* `bq.write_op_config(value)` — write OpConfig register |
| 194 | + |
| 195 | +## Examples |
| 196 | + |
| 197 | +| File | Description | |
| 198 | +| ---------------- | ------------------------------------------------ | |
| 199 | +| `fuel_gauge.py` | Battery monitoring (voltage, SoC, current, health) | |
| 200 | + |
| 201 | +```bash |
113 | 202 | mpremote mount lib/bq27441 run lib/bq27441/examples/fuel_gauge.py |
114 | 203 | ``` |
115 | 204 |
|
116 | | ---- |
117 | | - |
118 | | -# Notes |
| 205 | +## Notes |
119 | 206 |
|
120 | | -* The **BQ27441 requires a connected LiPo battery** to respond on I²C |
121 | | -* Default design capacity is **650 mAh** (configurable) |
122 | | -* Some configuration operations require entering **config mode** |
123 | | -* The device may be **sealed**, requiring unsealing for advanced configuration |
| 207 | +* The BQ27441 requires a connected LiPo battery to respond on I²C. |
| 208 | +* Default design capacity is 650 mAh (configurable via `set_capacity()`). |
| 209 | +* Some configuration operations require entering config mode with `enter_config()`. |
| 210 | +* The device may be sealed; use `unseal()` before advanced configuration. |
| 211 | +* Temperature readings are raw register values in 0.1 K units (see conversion above). |
0 commit comments