### OLED Display Integration Example Source: https://context7.com/adafruit/adafruit_ahtx0/llms.txt Integrates the AHT20 sensor with an OLED display to show real-time temperature and humidity readings. This example showcases combining the AHTX0 library with other Adafruit libraries for complete projects. ```cpp #include #include #include #include Adafruit_SH1107 display = Adafruit_SH1107(64, 128, &Wire); Adafruit_AHTX0 aht; void setup() { Serial.begin(115200); // Initialize OLED display display.begin(0x3C, true); display.display(); delay(1000); display.clearDisplay(); display.setRotation(1); display.setTextSize(1); display.setTextColor(SH110X_WHITE); // Initialize AHT20 sensor if (aht.begin()) { Serial.println("Found AHT20"); } else { Serial.println("Didn't find AHT20"); } } void loop() { display.clearDisplay(); sensors_event_t humidity, temp; // Get sensor readings aht.getEvent(&humidity, &temp); // Display on OLED display.setCursor(0, 20); display.print("AHT20 Demo"); display.setCursor(0, 40); display.print("Temp: "); display.print(temp.temperature); display.println(" C"); display.setCursor(0, 60); display.print("Hum: "); display.print(humidity.relative_humidity); display.println(" %"); display.display(); delay(100); } ``` -------------------------------- ### Include Adafruit AHTX0 Library Source: https://context7.com/adafruit/adafruit_ahtx0/llms.txt Include the library header file in your Arduino sketch. Ensure Adafruit BusIO and Adafruit Unified Sensor are installed as dependencies. ```cpp // Required dependencies: // - Adafruit BusIO // - Adafruit Unified Sensor #include ``` -------------------------------- ### Get Humidity Sensor Pointer Source: https://context7.com/adafruit/adafruit_ahtx0/llms.txt Obtain an Adafruit_Sensor pointer for the humidity component. This enables independent humidity readings and access to sensor metadata. The humidity range is 0% to 100% relative humidity. ```cpp #include Adafruit_AHTX0 aht; Adafruit_Sensor *aht_humidity; void setup() { Serial.begin(115200); while (!Serial) delay(10); if (!aht.begin()) { Serial.println("Failed to find AHT10/AHT20 chip"); while (1) delay(10); } // Get the humidity sensor object aht_humidity = aht.getHumiditySensor(); // Print sensor details aht_humidity->printSensorDetails(); // Output: // Sensor: AHTx0_H // Type: Relative Humidity (%) // Min Value: 0.00 // Max Value: 100.00 // Resolution: 2.00 } void loop() { sensors_event_t humidity; // Get humidity reading independently aht_humidity->getEvent(&humidity); Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println(" % rH"); delay(100); } ``` -------------------------------- ### Get Temperature Sensor Pointer Source: https://context7.com/adafruit/adafruit_ahtx0/llms.txt Retrieve an Adafruit_Sensor pointer for the temperature component. This allows independent temperature readings and access to sensor metadata. The temperature range is -40°C to 85°C. ```cpp #include Adafruit_AHTX0 aht; Adafruit_Sensor *aht_temp; void setup() { Serial.begin(115200); while (!Serial) delay(10); if (!aht.begin()) { Serial.println("Failed to find AHT10/AHT20 chip"); while (1) delay(10); } // Get the temperature sensor object aht_temp = aht.getTemperatureSensor(); // Print sensor details (name, range, resolution) aht_temp->printSensorDetails(); // Output: // Sensor: AHTx0_T // Type: Ambient Temperature (C) // Min Value: -40.00 // Max Value: 85.00 // Resolution: 0.30 } void loop() { sensors_event_t temp; // Get temperature reading independently aht_temp->getEvent(&temp); Serial.print("Temperature: "); Serial.print(temp.temperature); Serial.println(" degrees C"); delay(100); } ``` -------------------------------- ### Initialize AHTX0 Sensor Source: https://context7.com/adafruit/adafruit_ahtx0/llms.txt Use begin() to initialize the sensor hardware. You can use the default I2C address or specify a custom Wire instance and address. ```cpp #include Adafruit_AHTX0 aht; void setup() { Serial.begin(115200); // Initialize with default I2C address (0x38) and default Wire if (!aht.begin()) { Serial.println("Could not find AHT? Check wiring"); while (1) delay(10); } Serial.println("AHT10 or AHT20 found"); } // Alternative: Initialize with custom parameters void setup_custom() { Serial.begin(115200); // begin(TwoWire *wire, int32_t sensor_id, uint8_t i2c_address) // Use alternate I2C address 0x39 if (!aht.begin(&Wire, 0, 0x39)) { Serial.println("Could not find AHT at alternate address"); while (1) delay(10); } Serial.println("AHT found at alternate address"); } ``` -------------------------------- ### Adafruit_AHTX0::begin() Source: https://context7.com/adafruit/adafruit_ahtx0/llms.txt Initializes the sensor hardware and I2C communication, performing a soft reset and calibration. ```APIDOC ## Adafruit_AHTX0::begin() ### Description Initializes the sensor hardware and I2C communication. This method performs a soft reset, waits for the sensor to become ready, sends calibration commands, and verifies the sensor is properly calibrated. ### Parameters - **wire** (TwoWire*) - Optional - Pointer to the I2C bus object. - **sensor_id** (int32_t) - Optional - Unique sensor ID. - **i2c_address** (uint8_t) - Optional - I2C address (default 0x38, alternate 0x39). ### Response - **bool** - Returns true if initialization was successful, false otherwise. ``` -------------------------------- ### Unified Sensor Interface Usage Source: https://context7.com/adafruit/adafruit_ahtx0/llms.txt Demonstrates using the Adafruit Unified Sensor interface to read both temperature and humidity independently. This approach ensures interoperability with other Adafruit sensor libraries. ```cpp #include Adafruit_AHTX0 aht; Adafruit_Sensor *aht_humidity, *aht_temp; void setup() { Serial.begin(115200); while (!Serial) delay(10); if (!aht.begin()) { Serial.println("Failed to find AHT10/AHT20 chip"); while (1) delay(10); } Serial.println("AHT10/AHT20 Found!"); // Get both sensor objects aht_temp = aht.getTemperatureSensor(); aht_humidity = aht.getHumiditySensor(); // Print details for both sensors aht_temp->printSensorDetails(); aht_humidity->printSensorDetails(); } void loop() { sensors_event_t humidity; sensors_event_t temp; // Read each sensor independently aht_humidity->getEvent(&humidity); aht_temp->getEvent(&temp); Serial.print("Temperature: "); Serial.print(temp.temperature); Serial.println(" deg C"); Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println(" % rH"); delay(100); } ``` -------------------------------- ### Read Sensor Data with getEvent Source: https://context7.com/adafruit/adafruit_ahtx0/llms.txt Retrieve temperature and humidity readings in a single I2C transaction using the Adafruit Unified Sensor event structure. ```cpp #include Adafruit_AHTX0 aht; void setup() { Serial.begin(115200); if (!aht.begin()) { Serial.println("Could not find AHT? Check wiring"); while (1) delay(10); } } void loop() { sensors_event_t humidity, temp; // Read both temperature and humidity in one call if (aht.getEvent(&humidity, &temp)) { Serial.print("Temperature: "); Serial.print(temp.temperature); Serial.println(" degrees C"); Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println("% rH"); } delay(500); } // Output: // Temperature: 25.32 degrees C // Humidity: 48.75% rH ``` -------------------------------- ### Check Sensor Status Source: https://context7.com/adafruit/adafruit_ahtx0/llms.txt Read the status byte to determine if the sensor is busy or calibrated. Returns 0xFF on failure. ```cpp #include Adafruit_AHTX0 aht; void setup() { Serial.begin(115200); if (!aht.begin()) { Serial.println("Could not find AHT"); while (1) delay(10); } } void loop() { uint8_t status = aht.getStatus(); if (status == 0xFF) { Serial.println("Failed to read status"); } else { // Check if sensor is busy if (status & 0x80) { Serial.println("Sensor is busy"); } // Check if sensor is calibrated if (status & 0x08) { Serial.println("Sensor is calibrated"); } } delay(1000); } ``` -------------------------------- ### Adafruit_AHTX0::getEvent() Source: https://context7.com/adafruit/adafruit_ahtx0/llms.txt Reads both humidity and temperature values from the sensor in a single I2C transaction. ```APIDOC ## Adafruit_AHTX0::getEvent() ### Description Reads both humidity and temperature values from the sensor in a single I2C transaction. This method triggers a measurement, waits for completion, and populates the provided sensor event structures. ### Parameters - **humidity** (sensors_event_t*) - Required - Pointer to store humidity data. - **temp** (sensors_event_t*) - Required - Pointer to store temperature data. ### Response - **bool** - Returns true if the read operation was successful. ``` -------------------------------- ### Adafruit_AHTX0::getStatus() Source: https://context7.com/adafruit/adafruit_ahtx0/llms.txt Reads the status byte from the sensor to check busy and calibration states. ```APIDOC ## Adafruit_AHTX0::getStatus() ### Description Reads the status byte from the sensor which indicates the current state including busy and calibration status. ### Response - **uint8_t** - The status byte (0xFF if read fails). Contains flags: AHTX0_STATUS_BUSY (0x80) and AHTX0_STATUS_CALIBRATED (0x08). ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.