arduino + AHT20 + 0.96 oled 显示温湿度

2021/12/17 23:20:54

本文主要是介绍arduino + AHT20 + 0.96 oled 显示温湿度,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_AHTX0.h>
#include <Fonts/FreeSans9pt7b.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


Adafruit_AHTX0 aht;

void setup() {
  Serial.begin(115200);
  //while (!Serial);
  
  Serial.println("128x64 OLED FeatherWing test");
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  Serial.println("OLED begun");

  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
  display.display();
  delay(3000);

  // Clear the buffer.
  display.clearDisplay();
  display.display();

  display.setRotation(0); //显示方向
  display.setFont(&FreeSans9pt7b);
  if (aht.begin()) {
    Serial.println("Found AHT20");
  } else {
    Serial.println("Didn't find AHT20");
  }  

  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);  
}

void loop() {
  display.clearDisplay();
  sensors_event_t humidity, temp;
  
  aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
  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(" %");
  Serial.print("Temperature: ");Serial.print(temp.temperature);Serial.println(" degrees C");
  Serial.print("Pressure: ");Serial.print(humidity.relative_humidity);Serial.println(" RH %");
  
  yield();
  display.display();
  delay(100);
}


这篇关于arduino + AHT20 + 0.96 oled 显示温湿度的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程