-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSFM3300.ino
114 lines (102 loc) · 3.01 KB
/
SFM3300.ino
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
105
106
107
108
109
110
111
112
113
114
/*
Sensirion SFM3300 Flow Sensor interfacing
https://www.nabilbd.com
https://www.cruxbd.com
Credit:
Modified From: https://www.14core.com/wiring-sfm3000-air-gas-flow-meter/
*/
#include "SFM3300.h"
SFM3300::SFM3300(int i2cAddress)
{
mI2cAddress = i2cAddress;
}
void SFM3300::init()
{
int a = 0;
int b = 0;
int c = 0;
Wire.beginTransmission(byte(mI2cAddress)); // transmit to device with I2C mI2cAddress
Wire.beginTransmission(byte(mI2cAddress));
Wire.write(byte(0x10));
Wire.write(byte(0x00));
Wire.endTransmission();
delay(5);
// Wire.beginTransmission(byte(64));
// Wire.write(0x10);
// Wire.write(0x00);
// Wire.endTransmission();
// delay(5);
Wire.requestFrom(mI2cAddress, 3); //
a = Wire.read(); // received first byte stored here
b = Wire.read(); // received second byte stored here
c = Wire.read(); // received third byte stored here
Wire.endTransmission();
delay(5);
}
float SFM3300::getvalue()
{
#ifdef flowWithTemperature
Wire.beginTransmission(byte(mI2cAddress));
Wire.write(byte(0x10));
Wire.write(byte(0x00));
Wire.endTransmission();
#endif
Wire.requestFrom(mI2cAddress, 3); // set read 3 bytes from device with address 0x40
uint16_t a = Wire.read(); // received first byte stored here. The variable "uint16_t" can hold 2 bytes, this will be relevant later
uint8_t b = Wire.read(); // second received byte stored here
a = (a << 8) | b; // combine the two received bytes to a 16bit integer value
int Flow = a;
return Flow;
}
void SFM3300::softReset()
{
int ret;
do {
// Soft reset the sensor
Wire.beginTransmission(byte(mI2cAddress));
Wire.write(0x20);
Wire.write(0x00);
ret = Wire.endTransmission();
if (ret != 0) {
Serial.println("Error while sending soft reset command, retrying...");
delay(500); // wait long enough for chip reset to complete
}
} while (ret != 0);
if (ret == 0) {
Serial.println("Soft Reset Done");
}
}
void SFM3300::hardReset(uint8_t sensorPowerPin)
{ digitalWrite(sensorPowerPin, LOW);
delay(500);
digitalWrite(sensorPowerPin, HIGH);
delay(500);
int ret;
do {
// Soft reset the sensor
Wire.beginTransmission(byte(mI2cAddress));
Wire.write(0x20);
Wire.write(0x00);
ret = Wire.endTransmission();
if (ret != 0) {
Serial.println("Error while sending soft reset command, retrying...");
delay(500); // wait long enough for chip reset to complete
}
} while (ret != 0);
if (ret == 0) {
Serial.println("Hard Reset Done");
}
}
float SFM3300::tempRead() {
Wire.beginTransmission(byte(mI2cAddress));
Wire.write(0x10);
Wire.write(0x01);
Wire.endTransmission();
delay(5);
Wire.requestFrom(mI2cAddress, 3);
uint16_t x = Wire.read(); // received first byte stored here. The variable "uint16_t" can hold 2 bytes, this will be relevant later
uint16_t y = Wire.read();
x = (x << 8) | y;
int temp = x;
return temp;
}