|
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 | +The HTS221 provides: |
6 | 6 |
|
7 | | -```sh |
8 | | - mpremote mount . run examples/humidity.py |
| 7 | +* **Relative humidity (%)** |
| 8 | +* **Temperature (°C)** |
| 9 | + |
| 10 | +This driver offers a simple and complete API for configuration, measurement, calibration, and power management using **I²C**. |
| 11 | + |
| 12 | +## Features |
| 13 | + |
| 14 | +* I²C communication |
| 15 | +* Device identification (`WHO_AM_I`) |
| 16 | +* Relative humidity measurement |
| 17 | +* Temperature measurement |
| 18 | +* One-shot measurement mode |
| 19 | +* Continuous measurement mode |
| 20 | +* Configurable output data rate (ODR) |
| 21 | +* Configurable averaging |
| 22 | +* Data-ready status flags |
| 23 | +* Power management (power on/off, reboot) |
| 24 | +* Built-in factory calibration usage |
| 25 | +* User temperature calibration: |
| 26 | + * offset correction |
| 27 | + * two-point calibration |
| 28 | + |
| 29 | +## I²C Address |
| 30 | + |
| 31 | +* Default address: **0x5F** |
| 32 | + |
| 33 | +## Basic Usage |
| 34 | + |
| 35 | +```python |
| 36 | +from machine import I2C |
| 37 | +from hts221 import HTS221 |
| 38 | + |
| 39 | +# Initialize I2C |
| 40 | +i2c = I2C(1) |
| 41 | + |
| 42 | +# Initialize sensor |
| 43 | +sensor = HTS221(i2c) |
| 44 | + |
| 45 | +# Read values |
| 46 | +humidity = sensor.humidity() |
| 47 | +temperature = sensor.temperature() |
| 48 | + |
| 49 | +print("Humidity:", humidity, "%") |
| 50 | +print("Temperature:", temperature, "°C") |
| 51 | + |
| 52 | +# Read both at once |
| 53 | +h, t = sensor.read() |
| 54 | +``` |
| 55 | + |
| 56 | +## API |
| 57 | + |
| 58 | +### Device Information |
| 59 | + |
| 60 | +```python |
| 61 | +device_id() |
| 62 | +``` |
| 63 | + |
| 64 | +Returns the sensor device ID (`WHO_AM_I` register). |
| 65 | + |
| 66 | +### Data Readiness |
| 67 | + |
| 68 | +```python |
| 69 | +data_ready() |
| 70 | +temperature_ready() |
| 71 | +humidity_ready() |
9 | 72 | ``` |
| 73 | + |
| 74 | +* `data_ready()` → both humidity and temperature available |
| 75 | +* `temperature_ready()` → temperature available |
| 76 | +* `humidity_ready()` → humidity available |
| 77 | + |
| 78 | +### Measurements |
| 79 | + |
| 80 | +```python |
| 81 | +temperature() |
| 82 | +humidity() |
| 83 | +read() |
| 84 | +read_one_shot() |
| 85 | +``` |
| 86 | + |
| 87 | +* `temperature()` → temperature in °C |
| 88 | +* `humidity()` → relative humidity in % |
| 89 | +* `read()` → `(humidity, temperature)` |
| 90 | +* `read_one_shot()` → performs a one-shot measurement and returns `(humidity, temperature)` |
| 91 | + |
| 92 | +### One-Shot Mode |
| 93 | + |
| 94 | +```python |
| 95 | +trigger_one_shot() |
| 96 | +``` |
| 97 | + |
| 98 | +Triggers a single measurement (used internally by `read_one_shot()`). |
| 99 | + |
| 100 | +### Configuration |
| 101 | + |
| 102 | +```python |
| 103 | +get_odr() |
| 104 | +set_odr(odr) |
| 105 | +set_continuous(odr) |
| 106 | +``` |
| 107 | + |
| 108 | +* `get_odr()` → returns current output data rate |
| 109 | +* `set_odr(odr)` → set ODR (0 = one-shot mode) |
| 110 | +* `set_continuous(odr)` → enable continuous mode (ODR ≠ 0) |
| 111 | + |
| 112 | +### Averaging Configuration |
| 113 | + |
| 114 | +```python |
| 115 | +get_av() |
| 116 | +set_av(av) |
| 117 | +``` |
| 118 | + |
| 119 | +Controls internal averaging for humidity and temperature. |
| 120 | + |
| 121 | +### Power Management |
| 122 | + |
| 123 | +```python |
| 124 | +power_on() |
| 125 | +power_off() |
| 126 | +reboot() |
| 127 | +``` |
| 128 | + |
| 129 | +* `power_on()` → enable sensor |
| 130 | +* `power_off()` → disable sensor |
| 131 | +* `reboot()` → reload calibration data and reboot memory |
| 132 | + |
| 133 | +### Temperature Calibration |
| 134 | + |
| 135 | +```python |
| 136 | +set_temp_offset(offset_c) |
| 137 | +calibrate_temperature(ref_low, measured_low, ref_high, measured_high) |
| 138 | +``` |
| 139 | + |
| 140 | +* `set_temp_offset(offset_c)` |
| 141 | + Apply a simple temperature offset correction. |
| 142 | + |
| 143 | +* `calibrate_temperature(...)` |
| 144 | + Perform a **two-point calibration**: |
| 145 | + |
| 146 | + * improves accuracy |
| 147 | + * adjusts both gain and offset |
| 148 | + |
| 149 | +## Examples |
| 150 | + |
| 151 | +| File | Description | |
| 152 | +| ------------- | -------------------------------------- | |
| 153 | +| `humidity.py` | Basic humidity and temperature reading | |
| 154 | + |
| 155 | +Run with: |
| 156 | + |
| 157 | +```bash |
| 158 | +mpremote mount lib/htss221 run examples/htss221/examples/humidity.py |
| 159 | +``` |
| 160 | + |
| 161 | +--- |
| 162 | + |
| 163 | +## Notes |
| 164 | + |
| 165 | +* The driver uses **factory calibration data** stored in the sensor. |
| 166 | +* Temperature can be further corrected using: |
| 167 | + |
| 168 | + * `set_temp_offset()` (simple correction) |
| 169 | + * `calibrate_temperature()` (recommended for precision) |
| 170 | +* In one-shot mode, the driver automatically waits for data readiness. |
| 171 | +* In power-down or one-shot mode, measurements trigger automatically when needed. |
| 172 | + |
| 173 | +--- |
| 174 | + |
| 175 | +## Source |
| 176 | + |
| 177 | +This library is a port of: |
| 178 | +[https://github.com/jposada202020/MicroPython_HTS221/tree/master](https://github.com/jposada202020/MicroPython_HTS221/tree/master) |
0 commit comments