Driver interface for the AHT20 humidity and temperature sensor.
More...
#include "stdint.h"
#include "stddef.h"
Go to the source code of this file.
|
|
#define | AHT20_ADDRESS (0x38 << 1) |
| | AHT20 I2C base address (7-bit = 0x38, shifted left for HAL).
|
|
#define | AHT20_CMD_STATUS 0x71 |
| | Command byte: read status register.
|
|
#define | AHT20_CMD_INIT 0xBE |
| | Command byte: initialise / calibrate the sensor.
|
|
#define | AHT20_CMD_TRIGGER 0xAC |
| | Command byte: trigger a humidity + temperature measurement.
|
|
| typedef int8_t(* | AHT20_I2C_Read_Func) (uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t len) |
| | Function pointer type for platform I2C read operations.
|
| typedef int8_t(* | AHT20_I2C_Write_Func) (uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t len) |
| | Function pointer type for platform I2C write operations.
|
| typedef void(* | AHT20_Delay_Func) (uint32_t ms) |
| | Function pointer type for platform millisecond delay.
|
Driver interface for the AHT20 humidity and temperature sensor.
The AHT20 communicates over I2C at a fixed address of 0x38. The driver is platform-independent: the caller injects the I2C read/write and delay function pointers at initialisation time, so the same code runs on any MCU that provides those three primitives.
Typical usage:
haht20.i2c_read = my_i2c_read;
haht20.i2c_write = my_i2c_write;
float degC =
haht20.temperature;
int8_t AHT20_Init(AHT20_HandleTypeDef *dev)
Initialises the AHT20 sensor.
Definition aht20.c:11
int8_t AHT20_Read(AHT20_HandleTypeDef *dev)
Triggers a measurement and reads humidity and temperature from the AHT20.
Definition aht20.c:43
AHT20_HandleTypeDef haht20
Definition main.c:147
AHT20 driver handle.
Definition aht20.h:82
- Author
- Şule Nur Demirdaş
- Date
- April 2026
◆ AHT20_Delay_Func
| typedef void(* AHT20_Delay_Func) (uint32_t ms) |
Function pointer type for platform millisecond delay.
- Parameters
-
◆ AHT20_I2C_Read_Func
| typedef int8_t(* AHT20_I2C_Read_Func) (uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t len) |
Function pointer type for platform I2C read operations.
- Parameters
-
| dev_addr | 8-bit device address (HAL left-shifted format). |
| reg_addr | Register address; 0 for raw master-receive. |
| data | Buffer to store received bytes. |
| len | Number of bytes to read. |
- Returns
- 0 on success, non-zero on error.
◆ AHT20_I2C_Write_Func
| typedef int8_t(* AHT20_I2C_Write_Func) (uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t len) |
Function pointer type for platform I2C write operations.
- Parameters
-
| dev_addr | 8-bit device address (HAL left-shifted format). |
| reg_addr | Register address; 0 for raw master-transmit. |
| data | Buffer containing bytes to send. |
| len | Number of bytes to write. |
- Returns
- 0 on success, non-zero on error.
◆ AHT20_Init()
Initialises the AHT20 sensor.
Sends a status request and, if the calibration bit is not set, issues the initialisation command. Requires a 40 ms power-on delay before calling.
- Parameters
-
- Returns
- 0 on success.
-
-1 if any function pointer is NULL.
-
-2 if the status write fails.
-
-3 if the status read fails.
-
-4 if the initialisation command write fails.
◆ AHT20_Read()
Triggers a measurement and reads humidity and temperature from the AHT20.
Sends the trigger command, waits 80 ms for conversion, reads 7 bytes, and converts the raw 20-bit values to physical units stored in dev->humidity and dev->temperature. The CRC byte (rx_buffer[6]) is currently not verified.
- Parameters
-
- Returns
- 0 on success.
-
-1 if any function pointer is NULL.
-
-2 if the trigger command write fails.
-
-3 if the data read fails.
-
-4 if the busy flag in the status byte is set.