【测评SGP40】+用arduino测试传感器 I2C通信
[复制链接]
本帖最后由 damiaa 于 2021-3-2 08:43 编辑
【测评SGP40】+用arduino测试传感器 I2C通信
本来用arduino测试SVM40 I2C通讯是一个容易的事。但折腾了一下。花了些时间。
1,下载arduino-snippets-main包 解压。
2,使用里面的\arduino-snippets-main\arduino-snippets-main\SVM40_I2C_minimal_example例子。
3,准备接好电路,传感器板 VCC GND接stm32F767板子 VCC 地, 3腿 SDA 接 PB8, 4腿SCL PB9,5腿 接地
4,PB8,PB9接4.7K上拉电阻。记得 否则是数据不变哦
5,打开\arduino-snippets-main\arduino-snippets-main\SVM40_I2C_minimal_example
6,用arduino1.8.13 打开SVM40_I2C_minimal_example.ino
注意这里已经是安装好stm32duino包了得。具体可以看我的帖子 让arduino 把你的无用的stm32板子玩起来 之一
7,修改I2C时钟为100K。具体如下
#include <Wire.h>
// 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, > 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[9], 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[counter++] = 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[0] << 8 | data[1];
humidity = (uint16_t)data[3] << 8 | data[4];
temperature = (uint16_t)data[6] << 8 | data[7];
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);
}
8,编译下载。 运行。
结果出来了,今晚24.8度
|