首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将CO2传感器连接到Arduino卡并显示/保存其数据

如何将CO2传感器连接到Arduino卡并显示/保存其数据
EN

Stack Overflow用户
提问于 2021-03-01 04:16:06
回答 1查看 115关注 0票数 0

我第一次尝试使用Arduino做一个学校项目,但我对它了解不多,所以我希望你能帮助我。我买了一个CO2传感器,我想把它连接到一个"ELEGOO MEGA 2560 r3“(声称是Arduino Uno)来读取传感器的值。更具体地说,我将拥有这个工具包:

°https://www.amazon.fr/ELEGOO-D%C3%A9marrage-dUtilisation-D%C3%A9butants-Professionnels/dp/B01JD043XC/ref=sr_1_1?__mk_fr_FR=%C3%85M%C3%85%C5%BD%C3%95%C3%91&dchild=1&keywords=ELEGOO+Mega+2560+R3+Kit+de+D%C3%A9marrage+Ultime+Le+Plus+Complet+avec+Manuel+d%27Utilisation+Fran%C3%A7ais+pour+D%C3%A9butants+et+Professionnels+DIY+Compatible+avec+Arduino+IDE&qid=1613500133&s=computers&sr=1-1

这个感应器:

°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个垫与一根电线连接。

代码语言:javascript
运行
复制
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,但许多都是冗余的。按照以下方式连接到您的主板:

代码语言:javascript
运行
复制
5v DC -----> Pad 17
Ground ---> Pad 16
TX ----------> Pad 18
RX ----------> Pad 19"

我想我只需要使用相同的,但我不知道把它们放在我的板上的什么地方。

对于代码,我希望每隔x秒输出一次CO2的值,并将时间和值保存在一个文件中,以便在使用python后创建一个图形(我知道如何编写python,但不知道如何编写Arduino)。我在我之前链接的教程中看到了这段代码。

代码语言:javascript
运行
复制
#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, 0x00, 0x00, 0x00, 0x00, 0x79};  // get gas command
byte cmdCal[9] = {0xFF, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 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/网站,但我真的不明白为什么我需要这个来读取传感器的值?我对"Blinks“或"ArduinoOTA”(https://blynk.io/ ?)也有同样的问题。我猜这是一个很酷的应用程序,但我认为这有点太多了,我只需要显示(在终端或屏幕上,因为盒子里有一个屏幕,但我可能无法使用屏幕,而我甚至不知道如何将我的传感器连接到我的电路板上)并保存数据,然后将它们保存在文本文件中,例如使用Python并创建图形,我不需要应用程序或类似的东西。

我还需要校准它,如果我理解得很好,我只需要要求传感器自己完成它,发送这个weir命令?"0xFF,0x01,0x87,0x00,0x00,0x00,0x00,0x00,0x00,0x78“,像这样"Serial.write(cmdCal,9);"?

我真的不太理解这段代码,因为我从来没有用过Arduino它对我来说似乎有点复杂,有人能帮我理解它或帮助我简化它只有两个命令,一个每秒打印值(我可以通过复制并粘贴到文本文件中来保存它,但如果代码能做到这一点更好)和校准?

对于所有这些问题我很抱歉,我是Arduino的初学者,所以我希望有人能帮助我:)

祝你今天过得愉快,海泽林

EN

回答 1

Stack Overflow用户

发布于 2021-03-01 04:28:43

代码语言:javascript
运行
复制
Serial.begin(9600);

此命令指示Arduino串行接口设置波特率为9600bps的通信。这是后续串行命令Serial.Print的先决条件。如果您已将Arduino与PC连接,则可以在终端仿真器上看到它们。

这就是校准的想法。根据制造商提供的图表,您可以看到,当没有CO2时,传感器的输出为0.4v。即使它是指定的输出,该值也可以变化,例如,它可以是0.35v或0.43v,CO2为0。制造商建议在0 CO2环境中定期打开传感器,并以正确表示0值的方式调整代码中的电压或ADC值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66413450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档