|
| 1 | +/* |
| 2 | +* Copyright (c) 2020, Sensirion AG |
| 3 | +* All rights reserved. |
| 4 | +* |
| 5 | +* Redistribution and use in source and binary forms, with or without |
| 6 | +* modification, are permitted provided that the following conditions are met: |
| 7 | +* |
| 8 | +* * Redistributions of source code must retain the above copyright notice, this |
| 9 | +* list of conditions and the following disclaimer. |
| 10 | +* |
| 11 | +* * Redistributions in binary form must reproduce the above copyright notice, |
| 12 | +* this list of conditions and the following disclaimer in the documentation |
| 13 | +* and/or other materials provided with the distribution. |
| 14 | +* |
| 15 | +* * Neither the name of Sensirion AG nor the names of its |
| 16 | +* contributors may be used to endorse or promote products derived from |
| 17 | +* this software without specific prior written permission. |
| 18 | +* |
| 19 | +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | +* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 23 | +* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | +* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | +* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | +* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | +* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | +* POSSIBILITY OF SUCH DAMAGE. |
| 30 | +
|
| 31 | +*/ |
| 32 | + |
| 33 | +#include <Wire.h> |
| 34 | + |
| 35 | +// SVM40 |
| 36 | +const int16_t SVM40_ADDRESS = 0x6A; |
| 37 | + |
| 38 | +void setup() { |
| 39 | + Serial.begin(115200); |
| 40 | + // wait for serial connection from PC |
| 41 | + // comment the following line if you'd like the output |
| 42 | + // without waiting for the interface being ready |
| 43 | + while(!Serial); |
| 44 | + |
| 45 | + // output format |
| 46 | + Serial.println("VOC_Index\tRH\tT"); |
| 47 | + |
| 48 | + // init I2C |
| 49 | + Wire.begin(); |
| 50 | + |
| 51 | + // wait until sensors startup, > 1 ms according to datasheet |
| 52 | + delay(1); |
| 53 | + |
| 54 | + // start up sensor, sensor will go to continous measurement mode |
| 55 | + // each second there will be new measurement values |
| 56 | + Wire.beginTransmission(SVM40_ADDRESS); |
| 57 | + Wire.write(0x00); |
| 58 | + Wire.write(0x10); |
| 59 | + Wire.endTransmission(); |
| 60 | + |
| 61 | + // wait until sensors is ready, fan is initialized |
| 62 | + delay(1000); |
| 63 | +} |
| 64 | + |
| 65 | +void loop() { |
| 66 | + |
| 67 | + int16_t voc, humidity, temperature; |
| 68 | + uint8_t data[9], counter; |
| 69 | + |
| 70 | + // read measurement data |
| 71 | + Wire.beginTransmission(SVM40_ADDRESS); |
| 72 | + Wire.write(0x03); |
| 73 | + Wire.write(0xA6); |
| 74 | + Wire.endTransmission(); |
| 75 | + |
| 76 | + // wait 5 ms to allow the sensor to fill the internal buffer |
| 77 | + delay(5); |
| 78 | + |
| 79 | + // read measurement data svm40, after two bytes a CRC follows |
| 80 | + Wire.requestFrom(SVM40_ADDRESS, 9); |
| 81 | + counter = 0; |
| 82 | + while (Wire.available()) { |
| 83 | + data[counter++] = Wire.read(); |
| 84 | + } |
| 85 | + |
| 86 | + // VOC level is a signed int and scaled by a factor of 10 and needs to be divided by 10 |
| 87 | + // humidity is a signed int and scaled by 100 and need to be divided by 100 |
| 88 | + // temperature is a signed int and scaled by 200 and need to be divided by 200 |
| 89 | + voc = (uint16_t)data[0] << 8 | data[1]; |
| 90 | + humidity = (uint16_t)data[3] << 8 | data[4]; |
| 91 | + temperature = (uint16_t)data[6] << 8 | data[7]; |
| 92 | + |
| 93 | + Serial.print(String(float(voc) / 10)); |
| 94 | + Serial.print("\t"); |
| 95 | + Serial.print(String(float(humidity) / 100)); |
| 96 | + Serial.print("\t"); |
| 97 | + Serial.print(String(float(temperature) / 200)); |
| 98 | + Serial.println(); |
| 99 | + |
| 100 | + // wait 1 s for next measurement |
| 101 | + delay(1000); |
| 102 | +} |
0 commit comments