前幾天介紹一些開發工具現在使用 PlatformIO 來開發,現在進入正式開發先從小東西開始!溫度。這邊就紹如何取的溫度,依照手邊材料目前有 AHT10。
AHT10
接線方式:
SDA -> D2(gpio 4)
SCL -> D3(gpio 0)
VIN -> 3.3v
GND -> GND
配接方式
#include <Arduino.h>
#include <Wire.h>
void setup()
{
Wire.begin(0, 4);
Serial.begin(115200);
}
// the loop function runs over and over again forever
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for (address = 1; address < 127; address++)
{
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address < 16)
Serial.print("0");
Serial.print(address, HEX);
Serial.println(" !");
nDevices++;
}
else if (error == 4)
{
Serial.print("Unknow error at address 0x");
if (address < 16)
Serial.print("0");
Serial.println(address, HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
i2c 掃描
這邊要設定專案並且,指定或安裝套件。
先開啟專案設定檔編輯設定。
設定檔
設定檔說明。
[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
#序列PORT輸出的設定
monitor_speed = 9600
lib_deps =
# 我們辪接指定要用的程式庫位置即可
https://github.com/Thinary/AHT10
溫度偵測程式碼。
#include <Arduino.h>
#include <Wire.h>
#include <Thinary_AHT10.h>
AHT10Class AHT10;
void setup()
{
Wire.begin(0, 4);
Serial.begin(9600);
Serial.println(F("AHT10 test!"));
if (AHT10.begin(eAHT10Address_default))
Serial.println("Init AHT10 Sucess.");
else
Serial.println("Init AHT10 Failure.");
AHT10.Reset();
}
// the loop function runs over and over again forever
void loop()
{
Serial.println(String("") + "Humidity(%RH):\t\t" + AHT10.GetHumidity() + "%");
Serial.println(String("") + "Temperature(C):\t" + AHT10.GetTemperature() + " C");
delay(1000);
}
這邊是輸出的部分。
Humidity(%RH): 51.78%
Temperature(C): 28.66 C
Humidity(%RH): 51.76%
Temperature(C): 28.69 C
Humidity(%RH): 51.79%
Temperature(C): 28.70 C
Humidity(%RH): 51.76%
Temperature(C): 28.71 C
Humidity(%RH): 51.79%
Temperature(C): 28.71 C
Humidity(%RH): 51.75%
Temperature(C): 28.72 C
Humidity(%RH): 51.77%
Temperature(C): 28.71 C
Humidity(%RH): 51.74%
Temperature(C): 28.71 C
Humidity(%RH): 51.73%
Temperature(C): 28.71 C
Humidity(%RH): 51.74%
Temperature(C): 28.71 C
Humidity(%RH): 51.78%
Temperature(C): 28.72 C
今天就到這了感謝收看