|
1 | | -# MicroPython Driver for the HTS221 Humidity Sensor |
| 1 | +# HTS221 MicroPython Driver |
2 | 2 |
|
3 | | -This library is a port of the [MicroPython Driver for the HTS221 Humidity Sensor](https://github.com/jposada202020/MicroPython_HTS221/tree/master). |
| 3 | +MicroPython driver for the **STMicroelectronics HTS221** humidity and temperature sensor. |
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 [MicroPython_HTS221](https://github.com/jposada202020/MicroPython_HTS221). |
6 | 6 |
|
7 | | -```sh |
8 | | - mpremote mount . run examples/humidity.py |
| 7 | +## Supported Sensor |
| 8 | + |
| 9 | +| Parameter | Value | |
| 10 | +| -------------------- | ----------------- | |
| 11 | +| Interface | I²C | |
| 12 | +| Default I²C address | `0x5F` | |
| 13 | +| Device ID | `0xBC` | |
| 14 | +| Humidity range | 0–100 %RH | |
| 15 | +| Humidity accuracy | ±3.5 %RH | |
| 16 | +| Temperature range | −40 to +120 °C | |
| 17 | +| Temperature accuracy | ±0.5 °C | |
| 18 | + |
| 19 | +## Features |
| 20 | + |
| 21 | +* I²C communication |
| 22 | +* Device identification (`WHO_AM_I`) |
| 23 | +* Humidity and temperature measurement |
| 24 | +* One-shot and continuous measurement modes |
| 25 | +* Configurable output data rate (ODR) |
| 26 | +* Configurable averaging |
| 27 | +* Data-ready status flags |
| 28 | +* Power management (on/off, reboot) |
| 29 | +* Factory calibration (automatic) |
| 30 | +* User temperature calibration (offset and two-point) |
| 31 | + |
| 32 | +## Basic Usage |
| 33 | + |
| 34 | +```python |
| 35 | +from machine import I2C |
| 36 | +from hts221 import HTS221 |
| 37 | + |
| 38 | +i2c = I2C(1) |
| 39 | +sensor = HTS221(i2c) |
| 40 | + |
| 41 | +humidity = sensor.humidity() |
| 42 | +temperature = sensor.temperature() |
| 43 | + |
| 44 | +print("Humidity:", humidity, "%") |
| 45 | +print("Temperature:", temperature, "°C") |
| 46 | + |
| 47 | +# Read both at once |
| 48 | +h, t = sensor.read() |
| 49 | +``` |
| 50 | + |
| 51 | +## API Reference |
| 52 | + |
| 53 | +### Initialization |
| 54 | + |
| 55 | +```python |
| 56 | +sensor = HTS221(i2c, address=0x5F) |
| 57 | +``` |
| 58 | + |
| 59 | +### Device Information |
| 60 | + |
| 61 | +```python |
| 62 | +sensor.device_id() |
9 | 63 | ``` |
| 64 | + |
| 65 | +Returns the sensor device ID (`WHO_AM_I` register, expected `0xBC`). |
| 66 | + |
| 67 | +### Measurements |
| 68 | + |
| 69 | +* `sensor.temperature()` — temperature in °C |
| 70 | +* `sensor.humidity()` — relative humidity in % |
| 71 | +* `sensor.read()` — returns `(humidity, temperature)` |
| 72 | +* `sensor.read_one_shot()` — triggers a one-shot conversion and returns `(humidity, temperature)` after a fixed 15 ms delay |
| 73 | + |
| 74 | +### Data Readiness |
| 75 | + |
| 76 | +* `sensor.data_ready()` — `True` when both humidity and temperature are available |
| 77 | +* `sensor.temperature_ready()` — `True` when temperature is available |
| 78 | +* `sensor.humidity_ready()` — `True` when humidity is available |
| 79 | + |
| 80 | +### One-Shot Mode |
| 81 | + |
| 82 | +```python |
| 83 | +sensor.trigger_one_shot() |
| 84 | +``` |
| 85 | + |
| 86 | +Triggers a single conversion with a 15 ms delay. Used internally by `read_one_shot()`. |
| 87 | + |
| 88 | +### Configuration |
| 89 | + |
| 90 | +* `sensor.get_odr()` — returns current output data rate |
| 91 | +* `sensor.set_odr(odr)` — set ODR (0 = one-shot, 1 = 1 Hz, 2 = 7 Hz, 3 = 12.5 Hz) |
| 92 | +* `sensor.set_continuous(odr)` — enable continuous mode. Raises `ValueError` if `odr=0`. |
| 93 | + |
| 94 | +### Averaging |
| 95 | + |
| 96 | +* `sensor.get_av()` — returns current averaging configuration register |
| 97 | +* `sensor.set_av(av)` — set humidity and temperature averaging (raw register value) |
| 98 | + |
| 99 | +The AV_CONF register controls internal averaging for both channels. Higher values improve noise but increase conversion time. Refer to the HTS221 datasheet for the register encoding. |
| 100 | + |
| 101 | +### Power Management |
| 102 | + |
| 103 | +* `sensor.power_on()` — enable sensor |
| 104 | +* `sensor.power_off()` — disable sensor |
| 105 | +* `sensor.reboot()` — reload factory calibration data from non-volatile memory |
| 106 | + |
| 107 | +### Temperature Calibration |
| 108 | + |
| 109 | +```python |
| 110 | +# Simple offset correction |
| 111 | +sensor.set_temp_offset(-1.2) |
| 112 | + |
| 113 | +# Two-point calibration (gain + offset) |
| 114 | +sensor.calibrate_temperature( |
| 115 | + ref_low=0.0, |
| 116 | + measured_low=0.8, |
| 117 | + ref_high=50.0, |
| 118 | + measured_high=48.7, |
| 119 | +) |
| 120 | +``` |
| 121 | + |
| 122 | +No calibration is required for basic usage — the driver applies factory calibration automatically. |
| 123 | + |
| 124 | +## Examples |
| 125 | + |
| 126 | +| File | Description | |
| 127 | +| ------------- | -------------------------------------- | |
| 128 | +| `humidity.py` | Basic humidity and temperature reading | |
| 129 | + |
| 130 | +```bash |
| 131 | +mpremote mount lib/hts221 run lib/hts221/examples/humidity.py |
| 132 | +``` |
| 133 | + |
| 134 | +## Notes |
| 135 | + |
| 136 | +* The driver uses factory calibration data stored in the sensor, read automatically at initialization. |
| 137 | +* `temperature()`, `humidity()`, and `read()` poll the sensor's status bits via `_ensure_data()` to wait for fresh data before returning. |
| 138 | +* `read_one_shot()` triggers a conversion and waits a fixed 15 ms delay before reading registers (no status polling). |
| 139 | +* In power-down or one-shot mode, calling a measurement method automatically triggers a new conversion. |
0 commit comments