ESP32 SHT20驱动代码 ESP-IDF+VScode

本工程使用ESP-IDF5.0+VScode编程。

代码基于官方simple_i2c例程修改。

main函数内容。

#include <stdio.h>
#include "esp_log.h"
#include "driver/i2c.h"
#include "./../Include/SHT20.h"

static const char *TAG = "i2c-simple-example";

void app_main(void)
{

    ESP_ERROR_CHECK(i2c_master_init());
    ESP_LOGI(TAG, "I2C initialized successfully");

    while (1)
    {
        printf("Tempreture = %.2f℃\n", SHT20_Get_Tempreture());
        printf("Humidity = %.2f%%\n\n", SHT20_Get_Humidity());
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

SHT20.c文件内容

#include <stdio.h>
#include "driver/i2c.h"
#include "esp_log.h"
#include "./../Include/SHT20.h"

esp_err_t i2c_master_init(void)
{
    int i2c_master_port = I2C_MASTER_NUM;

    i2c_config_t conf = {
        .mode = I2C_MODE_MASTER,
        .sda_io_num = I2C_MASTER_SDA_IO,
        .scl_io_num = I2C_MASTER_SCL_IO,
        .sda_pullup_en = GPIO_PULLUP_ENABLE,
        .scl_pullup_en = GPIO_PULLUP_ENABLE,
        .master.clk_speed = I2C_MASTER_FREQ_HZ,
    };

    i2c_param_config(i2c_master_port, &conf);

    return i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
}
// 非占用总线的温度测量,默认14bit数据。
float SHT20_Get_Tempreture(void)
{
    uint8_t data[2] = {0, 0};
    float Tempreture = 0;
    i2c_cmd_handle_t handle = i2c_cmd_link_create();

    i2c_master_start(handle);
    i2c_master_write_byte(handle, SHT20_SENSOR_ADDR << 1 | 0, ACK_CHECK_EN);
    i2c_master_write_byte(handle, SHT20_SENSOR_TMEASUREMENT_NOHOLD_ADDR, ACK_CHECK_EN);
    vTaskDelay(0.02 / portTICK_PERIOD_MS);
    i2c_master_stop(handle);

    i2c_master_cmd_begin(I2C_MASTER_NUM, handle, 1000 / portTICK_PERIOD_MS);
    i2c_cmd_link_delete(handle);
    vTaskDelay(90 / portTICK_PERIOD_MS);

    handle = i2c_cmd_link_create();
    i2c_master_start(handle);
    i2c_master_write_byte(handle, SHT20_SENSOR_ADDR << 1 | 1, ACK_CHECK_EN);
    i2c_master_read_byte(handle, &data[0], ACK_VAL);
    i2c_master_read_byte(handle, &data[1], NACK_VAL);
    // i2c_master_read_byte(handle,&data[2],NACK_VAL);抛弃校验和,想要校验和,上一行改为ACK_VAL,取消注释此行
    i2c_master_stop(handle);

    i2c_master_cmd_begin(I2C_MASTER_NUM, handle, 1000 / portTICK_PERIOD_MS);
    i2c_cmd_link_delete(handle);

    Tempreture = (data[0] << 8) | (data[1] & 0xFC);
    Tempreture = 175.72 * ((float)Tempreture / 65536) - 46.85;
    return Tempreture;
}
// 非占用总线的湿度测量,默认12bit数据。
float SHT20_Get_Humidity(void)
{
    uint8_t data[2] = {0, 0};
    float Humidity = 0;
    i2c_cmd_handle_t handle = i2c_cmd_link_create();

    i2c_master_start(handle);
    i2c_master_write_byte(handle, SHT20_SENSOR_ADDR << 1 | 0, ACK_CHECK_EN);
    i2c_master_write_byte(handle, SHT20_SENSOR_RHMEASUREMENT_NOHOLD_ADDR, ACK_CHECK_EN);
    vTaskDelay(0.02 / portTICK_PERIOD_MS);
    i2c_master_stop(handle);

    i2c_master_cmd_begin(I2C_MASTER_NUM, handle, 1000 / portTICK_PERIOD_MS);
    i2c_cmd_link_delete(handle);
    vTaskDelay(35 / portTICK_PERIOD_MS);

    handle = i2c_cmd_link_create();
    i2c_master_start(handle);
    i2c_master_write_byte(handle, SHT20_SENSOR_ADDR << 1 | 1, ACK_CHECK_EN);
    i2c_master_read_byte(handle, &data[0], ACK_VAL);
    i2c_master_read_byte(handle, &data[1], NACK_VAL);
    // i2c_master_read_byte(handle,&data[2],NACK_VAL);抛弃校验和,想要校验和,上一行改为ACK_VAL,取消注释此行
    i2c_master_stop(handle);

    i2c_master_cmd_begin(I2C_MASTER_NUM, handle, 1000 / portTICK_PERIOD_MS);
    i2c_cmd_link_delete(handle);

    Humidity = (data[0] << 8) | (data[1] & 0xFC);
    Humidity = 125 * ((float)Humidity / 65536) - 6;
    return Humidity;
}

SHT20.h文件内容

#ifndef _SHT20_H_
#define _SHT20_H_

#define I2C_MASTER_SCL_IO 4         /*!< GPIO number used for I2C master clock */
#define I2C_MASTER_SDA_IO 15        /*!< GPIO number used for I2C master data  */
#define I2C_MASTER_NUM 1            /*!< I2C master i2c port number, the number of i2c peripheral interfaces available will depend on the chip */
#define I2C_MASTER_FREQ_HZ 400000   /*!< I2C master clock frequency */
#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
#define I2C_MASTER_TIMEOUT_MS 1000

#define ACK_CHECK_EN 0x1  /*!< I2C master will check ack from slave*/
#define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
#define ACK_VAL 0x0       /*!< I2C ack value */
#define NACK_VAL 0x1      /*!< I2C nack value */

#define SHT20_SENSOR_ADDR 0x40                      /*!< Slave address of the SHT20 sensor */
#define SHT20_SENSOR_TMEASUREMENT_NOHOLD_ADDR 0xF3  /*!< Register addresses of the "who am I" register */
#define SHT20_SENSOR_RHMEASUREMENT_NOHOLD_ADDR 0xF5 /*!< Register addresses of the "who am I" register */
// #define SHT20_SENSOR_USER_REG_ADDR 0xE7             /*!< Register addresses of the "who am I" register */

esp_err_t i2c_master_init(void);
float SHT20_Get_Tempreture(void);
float SHT20_Get_Humidity(void);
#endif /*_SHT20_H_*/

网盘工程文件

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是ESP32-idf读取SHT30温湿度传感器的代码示例: ```c #include <stdio.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "driver/i2c.h" #define I2C_MASTER_SCL_IO 22 /*!< GPIO number for I2C master clock */ #define I2C_MASTER_SDA_IO 21 /*!< GPIO number for I2C master data */ #define I2C_MASTER_NUM I2C_NUM_0 /*!< I2C port number for master dev */ #define I2C_MASTER_FREQ_HZ 100000 /*!< I2C master clock frequency */ #define SHT30_ADDRESS 0x44 /*!< I2C address for SHT30 sensor */ static void i2c_master_init() { i2c_config_t conf; conf.mode = I2C_MODE_MASTER; conf.sda_io_num = I2C_MASTER_SDA_IO; conf.sda_pullup_en = GPIO_PULLUP_ENABLE; conf.scl_io_num = I2C_MASTER_SCL_IO; conf.scl_pullup_en = GPIO_PULLUP_ENABLE; conf.master.clk_speed = I2C_MASTER_FREQ_HZ; i2c_param_config(I2C_MASTER_NUM, &conf); i2c_driver_install(I2C_MASTER_NUM, conf.mode, 0, 0, 0); } static esp_err_t i2c_master_sht30_read(uint8_t* data, size_t size) { i2c_cmd_handle_t cmd = i2c_cmd_link_create(); i2c_master_start(cmd); i2c_master_write_byte(cmd, (SHT30_ADDRESS << 1) | I2C_MASTER_WRITE, true); i2c_master_write_byte(cmd, 0x2C, true); i2c_master_write_byte(cmd, 0x06, true); i2c_master_stop(cmd); esp_err_t ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS); i2c_cmd_link_delete(cmd); if (ret != ESP_OK) { return ret; } vTaskDelay(20 / portTICK_RATE_MS); cmd = i2c_cmd_link_create(); i2c_master_start(cmd); i2c_master_write_byte(cmd, (SHT30_ADDRESS << 1) | I2C_MASTER_READ, true); if (size > 1) { i2c_master_read(cmd, data, size - 1, I2C_MASTER_ACK); } i2c_master_read_byte(cmd, data + size - 1, I2C_MASTER_NACK); i2c_master_stop(cmd); ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS); i2c_cmd_link_delete(cmd); return ret; } void app_main() { i2c_master_init(); uint8_t data[6] = {0}; while (1) { esp_err_t ret = i2c_master_sht30_read(data, sizeof(data)); if (ret != ESP_OK) { printf("SHT30 read failed: %d\n", ret); continue; } uint16_t temp_raw = (data[0] << 8) | data[1]; uint16_t hum_raw = (data[3] << 8) | data[4]; float temperature = -45 + 175 * ((float)temp_raw / 65535); float humidity = 100 * ((float)hum_raw / 65535); printf("Temperature: %.2f°C, Humidity: %.2f%%\n", temperature, humidity); vTaskDelay(1000 / portTICK_RATE_MS); } } ``` 首先需要在 `i2c_master_init()` 函数中初始化I2C总线。然后在 `i2c_master_sht30_read()` 函数中,发送读取SHT30传感器的命令,并读取温湿度数据。最后通过位运算和公式计算得到温度和湿度值。在主函数中循环读取SHT30传感器,并打印温湿度信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值