Skip to content

Commit 2473eca

Browse files
authored
Add sfm3x00 example
* Add minimal example for SFM3x00 familiy * clarify MODEL selection enum * add README describing Model configuration
1 parent e5488bd commit 2473eca

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

SFM3x00_I2C_minimal_example/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# SFM3x00 Minimal Example
2+
3+
## Configuration
4+
5+
Please note that the model selection is done manually, line 55 of [SFM3x00_minimal_example.ino](SFM3x00_minimal_example.ino#55)
6+
7+
```c++
8+
54: // ACTION: select your component here from the enum above:
9+
55: const uint8_t MODEL = SFM3200;
10+
```
11+
12+
The available sensors can be found on line 36 of [SFM3x00_minimal_example.ino](SFM3x00_minimal_example.ino#36)
13+
14+
```c++
15+
35: // supported sensors
16+
36: enum SFM_MODEL {
17+
37: SFM3000 = 0,
18+
38: SFM3200,
19+
39: SFM3300,
20+
40: SFM3400,
21+
41: SFM_MODEL_LENGTH //< Note: this is not a valid value for 'MODEL' below
22+
42: };
23+
```
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2021, 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+
// supported sensors
36+
enum SFM_MODEL {
37+
SFM3000 = 0,
38+
SFM3200,
39+
SFM3300,
40+
SFM3400,
41+
SFM_MODEL_LENGTH //< Note: this is not a valid value for 'MODEL' below
42+
};
43+
44+
// from the datasheets:
45+
const int16_t SFM3X00_ADDRESS = 0x40;
46+
47+
const int16_t FLOW_OFFSET[SFM_MODEL_LENGTH] = { 32000, 32768, 32768, 32768 };
48+
const int16_t SCALE_FACTOR_AIR[SFM_MODEL_LENGTH] = { 140, 140, 120, 800 };
49+
50+
const byte CMD_START_MEASUREMENT[] = { 0x10, 0x00 };
51+
const byte CMD_SOFT_RESET[] = { 0x20, 0x00 };
52+
53+
54+
// ACTION: select your component here from the enum above:
55+
const uint8_t MODEL = SFM3200;
56+
57+
void setup()
58+
{
59+
Serial.begin(115200);
60+
while(!Serial);
61+
62+
Wire.begin();
63+
64+
/*
65+
// optional reset before use
66+
Wire.beginTransmission(SFM3X00_ADDRESS);
67+
Wire.write(CMD_SOFT_RESET, 2);
68+
Wire.endTransmission();
69+
delay(100);
70+
*/
71+
72+
Wire.beginTransmission(SFM3X00_ADDRESS);
73+
Wire.write(CMD_START_MEASUREMENT, 2);
74+
Wire.endTransmission();
75+
76+
delay(100);
77+
Serial.println("Flow rate [slm]");
78+
}
79+
80+
void loop()
81+
{
82+
Wire.requestFrom(SFM3X00_ADDRESS, 3);
83+
if (Wire.available() < 3) {
84+
Serial.println("No data received from sensor");
85+
} else {
86+
int16_t flow;
87+
flow = (int16_t)Wire.read() << 8;
88+
flow |= Wire.read();
89+
// CRC verification (third byte) left as an exercise for the reader
90+
91+
flow = (flow - FLOW_OFFSET[MODEL]) / SCALE_FACTOR_AIR[MODEL];
92+
Serial.println(flow);
93+
}
94+
95+
delay(100);
96+
}

0 commit comments

Comments
 (0)