气体流量传感器 AFM3000/SFM3000 驱动

之前买入了奥松电子的气体流量传感器,型号为 AFM3000。到手后发现没有 Arduino 的驱动代码,调试不了。

离谱的是,我在 Arduino 论坛逛的时候,发现了这个问题:I2C communication with SFM3000 series flow sensor from Sensirion。问题中用到的气体流量传感器为 SFM3000,看了说明书后,只能说两款传感器基本一样。

于是我又去 git 上找了找,看是不是有现成的驱动,结果还真有 Sensirion I2C SFM3000 Arduino Library。之后在 Arduino IDE 的 Library 中搜索「Sensirion I2C SFM3000」下载即可。

这是 AFM3000/SFM3000 的驱动代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* I2C-Generator: 0.2.0
* Yaml Version: 0.1.0
* Template Version: 0.7.0-38-g217adaf
*/
/*
* Copyright (c) 2021, Sensirion AG
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Sensirion AG nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <Arduino.h>
#include <SensirionI2CSfm3000.h>
#include <Wire.h>

SensirionI2CSfm3000 sfm;


float scalingFactor = 140.0;
float offset = 32000;


void setup() {

Serial.begin(115200);
while (!Serial) {
delay(100);
}

Wire.begin();

uint16_t error;
char errorMessage[64];

sfm.begin(Wire, SFM300_I2C_ADDRESS_0);

uint32_t serialNumber;

error = sfm.readSerialNumber(serialNumber);
if (error) {
Serial.print("Error trying to execute serialNumber(): ");
errorToString(error, errorMessage, 64);
Serial.println(errorMessage);
} else {
Serial.print("SerialNumber:");
Serial.print(serialNumber);
Serial.println();
}

error = sfm.startContinuousMeasurement();

if (error) {
Serial.print("Error trying to execute startContinuousMeasurement(): ");
errorToString(error, errorMessage, 64);
Serial.println(errorMessage);
}
}

void loop() {
uint16_t error;
char errorMessage[64];

delay(100);

// Read Measurement
float flow;

error = sfm.readMeasurement(flow, scalingFactor, offset);

if (error) {
Serial.print("Error trying to execute readMeasurement(): ");
errorToString(error, errorMessage, 64);
Serial.println(errorMessage);
} else {
Serial.print("Flow:");
Serial.print(flow);
Serial.println();
}
}

烧录到 Arduino 即可。


气体流量传感器 AFM3000/SFM3000 驱动
https://wonderhoi.com/2023/08/03/气体流量传感器-AFM3000-SFM3000-驱动/
作者
wonderhoi
发布于
2023年8月3日
许可协议