-
Notifications
You must be signed in to change notification settings - Fork 0
AHT20 ‐ BMP280 usage
This Wiki page documents the library details for the AHT20 and BMP280 sensors, which are integrated into the sensor shield for the ATTiny84. The libraries are designed to provide easy access to environmental data, specifically humidity and temperature from the AHT20 sensor and pressure and temperature from the BMP280 sensor.
The AHT20 sensor library is structured for the ATTiny84 microcontroller and uses TinyWireM for I2C communication.
The AHT20 class provides functions to initialize, read, and interpret data from the AHT20 sensor. This class handles low-level communication with the sensor, making it simple to retrieve temperature and humidity measurements.
-
Constructor:
AHT20();Initializes the AHT20 instance.
-
begin():
bool begin();Initializes the I2C TWI and calibrates the AHT20 sensor. Returns 1 is successful.
-
readData():
bool readData();Reads raw humidity and temperature values. Returns 1 if 6 bytes are correctly read
-
getTemperature():
float getTemperature();Returns the temperature in Celsius as a float.
-
getHumidity():
float getHumidity();Returns relative humidity as a percentage (float).
-
reset():
void reset();Resets the AHT20 sensor and releases the I2C USI, which can be useful for reinitializing the I2C communication.
These are internal functions not intended for direct user interaction but crucial for the library's functionality:
-
initialize():
bool initialize();Writes initialization sequence {0xBE, 0x08, 0x00} to calibrate the sensor.
-
triggerMeasurement():
bool triggerMeasurement();Starts a humidity and temperature measurement. After 80 ms data will be available inside user registers.
-
readStatus():
uint8_t readStatus();Reads and returns the status register of the AHT20 sensor (0x71).
- bit7:BusyIndication (0 = free, 1 = busy in measurement mode)
- bit6:Remained
- bit5:Remained
- bit4:Remained
- bit3:CalibrationEnabled (1 = enabled,0 = uncalibrated)
- bit2:Remained
- bit1:Remained
- bit0:Remained
- Address: The I2C address for the AHT20 sensor is 0x38.
Basic setup, initialization, and data retrieval for the AHT20 sensor are demonstrated in the aht20_example folder.
IMPORTANT In this example, the AHT20 is used with the RFM95 module (SPI-based). Since the ATtiny84 shares I2C and SPI lines, all connected devices must actively drive the lines to avoid interference. We found that if the AHT20 enters a reset state, it disrupts SPI communication with the RFM95.
The BMP280 sensor library is structured for the ATTiny84 microcontroller and uses TinyWireM for I2C communication.
The BMP280 class provides functions to initialize, read, and interpret data from the BMP280 sensor. This class handles low-level communication with the sensor, making it simple to retrieve temperature and pressure measurements.
This page provides documentation for the BMP280 library. The library facilitates interaction with the BMP280 temperature and pressure sensor, optimized for ATTiny I2C communication. Please notice that the BMP280 has been tested by using the forced mode operation (every measurement has to be manually triggered), with the suggested settings for environmental monitoring applications (IIR filter OFF, no oversampling ratios, forced mode operation).
-
TempUnit
Defines units for temperature readings:TempUnit_CelsiusTempUnit_Fahrenheit
-
PresUnit
Defines units for pressure readings:PresUnit_PaPresUnit_hPaPresUnit_inHgPresUnit_atmPresUnit_barPresUnit_torrPresUnit_psi
-
OSR
Oversampling rate for better resolution:-
OSR_X1,OSR_X2,OSR_X4,OSR_X8,OSR_X16
-
-
Mode
Defines the sensor operating modes:-
Mode_Sleep: Sensor stops measurements but can be read -
Mode_Forced: Takes a single measurement then returns to sleep -
Mode_Normal: Alternates between measurement and standby
-
-
StandbyTime
Standby duration options in normal mode:- Options from
StandbyTime_500ustoStandbyTime_20ms
- Options from
-
Filter
IIR filter settings to reduce power consumption:-
Filter_Off,Filter_2,Filter_4,Filter_8,Filter_16
-
-
Constructor:
BMP280(const Settings& my_settings = Settings(), uint8_t address = 0x76);Initializes the BMP280 sensor with the given settings. The default I2C address is 0x76, however the aht20-bmp280 shield connects SDO to GND, forcing the address to be 0x77. The Settings() structure is initialized with default settings as described above (Temperature in °C, pressure in Pa, no IIR filter, no OSR, forced operating mode. For additional details visit the associated .h file.
-
begin():
bool begin():Initializes the I2C interface and writes default settings to the sensor, returns 1 on successful setup.
-
readTemperature(TempUnit unit):
float readTemperature(TempUnit unit);Reads the temperature in the specified unit (Celsius or Fahrenheit).
-
float readPressure(PresUnit unit):
float readPressure(PresUnit unit);Reads the pressure in the specified unit (e.g., Pa, hPa, psi, ...).
These are internal functions not intended for direct user interaction but crucial for the library's functionality:
-
readChipID():
bool readChipID();Checks if the chip ID matches the BMP280’s ID (0x58).
-
CalculateRegisters(uint8_t& ctrlMeas, uint8_t& config):
void CalculateRegisters(uint8_t& ctrlMeas, uint8_t& config);Calculates the byte to be written to the control register based on the user settings specified in the Settings structure. ctrlMeas and config represent the two bytes to be written in the corresponding registers.
-
WriteSettings():
void WriteSettings();Writes the current settings specified in the m_serttings Structure to the BMP280.
-
readCalibrationData():
bool readCalibrationData();Reads the calibration data stored in the registers 0x88...0xA1 needed for temperature and pressure calculations.
-
CalculateTemperature(int32_t raw, int32_t& t_fine, TempUnit unit = TempUnit_Celsius):
float CalculateTemperature(int32_t raw, int32_t& t_fine, TempUnit unit = TempUnit_Celsius);Calculates the temperature value based on raw data and calibration parameters. t_fine is needed to calibrate the temperature value and is internally calculated following informations reported in the Bosch-datasheet. Tempunit unit specifies the temperature format (default is celsius degrees).
-
CalculatePressure(int32_t adc_P, int32_t t_fine, PresUnit unit):
float CalculatePressure(int32_t adc_P, int32_t t_fine, PresUnit unit);Calculates the pressure value based on raw data and calibration parameters. t_fine is needed to calibrate the pressure value and is internally calculated following informations reported in the Bosch-datasheet. PressUnit unit specifies the pressure format (default is Pascal (Pa)).
-
ReadData(int32_t data[SENSOR_DATA_LENGTH]):
bool ReadData(int32_t data[SENSOR_DATA_LENGTH]);Reads raw temperature and pressure data from the sensor and stores them into a local buffer for future compensation.
-
*readRegister(uint8_t reg, uint8_t buff, uint8_t length):
bool readRegister(uint8_t reg, uint8_t *buff, uint8_t length);Reads specified bytes from a register. Return 1 if successful.
-
writeRegister(uint8_t reg, uint8_t value):
void writeRegister(uint8_t reg, uint8_t value);Writes a specified value into a register.
- Address: The I2C address for the BMP280 can be either 0x76 (if SDO is connected to Vdd) or 0x77 (if SDO is connected to GND). In the Aht20-BMP280 shield the SDO line was connected to GND, therefore 0x77 must be used.
Basic setup, initialization, and data retrieval for the BMP280 sensor are demonstrated in the bmp280_example folder.