|
| 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 | +#include <Wire.h> |
| 33 | + |
| 34 | +// SEN44 |
| 35 | +const int16_t SEN44_ADDRESS = 0x69; |
| 36 | +// SCD40 |
| 37 | +const int16_t SCD_ADDRESS = 0x62; |
| 38 | + |
| 39 | +void setup() { |
| 40 | + Serial.begin(115200); |
| 41 | + // wait for serial connection from PC |
| 42 | + // comment the following line if you'd like the output |
| 43 | + // without waiting for the interface being ready |
| 44 | + while(!Serial); |
| 45 | + |
| 46 | + // output format |
| 47 | + Serial.println("CO2\tRH_SCD\tT_SCD\tPM1.0\tPM2.5\tPM4.0\tPM10.0\tVOC_Index\tRH\tT"); |
| 48 | + |
| 49 | + // init I2C |
| 50 | + Wire.begin(); |
| 51 | + |
| 52 | + // wait until sensors startup, > 1 ms according to datasheet |
| 53 | + delay(1); |
| 54 | + |
| 55 | + // start scd measurement in periodic mode, will update every 2 s |
| 56 | + Wire.beginTransmission(SCD_ADDRESS); |
| 57 | + Wire.write(0x21); |
| 58 | + Wire.write(0xb1); |
| 59 | + Wire.endTransmission(); |
| 60 | + |
| 61 | + // start up sensor, sensor will go to continous measurement mode |
| 62 | + // each second there will be new measurement values |
| 63 | + Wire.beginTransmission(SEN44_ADDRESS); |
| 64 | + Wire.write(0x00); |
| 65 | + Wire.write(0x21); |
| 66 | + Wire.endTransmission(); |
| 67 | + |
| 68 | + // wait until sensors is ready, fan is initialized |
| 69 | + delay(2000); |
| 70 | +} |
| 71 | + |
| 72 | +void loop() { |
| 73 | + uint16_t pm1p0, pm2p5, pm4p0, pm10p0; |
| 74 | + int16_t voc, humidity, temperature; |
| 75 | + float co2, temp_scd, rh_scd; |
| 76 | + uint8_t data[21], counter; |
| 77 | + |
| 78 | + // read measurement data SCD |
| 79 | + Wire.beginTransmission(SCD_ADDRESS); |
| 80 | + Wire.write(0xec); |
| 81 | + Wire.write(0x05); |
| 82 | + Wire.endTransmission(); |
| 83 | + |
| 84 | + // read measurement data: 2 bytes co2, 1 byte CRC, |
| 85 | + // 2 bytes T, 1 byte CRC, 2 bytes RH, 1 byte CRC, |
| 86 | + // 2 bytes sensor status, 1 byte CRC |
| 87 | + // stop reading after 12 bytes (not used) |
| 88 | + // other data like ASC not included |
| 89 | + Wire.requestFrom(SCD_ADDRESS, 12); |
| 90 | + counter = 0; |
| 91 | + while (Wire.available()) { |
| 92 | + data[counter++] = Wire.read(); |
| 93 | + } |
| 94 | + |
| 95 | + // floating point conversion according to datasheet |
| 96 | + co2 = (float)((uint16_t)data[0] << 8 | data[1]); |
| 97 | + // convert T in degC |
| 98 | + temp_scd = -45 + 175 * (float)((uint16_t)data[3] << 8 | data[4]) / 65536; |
| 99 | + // convert RH in % |
| 100 | + rh_scd = 100 * (float)((uint16_t)data[6] << 8 | data[7]) / 65536; |
| 101 | + |
| 102 | + Serial.print(co2); |
| 103 | + Serial.print("\t"); |
| 104 | + Serial.print(rh_scd); |
| 105 | + Serial.print("\t"); |
| 106 | + Serial.print(temp_scd); |
| 107 | + Serial.print("\t"); |
| 108 | + |
| 109 | + // send read measurement data command |
| 110 | + Wire.beginTransmission(SEN44_ADDRESS); |
| 111 | + Wire.write(0x03); |
| 112 | + Wire.write(0x74); |
| 113 | + Wire.endTransmission(); |
| 114 | + |
| 115 | + // wait 5 ms to allow the sensor to fill the internal buffer |
| 116 | + delay(5); |
| 117 | + |
| 118 | + // read measurement data SEN44, after two bytes a CRC follows |
| 119 | + Wire.requestFrom(SEN44_ADDRESS, 21); |
| 120 | + counter = 0; |
| 121 | + while (Wire.available()) { |
| 122 | + data[counter++] = Wire.read(); |
| 123 | + } |
| 124 | + |
| 125 | + // PM1.0 to PM10 are unscaled unsigned integer values in ug / um3 |
| 126 | + // VOC level is a signed int and scaled by a factor of 10 and needs to be divided by 10 |
| 127 | + // humidity is a signed int and scaled by 100 and need to be divided by 100 |
| 128 | + // temperature is a signed int and scaled by 200 and need to be divided by 200 |
| 129 | + pm1p0 = (uint16_t)data[0] << 8 | data[1]; |
| 130 | + pm2p5 = (uint16_t)data[3] << 8 | data[4]; |
| 131 | + pm4p0 = (uint16_t)data[6] << 8 | data[7]; |
| 132 | + pm10p0 = (uint16_t)data[9] << 8 | data[10]; |
| 133 | + voc = (uint16_t)data[12] << 8 | data[13]; |
| 134 | + humidity = (uint16_t)data[15] << 8 | data[16]; |
| 135 | + temperature = (uint16_t)data[18] << 8 | data[19]; |
| 136 | + |
| 137 | + Serial.print(pm1p0); |
| 138 | + Serial.print("\t"); |
| 139 | + Serial.print(pm2p5); |
| 140 | + Serial.print("\t"); |
| 141 | + Serial.print(pm4p0); |
| 142 | + Serial.print("\t"); |
| 143 | + Serial.print(pm10p0); |
| 144 | + Serial.print("\t"); |
| 145 | + Serial.print(String(float(voc) / 10)); |
| 146 | + Serial.print("\t"); |
| 147 | + Serial.print(String(float(humidity) / 100)); |
| 148 | + Serial.print("\t"); |
| 149 | + Serial.print(String(float(temperature) / 200)); |
| 150 | + Serial.println(); |
| 151 | + |
| 152 | + // wait 2 s for next measurement, due to sampling frequency of SCD |
| 153 | + delay(2000); |
| 154 | +} |
0 commit comments