damiaa 发表于 2021-3-1 23:40

【测评SGP40】+用arduino测试传感器 I2C通信

本帖最后由 damiaa 于 2021-3-2 08:43 编辑

<p>【测评SGP40】+用arduino测试传感器 I2C通信</p>

<p>本来用arduino测试SVM40 I2C通讯是一个容易的事。但折腾了一下。花了些时间。</p>

<p>1,下载arduino-snippets-main包 解压。</p>

<p>2,使用里面的\arduino-snippets-main\arduino-snippets-main\SVM40_I2C_minimal_example例子。</p>

<p>3,准备接好电路,传感器板 VCC GND接stm32F767板子 VCC&nbsp; 地, 3腿 SDA&nbsp;接 PB8,&nbsp; 4腿SCL PB9,<span style="color:#c0392b;">5腿&nbsp;接地</span></p>

<p></p>

<p>4,<span style="color:#990000;">PB8,PB9接4.7K上拉电阻。记得 否则是数据不变哦</span></p>

<p></p>

<p>5,打开\arduino-snippets-main\arduino-snippets-main\SVM40_I2C_minimal_example</p>

<p>6,用arduino1.8.13 打开SVM40_I2C_minimal_example.ino&nbsp;</p>

<p>注意这里已经是安装好stm32duino包了得。具体可以看我的帖子<span style="color:#3498db;"> 让arduino 把你的无用的stm32板子玩起来 之一</span></p>

<p>7,<span style="background-color:#2980b9;"><span style="color:#c0392b;">修改I2C时钟为100K</span>。</span>具体如下</p>

<pre>
<code>#include &lt;Wire.h&gt;
// SVM40
const int16_t SVM40_ADDRESS = 0x6A;
void setup() {
  Serial.begin(115200);
  // wait for serial connection from PC
  // comment the following line if you'd like the output
  // without waiting for the interface being ready
  while(!Serial);
  // output format
  Serial.println("VOC_Index\tRH\tT");
  // init I2C
  Wire.setClock(100000L);//修改I2C时钟 据说模块最多100K
  Wire.begin();
  // wait until sensors startup, &gt; 1 ms according to datasheet
  delay(1);
  // start up sensor, sensor will go to continous measurement mode
  // each second there will be new measurement values
  Wire.beginTransmission(SVM40_ADDRESS);
  Wire.write(0x00);
  Wire.write(0x10);
  Wire.endTransmission();
  // wait until sensors is ready, fan is initialized
  delay(1000);
}
void loop() {
  uint16_t voc, humidity, temperature;
  uint8_t data, counter;
  // read measurement data
  Wire.beginTransmission(SVM40_ADDRESS);
  Wire.write(0x03);
  Wire.write(0xA6);
  Wire.endTransmission();
  // wait 5 ms to allow the sensor to fill the internal buffer
  delay(5);
  // read measurement data svm40, after two bytes a CRC follows
  Wire.requestFrom(SVM40_ADDRESS, 9);
  counter = 0;
  while (Wire.available()) {
    data = Wire.read();
  }
  // VOC level is a signed int and scaled by a factor of 10 and needs to be divided by 10
  // humidity is a signed int and scaled by 100 and need to be divided by 100
  // temperature is a signed int and scaled by 200 and need to be divided by 200
  voc = (uint16_t)data &lt;&lt; 8 | data;
  humidity = (uint16_t)data &lt;&lt; 8 | data;
  temperature = (uint16_t)data &lt;&lt; 8 | data;
  Serial.print(String(float(voc) / 10));
  Serial.print("\t");
  Serial.print(String(float(humidity) / 100));
  Serial.print("\t");
  Serial.print(String(float(temperature) / 200));
  Serial.println();
  delay(1000);
}</code></pre>

<p>8,编译下载。 运行。</p>

<p>结果出来了,今晚24.8度&nbsp;</p>

<p></p>

<p>&nbsp;</p>

Jacktang 发表于 2021-3-2 14:04

<p>&nbsp;还可以这样玩,用stm32duino安装包</p>

<p>谢谢分享</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

damiaa 发表于 2021-3-2 14:43

Jacktang 发表于 2021-3-2 14:04
&nbsp;还可以这样玩,用stm32duino安装包

谢谢分享

&nbsp;

&nbsp;

&nbsp;

<p>是啊挺方便的。手上的板子可以玩起来。</p>

w494143467 发表于 2021-3-3 19:40

<p>这个空中飞电阻的操作哈哈,有那么点意思,以前也用过这样的方式!</p>
页: [1]
查看完整版本: 【测评SGP40】+用arduino测试传感器 I2C通信