|
| 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 | + |
| 37 | +void setup() { |
| 38 | + Serial.begin(115200); |
| 39 | + // wait for serial connection from PC |
| 40 | + // comment the following line if you'd like the output |
| 41 | + // without waiting for the interface being ready |
| 42 | + while(!Serial); |
| 43 | + |
| 44 | + // output format |
| 45 | + Serial.println("PM1.0\tPM2.5\tPM4.0\tPM10.0\tVOC_Index\tRH\tT"); |
| 46 | + |
| 47 | + // init I2C |
| 48 | + Wire.begin(); |
| 49 | + |
| 50 | + // wait until sensors startup, > 1 ms according to datasheet |
| 51 | + delay(1); |
| 52 | + |
| 53 | + // start up sensor, sensor will go to continuous measurement mode |
| 54 | + // each second there will be new measurement values |
| 55 | + Wire.beginTransmission(SEN44_ADDRESS); |
| 56 | + Wire.write(0x00); |
| 57 | + Wire.write(0x21); |
| 58 | + Wire.endTransmission(); |
| 59 | + |
| 60 | + // wait until sensor is ready, fan is initialized |
| 61 | + delay(1000); |
| 62 | +} |
| 63 | + |
| 64 | +void loop() { |
| 65 | + |
| 66 | + uint16_t pm1p0, pm2p5, pm4p0, pm10p0; |
| 67 | + int16_t voc, humidity, temperature; |
| 68 | + uint8_t data[21], counter; |
| 69 | + |
| 70 | + // send read measurement data command |
| 71 | + Wire.beginTransmission(SEN44_ADDRESS); |
| 72 | + Wire.write(0x03); |
| 73 | + Wire.write(0x74); |
| 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 SEN44, after two bytes a CRC follows |
| 80 | + Wire.requestFrom(SEN44_ADDRESS, 21); |
| 81 | + counter = 0; |
| 82 | + while (Wire.available()) { |
| 83 | + data[counter++] = Wire.read(); |
| 84 | + } |
| 85 | + |
| 86 | + // PM1.0 to PM10 are unscaled unsigned integer values in ug / um3 |
| 87 | + // VOC level is a signed int and scaled by a factor of 10 and needs to be divided by 10 |
| 88 | + // humidity is a signed int and scaled by 100 and need to be divided by 100 |
| 89 | + // temperature is a signed int and scaled by 200 and need to be divided by 200 |
| 90 | + pm1p0 = (uint16_t)data[0] << 8 | data[1]; |
| 91 | + pm2p5 = (uint16_t)data[3] << 8 | data[4]; |
| 92 | + pm4p0 = (uint16_t)data[6] << 8 | data[7]; |
| 93 | + pm10p0 = (uint16_t)data[9] << 8 | data[10]; |
| 94 | + voc = (uint16_t)data[12] << 8 | data[13]; |
| 95 | + humidity = (uint16_t)data[15] << 8 | data[16]; |
| 96 | + temperature = (uint16_t)data[18] << 8 | data[19]; |
| 97 | + |
| 98 | + Serial.print(pm1p0); |
| 99 | + Serial.print("\t"); |
| 100 | + Serial.print(pm2p5); |
| 101 | + Serial.print("\t"); |
| 102 | + Serial.print(pm4p0); |
| 103 | + Serial.print("\t"); |
| 104 | + Serial.print(pm10p0); |
| 105 | + Serial.print("\t"); |
| 106 | + Serial.print(String(float(voc) / 10)); |
| 107 | + Serial.print("\t"); |
| 108 | + Serial.print(String(float(humidity) / 100)); |
| 109 | + Serial.print("\t"); |
| 110 | + Serial.print(String(float(temperature) / 200)); |
| 111 | + Serial.println(); |
| 112 | + |
| 113 | + // wait 1 s for next measurement |
| 114 | + delay(1000); |
| 115 | +} |
0 commit comments