Gravity to Qwiic Adapter

产品简介

Gravity to Qwiic I2C Adapter 是一款用于连接 DFRobot Gravity 生态 I2C 传感器与 Qwiic 生态系统的转接板。产品 PCB 两端为标准 Qwiic 接口(JST SH 4Pin),支持 Qwiic 总线级联;PCB 中间提供 Gravity I2C 线序的排针接口(VCC / GND / SDA / SCL),用于直接连接现有 Gravity I2C 传感器模块。该转接板无需焊接、即插即用,可帮助用户在 Qwiic 生态下快速复用大量 Gravity I2C 传感器,降低跨生态使用门槛。

产品特性

  • 生态互通:将 DFRobot Gravity I2C 传感器无缝接入 SparkFun Qwiic / Adafruit STEMMA QT 等传感器生态
  • 即插即用:免焊接设计,两端 Qwiic 接口与中间 Gravity 排针接口均按标准线序引出,无需额外接线
  • 支持级联:标准 Qwiic 接口支持 I2C 总线级联(Daisy Chain),不占用额外主控 I/O 口

技术规格

参数 值
接口类型 Qwiic 端:JST SH 4Pin 母座 × 2。Gravity 端:2.54mm 4Pin 排针(VCC / GND / SDA / SCL)
工作温度 -40℃ ~ +85℃(取决于所接传感器)
供电电压 3.3V ~ 5V

引脚说明

引脚说明

引脚 说明
VCC 供电正极,3.3~5V DC输入
GND 供电地
SCL I2C时钟线
SDA I2C数据线

Arduino IDE使用教程

软硬件准备

硬件准备

产品名 SKU 数量 购买链接 描述
Arduino UNO R4 WiFi DFR1084 1 https://www.dfrobot.com.cn/goods-3709.html
Gravity: STCC4 二氧化碳传感器 SEN0678 1 https://www.dfrobot.com.cn/goods-4301.html
Gravity to Qwiic 转换器 1

软件准备

接线图

软硬件准备 接线图

传感器侧 引脚名称 主控侧 引脚名称
Gravity STCC4 二氧化碳传感器 VCC DFRduino UNO 5V
Gravity STCC4 二氧化碳传感器 GND DFRduino UNO GND
Gravity STCC4 二氧化碳传感器 SCL DFRduino UNO SCL
Gravity STCC4 二氧化碳传感器 SDA DFRduino UNO SDA

示例代码1-I2C 地址检测

此教程将展示使用Gravity to Qwiic转换器,扫描传感器地址并输出
注:Arduino UNO R4 WIFI的Qwiic接口,在Wire库中的标识符为Wire1,而非默认的Wire。

#include <Wire.h>
void setup() {
 Wire1.begin(); // 如需非UNO R4 WiFi上使用此代码,将此处修改为Wire.begin();
 Serial.begin(115200);
 while (!Serial); 
 Serial.println("\nI2C Scanner");
}
void loop() {
 byte error, address;
 int nDevices = 0;
 Serial.println("Scanning...");
 for (address = 1; address < 127; address++) {
   Wire1.beginTransmission(address);// 如需非UNO R4 WiFi上使用此代码,将此处修改为Wire.beginTransmission(address);
   error = Wire1.endTransmission();//error = Wire.endTransmission();
   if (error == 0) {
     Serial.print("I2C device found at address 0x");
     if (address < 16) Serial.print("0");
     Serial.println(address, HEX);
     nDevices++;
   } else if (error == 4) {
     Serial.print("Unknown 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("Scan complete\n");
 delay(5000); // Wait for 5 seconds before scanning again
}

结果

串口监视器输出I2C传感器地址:
示例代码1-I2C 地址检测 结果

示例代码2-获取传感器数据

本教程将使用 Arduino UNO R4 WiFi 和 Gravity to Qwiic 转接板连接 STCC4 传感器,通过 Arduino IDE 编写代码检测 I2C 地址并读取 CO2 数据,最终在串口监视器中看到传感器输出。

#include "DFRobot_STCC4.h"

#define ARDUINO_UNO_R4_WIFI //如需非UNO R4 WiFi上使用此代码,删除此行

const uint16_t tCompensation = 26;
const uint16_t hCompensation = 55;
const uint16_t pCompensation = 950;
float temperature;
float humidity;
uint16_t co2Concentration;
uint16_t sensorStatus;

const uint8_t ADDR = 0x64;

DFRobot_STCC4_I2C* sensor = NULL;

void setup() {
  Serial.begin(115200);
  unsigned long serialWaitStart = millis();
  while(!Serial && (millis() - serialWaitStart < 3000)) {
    delay(10); 
  }

#if defined(ARDUINO_UNO_R4_WIFI)
  static DFRobot_STCC4_I2C sensorInstance(&Wire1, ADDR);
#else
  static DFRobot_STCC4_I2C sensorInstance(&Wire, ADDR); 
#endif
  sensor = &sensorInstance;

  Serial.println("[STCC4] setup start");
  Serial.println("This is a demo of continuous reading sensor data.\n");

  /* Initialize the sensor */
  while(!sensor->begin()){
    Serial.println("Init error!");
    delay(500);
  }

  /* Wake up the sensor */
  sensor->wakeup();
  delay(10);

  /**
   * Get the ID of the sensor.
   * The ID values read should all be 0x901018A.
   */
  Serial.print("ID: 0x");
  Serial.println(sensor->getID(), HEX);

  /**
    * @brief Set temperature and humidity compensation
    * @param temperature Temperature compensation value
    * @param humidity Humidity compensation value
    * @return true if successful, false otherwise
    */
  if(sensor->setRHTcompensation(tCompensation, hCompensation)){
    Serial.println("Set RHT compensation successful.");
  }else{
    Serial.println("Set RHT compensation error!");
  }

  /**
    * @brief Set pressure compensation
    * @param pressure Pressure compensation value
    * @return true if successful, false otherwise
    */
  if(sensor->setPressureCompensation(pCompensation)){
    Serial.println("Set pressure compensation successful.");
  }else{
    Serial.println("Set pressure compensation error!");
  }

  sensor->startMeasurement(); // Start measurement
}

void loop() {
  delay(2000);
  if(sensor == NULL) {
    return;
  }
  /**
    * @brief Read measurement data
    * @param co2Concentration Pointer to store CO2 concentration
    * @param temperature Pointer to store temperature
    * @param humidity Pointer to store humidity
    * @param sensorStatus Pointer to store sensor status, 0 means normal, otherwise error.
    * @return true if successful, false otherwise
    */
  if(sensor->measurement(&co2Concentration,&temperature,&humidity,&sensorStatus)){
    Serial.print("CO2:");
    Serial.print(co2Concentration);
    Serial.print(" ppm ");
    Serial.print(" temperature:");
    Serial.print(temperature);
    Serial.print(" ℃ ");
    Serial.print(" humidity:");
    Serial.print(humidity);
    Serial.print(" % ");
    Serial.print(" status:");
    Serial.println(sensorStatus);
  }
}

结果

示例代码2-获取传感器数据 结果

Arduino UNO R4 WiFi代码修改建议

早期版本的 UNO 使用的是 8 位的 Atmel(现 Microchip)ATMega328 AVR 微控制器。而最新的 Arduino UNO R4 则基于先进的 32 位 Renesas RA4M1 芯片。因此,它具备多个通信外设等更强大的功能。

  • ATMega328 只配备了一个基于硬件的 I2C 模块,这个模块被识别为 Wire 对象,使用 Wire.begin()、Wire.write() 和 Wire.end() 等方法访问。从物理引脚来看,这组 I2C 被映射到 D18(SDA)和 D19(SCL)。大多数“Arduino”库都基于这一硬件接口以实现更高通信速度。
  • 而 RA4M1 拥有两个可用的 I2C 通信外设。其中一个是刚才提到的 D18 和 D19,另一个是新的 Qwiic 接口,对应引脚为 RA4M1 的 P401(SDA)和 P400(SCL)。这个第二个 I2C 接口就是 Wire1 对象。

您可以使用以下两种方式来修改传感器代码使其在UNO R4 的Qwiic接口上运行
不推荐,但快速的修改方式
有一种不推荐但快速的解决方法是直接修改传感器库文件中的Wire对象只想,将其修改为Wire1。
修改方法很简单,如下面的代码所示:

  bool begin(uint8_t addr = STCC4_DEFAULT_ADDRESS,
                 TwoWire *theWire = &Wire1);

但这牵涉到传感器底层库代码的修改。如果您之后在UNO R3或者ESP32开发板上使用相同的传感器库,这段代码将无法编译。因为他们的生态中没有Wire1对象。

繁琐但干净的修改方式
更好的做法是在自己的草图(sketch).ino文件中修改调用方式,而不是破坏库文件。也就是说,从外部传入Wire1对象地址。例如:

if (!sensor.begin(STCC4_DEFAULT_ADDRESS, &Wire1)){

因为各个库示例代码实现方式略有不同,所以这样修改代码的方式可能较为复杂。但无需修改底层库文件即可使用。必要时,也可使用AI辅助进行修改。

更多资料