Envorimental Monitoring
Loading...
Searching...
No Matches
aht20.h File Reference

Driver interface for the AHT20 humidity and temperature sensor. More...

#include "stdint.h"
#include "stddef.h"

Go to the source code of this file.

Data Structures

struct  AHT20_HandleTypeDef
 AHT20 driver handle. More...

Macros

#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.

Typedefs

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.

Functions

int8_t AHT20_Init (AHT20_HandleTypeDef *dev)
 Initialises the AHT20 sensor.
int8_t AHT20_Read (AHT20_HandleTypeDef *dev)
 Triggers a measurement and reads humidity and temperature from the AHT20.

Detailed Description

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;
haht20.delay_ms = my_delay;
float rh = haht20.humidity; // % RH
float degC = haht20.temperature; // °C
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

Typedef Documentation

◆ AHT20_Delay_Func

typedef void(* AHT20_Delay_Func) (uint32_t ms)

Function pointer type for platform millisecond delay.

Parameters
msDelay in milliseconds.

◆ 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_addr8-bit device address (HAL left-shifted format).
reg_addrRegister address; 0 for raw master-receive.
dataBuffer to store received bytes.
lenNumber 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_addr8-bit device address (HAL left-shifted format).
reg_addrRegister address; 0 for raw master-transmit.
dataBuffer containing bytes to send.
lenNumber of bytes to write.
Returns
0 on success, non-zero on error.

Function Documentation

◆ AHT20_Init()

int8_t AHT20_Init ( AHT20_HandleTypeDef * dev)

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
[in,out]devPointer to an AHT20_HandleTypeDef with function pointers set.
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()

int8_t AHT20_Read ( AHT20_HandleTypeDef * dev)

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
[in,out]devPointer to an initialised AHT20_HandleTypeDef.
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.