[Review of SGP40] + Testing the sensor I2C communication with Arduino
[Copy link]
This post was last edited by damiaa on 2021-3-2 08:43
[Review of SGP40] + Testing the sensor I2C communication with Arduino
It was easy to test SVM40 I2C communication with Arduino, but it took some time.
1. Download the arduino-snippets-main package and unzip it.
2. Use the example in \arduino-snippets-main\arduino-snippets-main\SVM40_I2C_minimal_example.
3. Prepare to connect the circuit, connect the sensor board VCC GND to the stm32F767 board VCC ground, 3-pin SDA to PB8, 4-pin SCL PB9, 5-pin to ground
4. Connect 4.7K pull-up resistors to PB8 and PB9. Remember, otherwise the data will remain unchanged.
5,打开\arduino-snippets-main\arduino-snippets-main\SVM40_I2C_minimal_example
6. Open SVM40_I2C_minimal_example.ino with arduino1.8.13
Note that the stm32duino package has been installed here. For details, please see my post Let Arduino play with your useless stm32 board.
7. Change the I2C clock to 100K . The details are as follows
#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. Compile and download. Run.
The results are out, 24.8 degrees tonight
|