以下是Arduino IDE下esp8266 nodemcu控制JW01使用IIC协议采集二氧化碳参数的代码,以及连线:

代码:

#include <Wire.h>

#define JW01_IIC_ADDRESS 0x15 // JW01的IIC地址

void setup() {
  Serial.begin(9600);
  Wire.begin();
}

void loop() {
  Wire.beginTransmission(JW01_IIC_ADDRESS);
  Wire.write(0x03); // 发送命令,读取CO2浓度
  Wire.endTransmission();

  delay(100);

  Wire.requestFrom(JW01_IIC_ADDRESS, 4);
  if (Wire.available() == 4) {
    uint16_t co2 = Wire.read() << 8 | Wire.read(); // CO2浓度
    uint16_t temp = Wire.read() << 8 | Wire.read(); // 温度
    Serial.print("CO2: ");
    Serial.print(co2);
    Serial.print(", Temperature: ");
    Serial.println(temp / 100.0);
  }

  delay(1000);
}

连线:

  • JW01的VCC连接到NodeMCU的3.3V引脚
  • JW01的GND连接到NodeMCU的GND引脚
  • JW01的SDA连接到NodeMCU的D2引脚
  • JW01的SCL连接到NodeMCU的D1引脚
Arduino IDE下esp8266 nodemcu控制JW01使用IIC协议采集二氧化碳参数的代码以及连线

原文地址: https://www.cveoy.top/t/topic/bTcb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录