利用DHT22和Arduino测量温湿度并显示在串口和OLED显示屏上
生活随笔
收集整理的這篇文章主要介紹了
利用DHT22和Arduino测量温湿度并显示在串口和OLED显示屏上
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實驗結果
溫濕度顯示在串口:
溫濕度顯示在OLED屏幕:
實驗代碼
#include "U8glib.h" #include "DHT.h"U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE); #define DHTTYPE DHT22 // DHT 22 (AM2302) #define DHTPIN 2 // what pin we're connected to DHT dht(DHTPIN, DHTTYPE);void setup() {// put your setup code here, to run once:u8g.setFont(u8g_font_8x13B);u8g.setFontRefHeightText();u8g.setFontPosTop();Serial.begin(9600); Serial.println("DHTxx test!");dht.begin(); }void loop() {// put your main code here, to run repeatedly:float h = dht.readHumidity();float t = dht.readTemperature();delay(2000);// check if returns are valid, if they are NaN (not a number) then something went wrong!if (isnan(t) || isnan(h)) {Serial.println("Failed to read from DHT");} else {Serial.print("Humidity: "); Serial.print(h);Serial.print(" %\t");Serial.print("Temperature: "); Serial.print(t);Serial.println(" *C");u8g.firstPage();do{u8g.drawStr(0, 0, "Hum");u8g.setPrintPos(40, 0);u8g.print(h);u8g.print("%");u8g.drawStr(0, 20, "Temp");u8g.setPrintPos(40, 20);u8g.print(t);u8g.print("c");}while(u8g.nextPage());} }實驗過程
先導入U8glib和DHT兩個庫,然后利用庫里面的函數做測試,了解各個函數都是怎么用的,比如
U8glib里面有個循環模塊,用于顯示
u8g.firstpage();//圖片循環模塊do{draw();//自己寫的函數}while (u8g.nextpage()) {delay(100);}U8gilb里面 u8g.setPrintPos(40, 0);固定了下個輸出在屏幕上的位置,第一個參數決定行,第二個參數決定列。
至于硬件的線如何連接,dth22的DAT引腳接Arduino的2引腳
OLED的SCL接Arduino的A5
OLED的SDA接Arduino的A4
其余的OLED和DTH22的GND,5V接Arduino的GND和5V。
總結
以上是生活随笔為你收集整理的利用DHT22和Arduino测量温湿度并显示在串口和OLED显示屏上的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java 使用反射处理注解
- 下一篇: CATIA 界面介绍