TMP108
A #[no_std] platform-agnostic driver for the
TMP108 temperature sensor using
the embedded-hal traits.
I²C addresses
The TMP108 can take one of 4 I²C addresses depending on the state of the A0 pin:
| A0 | Addr |
|---|---|
| GND | 0x48 |
| V+ | 0x49 |
| SDA | 0x4a |
| SCL | 0x4b |
The driver has dedicated constructors for each — new_with_a0_gnd,
new_with_a0_vplus, new_with_a0_sda, new_with_a0_scl — so an invalid
address cannot be requested.
Usage
Blocking one-shot read
let hal = new;
let i2c = hal.i2c;
let mut tmp = new_with_a0_gnd;
let temperature = tmp.temperature.map_err?;
println!;
Async continuous conversions
let hal = new;
let i2c = hal.i2c;
let mut delay = hal.delay;
let mut tmp = new_with_a0_gnd;
tmp.continuous
.await
.map_err?;
Async interrupt-mode threshold alert
let mut tmp = new_with_a0_gnd;
tmp.sensor_mut
.configure
.await
.map_err?;
tmp.set_temperature_threshold_low
.await
.map_err?;
tmp.set_temperature_threshold_high
.await
.map_err?;
println!;
let temperature = tmp
.wait_for_temperature_threshold
.await
.map_err?;
println!;
See examples/ for complete, runnable versions of each snippet (and more).
Cargo features
| Feature | Effect | Implies |
|---|---|---|
| (none) | Blocking Tmp108 over embedded-hal. |
— |
async |
Async AsyncTmp108 over embedded-hal-async. AsyncTmp108::continuous is unlocked. |
— |
embedded-sensors-hal |
Blocking TemperatureSensor impl on Tmp108. |
— |
embedded-sensors-hal-async |
Async TemperatureSensor, TemperatureThresholdSet, TemperatureHysteresis impls on AsyncTmp108, plus the AlertTmp108 wrapper with TemperatureThresholdWait. |
async |
Since 0.6.0 both Tmp108 (blocking) and AsyncTmp108 (async) are
available simultaneously when both relevant features are enabled.
Gotchas
- Constructors are infallible. They take no delay; the delay only
appears on
wait_for_temperature, where it is genuinely needed to wait out a conversion period. - Comparator vs interrupt mode latching. In comparator mode the ALERT pin
stays asserted until temperature returns inside
(T_low + HYS, T_high − HYS). In interrupt mode the pin clears as soon as the configuration register is read (the driver does this for you insidewait_for_temperature_threshold). Seeexamples/alert_comparator.rsfor a demonstration. - ALERT polarity is set on-chip. Wire your pull resistor for the polarity you configured. Examples assume active-low + external pull-up.
AsyncTmp108::continuousis async-only. For blocking continuous-mode use, callTmp108::configure(...)to setMode::Continuousmanually, loop onTmp108::wait_for_temperature(&mut delay), then callTmp108::shutdown().AsyncTmp108::continuousfuture is not cancel-safe. Dropping it before completion (e.g. viaembassy_futures::select!ortokio::time::timeout) leaves the chip inMode::Continuous. See the method's doc.- Temperature scale. Raw register values are 12-bit signed in the upper
bits of a 16-bit register at 0.0625 °C/LSB. The driver models this as
Celsius, a newtype over sixteenths of a degree whose 4096 inhabitants are exactly the temperatures the part can report or accept as a limit.Celsius::try_from_degreesis the single fallible parse (it rejectsNaN, infinities and out-of-range values, and rounds half away from zero);Celsius::to_degreesrenders back tof32, andDisplayhonours precision so{t:.2}works. Theembedded-sensors-haltrait impls keep theirDegreesCelsius(f32) signatures and translate at the boundary.
MSRV
Rust 1.94 and up.
License
Licensed under the terms of the MIT license.
Contribution
Unless you explicitly state otherwise, any contribution submitted for inclusion in the work by you shall be licensed under the terms of the MIT license.
See CONTRIBUTING.md for the full contribution workflow, including the Conventional Commits v1.0.0 commit-message format this repository uses.