|
1 | | -# MicroPython BQ27441 Library |
| 1 | +# BQ27441 MicroPython Driver |
2 | 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). |
| 3 | +MicroPython driver for the **Texas Instruments BQ27441-G1A** LiPo fuel gauge. |
4 | 4 |
|
5 | | -The examples could be easily tested with [mpremote](https://docs.micropython.org/en/latest/reference/mpremote.html) with the following command : |
| 5 | +This library is a port of the [SparkFun BQ27441 Arduino Library](https://github.com/sparkfun/SparkFun_BQ27441_Arduino_Library). |
6 | 6 |
|
7 | | -```sh |
8 | | -mpremote mount . run examples/fuel_gauge.py |
| 7 | +## Supported Device |
| 8 | + |
| 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 | |
| 16 | + |
| 17 | +**Note:** The BQ27441 only responds on I²C when a battery is connected. |
| 18 | + |
| 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 |
| 34 | + |
| 35 | +```python |
| 36 | +from machine import I2C |
| 37 | +from bq27441 import BQ27441 |
| 38 | + |
| 39 | +i2c = I2C(1) |
| 40 | +bq = BQ27441(i2c) |
| 41 | + |
| 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 |
| 48 | + |
| 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 |
| 72 | +``` |
| 73 | + |
| 74 | +#### Current |
| 75 | + |
| 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 | +``` |
| 81 | + |
| 82 | +#### Capacity |
| 83 | + |
| 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 | +``` |
| 95 | + |
| 96 | +#### State of Charge |
| 97 | + |
| 98 | +```python |
| 99 | +bq.soc(SocMeasureType.FILTERED) # Filtered SoC (default) |
| 100 | +bq.soc(SocMeasureType.UNFILTERED) # Unfiltered SoC |
| 101 | +``` |
| 102 | + |
| 103 | +#### State of Health |
| 104 | + |
| 105 | +```python |
| 106 | +bq.soh(SohMeasureType.PERCENT) # Health percentage (default) |
| 107 | +bq.soh(SohMeasureType.SOH_STAT) # Health status bits |
| 108 | +``` |
| 109 | + |
| 110 | +#### Temperature |
| 111 | + |
| 112 | +```python |
| 113 | +bq.temperature(TempMeasureType.BATTERY) # Battery temperature |
| 114 | +bq.temperature(TempMeasureType.INTERNAL_TEMP) # Internal IC temperature |
| 115 | +``` |
| 116 | + |
| 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`. |
| 118 | + |
| 119 | +### Configuration |
| 120 | + |
| 121 | +```python |
| 122 | +# Set battery design capacity |
| 123 | +bq.set_capacity(650) |
| 124 | + |
| 125 | +# Configuration mode (required for some settings) |
| 126 | +bq.enter_config(user_control=True) |
| 127 | +# ... modify settings ... |
| 128 | +bq.exit_config(resim=True) |
9 | 129 | ``` |
| 130 | + |
| 131 | +### GPOUT / Alerts |
| 132 | + |
| 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 | +``` |
| 167 | + |
| 168 | +### Power Management |
| 169 | + |
| 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 |
| 175 | + |
| 176 | +### Device Control |
| 177 | + |
| 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 |
| 183 | + |
| 184 | +### Seal / Unseal |
| 185 | + |
| 186 | +* `bq.sealed()` — check if device is sealed |
| 187 | +* `bq.seal()` — seal the device (restrict configuration access) |
| 188 | +* `bq.unseal()` — unseal for configuration |
| 189 | + |
| 190 | +### Operation Config |
| 191 | + |
| 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 |
| 202 | +mpremote mount lib/bq27441 run lib/bq27441/examples/fuel_gauge.py |
| 203 | +``` |
| 204 | + |
| 205 | +## Notes |
| 206 | + |
| 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