ESP32 does not read data from AHT25 sensor?

192 Views Asked by At

I am using ESP32 to read temperature and humidity from AHT25 sensor. I am using ESP-IDF 5.1 tool. I am new to this tool. This is my source code,

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "driver/i2c.h"

static const char *TAG = "AHT25_Sensor";

#define I2C_MASTER_SCL_IO 22        // GPIO for I2C 
  clock line
#define I2C_MASTER_SDA_IO 21        // GPIO for I2C data line
#define I2C_MASTER_NUM I2C_NUM_1    // I2C port number
#define I2C_MASTER_FREQ_HZ 100000   // I2C clock frequency

#define AHT25_I2C_ADDR 0x38         // I2C address of AHT25 sensor

esp_err_t i2c_master_init() {
  i2c_config_t conf = {
    .mode = I2C_MODE_MASTER,
    .sda_io_num = I2C_MASTER_SDA_IO,
    .sda_pullup_en = GPIO_PULLUP_ENABLE,
    .scl_io_num = I2C_MASTER_SCL_IO,
    .scl_pullup_en = GPIO_PULLUP_ENABLE,
    .master.clk_speed = I2C_MASTER_FREQ_HZ,
    i2c_driver_install(I2C_MASTER_NUM, conf.mode, 0, 0, 0),
};
return i2c_param_config(I2C_MASTER_NUM, &conf);
}

void aht25_task(void *pvParameters) {
  ESP_LOGI(TAG, "aht25_task has started");
  uint8_t data[6];

while (1) {
    i2c_cmd_handle_t cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, (AHT25_I2C_ADDR << 1) | I2C_MASTER_WRITE, true);
    i2c_master_write_byte(cmd, 0xAC, true);
    i2c_master_write_byte(cmd, 0x33, true);
    i2c_master_stop(cmd);
    i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_PERIOD_MS);
    i2c_cmd_link_delete(cmd);

    vTaskDelay(1000 / portTICK_PERIOD_MS);

    cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, (AHT25_I2C_ADDR << 1) | I2C_MASTER_READ, true);
    i2c_master_read_byte(cmd, &data[0], I2C_MASTER_ACK);
    i2c_master_read_byte(cmd, &data[1], I2C_MASTER_ACK);
    i2c_master_read_byte(cmd, &data[2], I2C_MASTER_ACK);
    i2c_master_read_byte(cmd, &data[3], I2C_MASTER_ACK);
    i2c_master_read_byte(cmd, &data[4], I2C_MASTER_ACK);
    i2c_master_read_byte(cmd, &data[5], I2C_MASTER_NACK);
    i2c_master_stop(cmd);
    i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_PERIOD_MS);
    i2c_cmd_link_delete(cmd);

    float humidity = ((data[1] << 12) | (data[2] << 4) | (data[3] >> 4)) * 100 / 0x100000;
    float temperature = (((data[3] & 0xF) << 16) | (data[4] << 8) | data[5]) * 200 / 0x1000000 - 50;

    ESP_LOGI(TAG, "Humidity: %.2f%%, Temperature: %.2f°C", humidity, temperature);

    vTaskDelay(5000 / portTICK_PERIOD_MS);
}
}

void app_main() {
  esp_err_t ret = i2c_master_init();
  if (ret != ESP_OK) {
    ESP_LOGE(TAG, "I2C master initialization failed");
    return;
}

while (1) {
        aht25_task(NULL);  // Manually call the task function
        vTaskDelay(5000 / portTICK_PERIOD_MS);  // Delay between iterations
    }
 }

But it does not read data from sensor, I got output like, enter image description here

I am new to Espressif IDE tool. I already read data from sensor through Arduino IDE, it works fine there. But I am struggling in this tool. Can anyone help me to solve this error?

0

There are 0 best solutions below