| 本帖最后由 aramy 于 2025-10-15 10:09 编辑 
 自打上次使用esp-idf驱动起来了GDI接口的显示屏后,就想着同时驱动起GDI接口中的I2C总线的温湿度传感器(SHT30)。可是使用esp-idf的例程,无论是硬件I2C还是软件模拟I2C,去读取sht20都失败了。
 
  来来回回搞了几天,无奈技术太菜,解决不了问题。最终放弃使用esp-idf。
 
  
  切换回Arduino编程,感觉一切又变得简单起来了。
 第一步:驱动SPI屏幕。扩展板上有块ST7789屏幕,使用SIP总线,通过GDI接口与开发板连接。查阅官方文档,可以查到GDI口中对应的管脚信息。安装第三方屏幕驱动库"AdafruitST7789",然后在程序中修改对应管脚。   
 第二步:驱动扩展板上的温湿度传感器SHT30。这里也是使用第三方驱动库。 需要留意一下,GDI接口中的I2C接口是接在9、10两个管脚上的。需要在Wire类初始化时,修改一下对应的管脚。 第三步:就是把传感器收集来的数据通过屏幕展示出来。复制代码#include "Adafruit_SHT31.h"
#define SDA_PIN 9
#define SCL_PIN 10
Wire.begin(SDA_PIN, SCL_PIN);
 复制代码#include <Adafruit_GFX.h>     // Core graphics library
#include <Adafruit_ST7789.h>  // Hardware-specific library for ST7789
#include <SPI.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"
#define SDA_PIN 9
#define SCL_PIN 10
#define TFT_CS 27
#define TFT_RST 26
#define TFT_DC 8
#define TFT_BLK 15
#define TFT_MOSI 24  // Data out
#define TFT_SCLK 23  // Clock out
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
Adafruit_SHT31 sht31 = Adafruit_SHT31();
void setup(void) {
  Serial.begin(115200);
  Wire.begin(SDA_PIN, SCL_PIN);
  if (!sht31.begin(0x44)) {
    Serial.println("找不到SHT31");
    while (1) delay(1);
  }
  pinMode(TFT_BLK, OUTPUT);
  digitalWrite(TFT_BLK, HIGH);  //开背光
  tft.init(172, 320);           // Init ST7789 280x240
  tft.setSPISpeed(40000000);
  //  tft.fillScreen(ST77XX_BLACK);
  tft.setTextWrap(false);
}
void loop() {
  char m_str[6];
  float temp = sht31.readTemperature();
  float hum = sht31.readHumidity();
  if (!isnan(temp)) {
    Serial.print("温度: ");
    Serial.print(temp);
    Serial.println(" *C");
  } else {
    Serial.println("读取温度失败");
  }
  if (!isnan(hum)) {
    Serial.print("湿度: ");
    Serial.print(hum);
    Serial.println(" %");
  } else {
    Serial.println("读取湿度失败");
  }
  tft.fillScreen(ST77XX_BLACK);
  tft.setTextSize(5);
  tft.setTextColor(ST77XX_RED);
  tft.setCursor(5, 50);
  tft.println("T:");
  tft.setCursor(5, 100);
  dtostrf(temp, 3, 1, m_str); 
  tft.println(m_str);
  tft.setTextColor(ST77XX_GREEN);
  tft.setCursor(0, 180);
  tft.println("H:");
  tft.setCursor(0, 230);  
  dtostrf(hum, 3, 1, m_str); 
  tft.println(m_str);
  delay(2000);
}
   成功读取并展示温湿度信息。不过温度与实际偏高,其原因是SHT30安装位置太贴近ESP32-C5的开发板了,MCU工作时导致周围温度升高。
 
 
 
 
 |