22
33MicroPython driver for the ** STMicroelectronics HTS221** humidity and temperature sensor.
44
5- The HTS221 provides:
5+ This library is a port of [ MicroPython_HTS221 ] ( https://github.com/jposada202020/MicroPython_HTS221 ) .
66
7- * ** Relative humidity (%)**
8- * ** Temperature (°C)**
7+ ## Supported Sensor
98
10- This driver offers a simple and complete API for configuration, measurement, calibration, and power management using ** I²C** .
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 |
1118
1219## Features
1320
1421* I²C communication
1522* Device identification (` WHO_AM_I ` )
16- * Relative humidity measurement
17- * Temperature measurement
18- * One-shot measurement mode
19- * Continuous measurement mode
23+ * Humidity and temperature measurement
24+ * One-shot and continuous measurement modes
2025* Configurable output data rate (ODR)
2126* Configurable averaging
2227* 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**
28+ * Power management (on/off, reboot)
29+ * Factory calibration (automatic)
30+ * User temperature calibration (offset and two-point)
3231
3332## Basic Usage
3433
3534``` python
3635from machine import I2C
3736from hts221 import HTS221
3837
39- # Initialize I2C
4038i2c = I2C(1 )
41-
42- # Initialize sensor
4339sensor = HTS221(i2c)
4440
45- # Read values
4641humidity = sensor.humidity()
4742temperature = sensor.temperature()
4843
@@ -53,126 +48,92 @@ print("Temperature:", temperature, "°C")
5348h, t = sensor.read()
5449```
5550
56- ## API
51+ ## API Reference
5752
58- ### Device Information
53+ ### Initialization
5954
6055``` python
61- device_id( )
56+ sensor = HTS221(i2c, address = 0x 5F )
6257```
6358
64- Returns the sensor device ID (` WHO_AM_I ` register).
65-
66- ### Data Readiness
59+ ### Device Information
6760
6861``` python
69- data_ready()
70- temperature_ready()
71- humidity_ready()
62+ sensor.device_id()
7263```
7364
74- * ` data_ready() ` → both humidity and temperature available
75- * ` temperature_ready() ` → temperature available
76- * ` humidity_ready() ` → humidity available
65+ Returns the sensor device ID (` WHO_AM_I ` register, expected ` 0xBC ` ).
7766
7867### Measurements
7968
80- ``` python
81- temperature()
82- humidity()
83- read()
84- read_one_shot()
85- ```
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
8675
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) `
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
9179
9280### One-Shot Mode
9381
9482``` python
95- trigger_one_shot()
83+ sensor. trigger_one_shot()
9684```
9785
98- Triggers a single measurement (used internally by ` read_one_shot() ` ) .
86+ Triggers a single conversion with a 15 ms delay. Used internally by ` read_one_shot() ` .
9987
10088### Configuration
10189
102- ``` python
103- get_odr()
104- set_odr(odr)
105- set_continuous(odr)
106- ```
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 ` .
10793
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)
94+ ### Averaging
11195
112- ### Averaging Configuration
96+ * ` sensor.get_av() ` — returns current averaging configuration register
97+ * ` sensor.set_av(av) ` — set humidity and temperature averaging (raw register value)
11398
114- ``` python
115- get_av()
116- set_av(av)
117- ```
118-
119- Controls internal averaging for humidity and temperature.
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.
120100
121101### Power Management
122102
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
103+ * ` sensor.power_on() ` — enable sensor
104+ * ` sensor.power_off() ` — disable sensor
105+ * ` sensor.reboot() ` — reload factory calibration data from non-volatile memory
132106
133107### Temperature Calibration
134108
135109``` python
136- set_temp_offset(offset_c)
137- calibrate_temperature(ref_low, measured_low, ref_high, measured_high)
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+ )
138120```
139121
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
122+ No calibration is required for basic usage — the driver applies factory calibration automatically.
148123
149124## Examples
150125
151126| File | Description |
152127| ------------- | -------------------------------------- |
153128| ` humidity.py ` | Basic humidity and temperature reading |
154129
155- Run with:
156-
157130``` bash
158- mpremote mount lib/htss221 run examples/htss221 /examples/humidity.py
131+ mpremote mount lib/hts221 run lib/hts221 /examples/humidity.py
159132```
160133
161- ---
162-
163134## Notes
164135
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 )
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