问题描述
我第一次尝试将 Arduino 用于学校项目,但我对此知之甚少,所以希望您能帮助我。 我买了一个 CO2 传感器,我想将它连接到“ELEGOO MEGA 2560 r3”(声称用作 Arduino Uno)以读取传感器的值。更具体地说,我将拥有此套件:
还有这个传感器:
°https://www.winsen-sensor.com/d/files/infrared-gas-sensor/mh-z14a_co2-manual-v1_01.pdf
(°https://wiki.dfrobot.com/Gravity__Analog_Infrared_CO2_Sensor_For_Arduino_SKU__SEN0219#target₆
° https://docs.rs-online.com/c20e/A700000006944474.pdf) → 这是另一个看似相同但可能不相同的传感器。
我想知道我是否必须购买其他组件,或者收集传感器的数据是否可以。我看到它有 19 个圆形焊盘和 8 个用电线连接它们的焊盘。
PIN Description
Pad1/Pad15/Pad17/Pad23 Vin Voltage Input
Pad2/Pad3/Pad12/Pad16/Pad22 GND
Pad4/Pad21 Analogoutput(0.4~2V)
Pad6/Pad26 PWM
Pad8/Pad20 HD(Zerocalibration,lowlevelformorethan7secondsvalid)
Pad7/Pad9 NC
Pad11/Pad14/Pad18/Pad24 UART(RXD)TTLLevelinput
Pad10/Pad13/Pad19/Pad25 UART(TXD)TTLLeveloutput
GND 是地面,但我真的不知道其他的以及如何将它们连接到我的“ELEGOO MEGA 2560 r3”以获取数据。
如果找到此链接,有人解释了他是如何做到的,但我没有相同的电路板。 https://www.hackster.io/kritch83/getting-started-with-the-mh-z14a-co2-detector-e96234
" 对于物理连接,我们将使用四根线:电源、地、RX 和 TX,这些是焊盘 19、18、17 和 16,但许多是多余的。按如下方式连接到您的电路板:
5v DC -----> Pad 17
Ground ---> Pad 16
TX ----------> Pad 18
RX ----------> Pad 19"
我想我只需要使用相同的,但我不知道将它们放在我的板上。
对于代码,我想每 x 秒打印一次空气中 CO2 的值,并将时间和值保存在文件中以在使用 python 后创建图形(我知道如何编写 python 但不知道如何编写 Arduino)。我在之前链接的教程中看到了这段代码。
#include <Arduino.h>
#include <ESP8266WiFi.h> // for OTA,Blynk,& restart command
#include <BlynkSimpleEsp8266.h> // for blynk
#include <ESP8266mDNS.h> // for OTA
#include <WiFiUdp.h> // for OTA
#include <ArduinoOTA.h> // for OTA
char auth[] = "token";
char ssid[] = "IoT";
char pass[] = "password";
byte cmd[9] = {0xFF,0x01,0x86,0x00,0x79}; // get gas command
byte cmdCal[9] = {0xFF,0x87,0x78}; // calibrate command
char response[9]; // holds the recieved data
int CO2ppm = 0;
unsigned long warmingTimer = 0;
unsigned long timer1 = 0;
void setup()
{
Serial.begin(9600); // Setup a serial connection with the sensor
Blynk.begin(auth,ssid,pass);
wifi_station_set_hostname("ESP-CO2 Monitor"); // network device hostname
ArduinoOTA.setHostname("ESP-CO2 Monitor"); // for OTA
ArduinoOTA.begin(); // for OTA
warmingTimer = millis(); // initilize warmup timer
}
void loop()
{
Blynk.run();
ArduinoOTA.handle(); // for OTA
while (millis() - warmingTimer < 310000)
{
Blynk.run();
ArduinoOTA.handle(); // for OTA
if (millis() - timer1 > 1000)
{
Blynk.virtualWrite(V2,(310000 - (millis() - warmingTimer)) / 1000); // counts down till the sensor is ready
timer1 = millis();
}
}
if (millis() - timer1 > 15000) // runs every 15 sec
{
getReadings();
maint();
timer1 = millis();
}
} // loop
void getReadings()
{
while (Serial.available()) // this clears out any garbage in the RX buffer
{
int garbage = Serial.read();
}
Serial.write(cmd,9); // Sent out read command to the sensor
Serial.flush(); // this pauses the sketch and waits for the TX buffer to send all its data to the sensor
while (!Serial.available()) // this pauses the sketch and waiting for the sensor responce
{
delay(0);
}
Serial.readBytes(response,9); // once data is avilable,it reads it to a variable
int responseHigh = (int)response[2];
int responseLow = (int)response[3];
CO2ppm = (256 * responseHigh) + responseLow;
Blynk.virtualWrite(V0,CO2ppm);
}
void calibrate()
{
Serial.write(cmdCal,9);
delay(3000);
}
void maint()
{
int RSSi = wifi_station_get_RSSi();
Blynk.virtualWrite(V12,RSSi);
}
BLYNK_WRITE(V10)
{
byte cali = param.asInt();
if (cali)
{
calibrate();
}
}
BLYNK_WRITE(V11) // restart chip
{
byte reset = param.asInt();
if (reset)
{
ESP.restart();
}
}
我完全不明白,“AOT”是什么意思?此外,我发现了该网站 https://arrayofthings.github.io/,但我真的不明白为什么我需要它来读取传感器的值?我对“闪烁”或“ArduinoOTA”(https://blynk.io/ ?)有同样的问题。我想是有一个很酷的应用程序,但我觉得有点太多了,我只需要显示(在终端或屏幕上,因为盒子里有屏幕,但我可能无法使用屏幕虽然我什至不知道如何将我的传感器连接到我的电路板)并保存数据,然后将它们保存在一个文本文件中,例如与 Python 一起使用它并创建一个图形,但我不需要应用程序或类似的东西.
我还需要校准它,如果我理解得很好,我只需要通过发送此堰命令让传感器自己完成? “0xFF,0x78” 像“Serial.write(cmdCal,9);”?
我不太理解这段代码,因为我从未使用过 Arduino,这对我来说似乎有点复杂,有人可以帮助我理解它或帮助我简化它,使其只有 2 个命令,每个命令都打印值秒(我可以通过将其复制并粘贴到文本文件中来保存它,但如果代码这样做会更好)并校准?
对于所有这些问题,我很抱歉,我是 Arduino 的初学者,所以我希望有人能帮助我 :)
祝你有美好的一天,欣策林
解决方法
Serial.begin(9600);
此命令指示 Arduino 串行接口以 9600bps 波特率设置通信。这是后续串行命令 Serial.Print 的先决条件。如果您已将 Arduino 与 PC 连接,您可以在终端模拟器上看到它们。
这就是校准的想法。根据制造商提供的图表,您可以看到在没有 CO2 的情况下,Sensor 的输出为 0.4v。即使它是指定的输出,该值也可能会有所不同,例如,它可以是 0.35v 或 0.43v,CO2 为 0。制造商建议在 0 CO2 环境中定期打开传感器,并以正确代表 0 值的方式调整代码中的电压或 ADC 值。