Crates.io | aht20-embassy-stm32 |
lib.rs | aht20-embassy-stm32 |
version | 0.0.1 |
created_at | 2025-08-29 20:55:28.306946+00 |
updated_at | 2025-08-29 20:55:28.306946+00 |
description | An async driver for the AHT20 temperature and humidity sensor using embassy. |
homepage | |
repository | https://github.com/https://github.com/BiggiePete/aht20-embassy/aht20-embassy |
max_upload_size | |
id | 1816890 |
size | 33,247 |
Embassy compatible, No Std AHT20 library. Designed to be simple and easy to use, without moving the I2C object
A minimal, no_std, async AHT20 temperature and humidity sensor driver for Embassy on STM32 microcontrollers.
This library is designed to be simple, ergonomic, and efficient, without taking ownership of the I2C peripheral.
Make sure to bring your own version of embassy-stm32, with a chip selected!
Set up your I2C peripheral using Embassy STM32 HAL as usual.
Wrap it in a RefCell
to allow shared mutability.
use embassy_stm32::i2c::I2c;
use embassy_stm32::mode::Async;
use core::cell::RefCell;
// ... I2C peripheral setup code ...
let i2c = RefCell::new(I2c::new(/* ... */));
use aht20_embassy::AHT20;
let mut sensor = AHT20::new(&i2c);
sensor.init().await.unwrap();
let (temperature, humidity) = sensor.read().await.unwrap();
defmt::info!("Temperature: {}°C, Humidity: {}%", temperature, humidity);
All methods return a Result<T, Aht20Error>
.
Errors include I2C errors, not initialized, sensor busy, or not calibrated.
match sensor.read().await {
Ok((temp, hum)) => { /* use values */ }
Err(e) => defmt::error!("Sensor error: {:?}", e),
}
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
// ... I2C setup ...
let i2c = RefCell::new(I2c::new(/* ... */));
let mut sensor = AHT20::new(&i2c);
sensor.init().await.unwrap();
loop {
match sensor.read().await {
Ok((temp, hum)) => {
defmt::info!("Temp: {:.2}°C, Humidity: {:.2}%", temp, hum);
}
Err(e) => {
defmt::error!("AHT20 error: {:?}", e);
}
}
embassy_time::Timer::after(embassy_time::Duration::from_secs(2)).await;
}
}