1
+ /*
2
+ Using the BMV080 Particulate Matter PM2.5 Sensor
3
+
4
+ This Example shows how to read and set the parameters of the BMV080 sensor.
5
+
6
+ These include:
7
+ volumetric_mass_density
8
+ integration_time
9
+ distribution_id
10
+ do_obstruction_detection
11
+ do_vibration_filtering
12
+
13
+ After these parameters are read and set, This example shows how to use the
14
+ sensor in "continuous mode" to get particulate matter readings once every
15
+ second.
16
+
17
+ It uses polling of the device to check if new data is available.
18
+
19
+ By: Pete Lewis
20
+ SparkFun Electronics
21
+ Date: September, 2024
22
+ SparkFun code, firmware, and software is released under the MIT License.
23
+ Please see LICENSE.md for further details.
24
+
25
+ Hardware Connections:
26
+ IoT RedBoard --> BMV080
27
+ QWIIC --> QWIIC
28
+
29
+ BMV080 "mode" jumper set to I2C (default)
30
+
31
+ Serial.print it out at 115200 baud to serial monitor.
32
+
33
+ Feel like supporting our work? Buy a board from SparkFun!
34
+ https://www.sparkfun.com/products/?????
35
+ */
36
+
37
+ #include < Wire.h>
38
+ #include " SparkFun_BMV080_Arduino_Library.h" // CTRL+Click here to get the library: http://librarymanager/All#SparkFun_BMV080
39
+
40
+ Bmv080 bmv080; // Create an instance of the BMV080 class
41
+ #define BMV080_ADDR 0x57 // SparkFun BMV080 Breakout defaults to 0x57
42
+
43
+ i2c_device_t i2c_device = {}; // I2C device struct instance for Bosch API
44
+
45
+ void setup ()
46
+ {
47
+ Serial.begin (115200 );
48
+
49
+ while (!Serial) delay (10 ); // Wait for Serial to become available.
50
+ // Necessary for boards with native USB (like the SAMD51 Thing+).
51
+ // For a final version of a project that does not need serial debug (or a USB cable plugged in),
52
+ // Comment out this while loop, or it will prevent the remaining code from running.
53
+
54
+ Serial.println ();
55
+ Serial.println (" BMV080 Example 5 - Get and Set Parameters" );
56
+
57
+ Wire.begin ();
58
+
59
+ if (bmv080.begin (BMV080_ADDR, Wire) == false ) {
60
+ Serial.println (" BMV080 not detected at default I2C address. Check your jumpers and the hookup guide. Freezing..." );
61
+ while (1 )
62
+ ;
63
+ }
64
+ Serial.println (" BMV080 found!" );
65
+
66
+ // Wire.setClock(400000); //Increase I2C data rate to 400kHz
67
+
68
+ /* Communication interface initialization */
69
+ i2c_init (&i2c_device);
70
+
71
+ /* Initialize the Sensor (read driver, open, reset, id etc.)*/
72
+ bmv080.init (&i2c_device);
73
+
74
+ getSetParameters (); // Get and set parameters
75
+
76
+ /* Set the sensor mode to continuous mode */
77
+ if (bmv080.setMode (SFE_BMV080_MODE_CONTINUOUS) == true )
78
+ {
79
+ Serial.println (" BMV080 set to continuous mode" );
80
+ }
81
+ else
82
+ {
83
+ Serial.println (" Error setting BMV080 mode" );
84
+ }
85
+ }
86
+
87
+ void loop ()
88
+ {
89
+ if (bmv080.dataAvailable ())
90
+ {
91
+ float pm25 = bmv080.getPM25 ();
92
+
93
+ Serial.print (pm25);
94
+
95
+ if (bmv080.getIsObstructed () == true )
96
+ {
97
+ Serial.print (" \t Obstructed" );
98
+ }
99
+
100
+ Serial.println ();
101
+ }
102
+ delay (100 );
103
+ }
104
+
105
+ void getSetParameters (void )
106
+ {
107
+ // Variables to store the parameters
108
+ // Note, for custom settings, you will need to set these variables before
109
+ // calling the set functions (down below).
110
+
111
+ float volumetric_mass_density = 0 .0f ;
112
+ float integration_time = 0 .0f ;
113
+ uint32_t distribution_id = 0 ;
114
+ bool do_obstruction_detection = false ;
115
+ bool do_vibration_filtering = false ;
116
+
117
+ /* **************************************************************************
118
+ *
119
+ * Reading configuration parameters
120
+ *
121
+ * *************************************************************************/
122
+
123
+
124
+ /* Get default parameter "volumetric_mass_density" */
125
+ volumetric_mass_density = bmv080.getVolumetricMassDensity ();
126
+ Serial.print (" BMV080 parameter 'volumetric_mass_density' READ: " );
127
+ Serial.println (volumetric_mass_density);
128
+
129
+ /* Get default parameter "integration_time" */
130
+ integration_time = bmv080.getIntegrationTime ();
131
+ Serial.print (" BMV080 parameter 'integration_time' READ: " );
132
+ Serial.println (integration_time);
133
+
134
+ /* Get default parameter "distribution_id" */
135
+ distribution_id = bmv080.getDistributionId ();
136
+ Serial.print (" BMV080 parameter 'distribution_id' READ: " );
137
+ Serial.println (distribution_id);
138
+
139
+ /* Get default parameter "do_obstruction_detection" */
140
+ do_obstruction_detection = bmv080.getDoObstructionDetection ();
141
+ Serial.print (" BMV080 parameter 'do_obstruction_detection' READ: " );
142
+ if (do_obstruction_detection == true )
143
+ {
144
+ Serial.println (" true" );
145
+ }
146
+ else
147
+ {
148
+ Serial.println (" false" );
149
+ }
150
+
151
+ /* Get default parameter "do_vibration_filtering" */
152
+ do_vibration_filtering = bmv080.getDoVibrationFiltering ();
153
+ Serial.print (" BMV080 parameter 'do_vibration_filtering' READ: " );
154
+ if (do_vibration_filtering == true )
155
+ {
156
+ Serial.println (" true" );
157
+ }
158
+ else
159
+ {
160
+ Serial.println (" false" );
161
+ }
162
+
163
+
164
+ /* **************************************************************************
165
+ *
166
+ * Setting (custom) configuration parameters
167
+ *
168
+ * *************************************************************************/
169
+
170
+ // Uncomment the following lines to set the parameters to custom values
171
+
172
+ // volumetric_mass_density = 1.6f;
173
+ // integration_time = 10.0f;
174
+ // distribution_id = 3;
175
+ // do_obstruction_detection = true;
176
+ // do_vibration_filtering = false;
177
+
178
+ /* Set custom parameter "volumetric_mass_density" */
179
+ if (bmv080.setVolumetricMassDensity (volumetric_mass_density) == true )
180
+ {
181
+ Serial.print (" BMV080 parameter 'volumetric_mass_density' SET TO: " );
182
+ Serial.println (volumetric_mass_density);
183
+ }
184
+ else
185
+ {
186
+ Serial.println (" Error setting BMV080 parameter 'volumetric_mass_density'" );
187
+ }
188
+
189
+ /* Set custom parameter "integration_time" */
190
+ if (bmv080.setIntegrationTime (integration_time) == true )
191
+ {
192
+ Serial.print (" BMV080 parameter 'integration_time' SET TO: " );
193
+ Serial.println (integration_time);
194
+ }
195
+ else
196
+ {
197
+ Serial.println (" Error setting BMV080 parameter 'integration_time'" );
198
+ }
199
+
200
+ /* Set custom parameter "distribution_id" */
201
+ if (bmv080.setDistributionId (distribution_id) == true )
202
+ {
203
+ Serial.print (" BMV080 parameter 'distribution_id' SET TO: " );
204
+ Serial.println (distribution_id);
205
+ }
206
+ else
207
+ {
208
+ Serial.println (" Error setting BMV080 parameter 'distribution_id'" );
209
+ }
210
+
211
+ /* Set custom parameter "do_obstruction_detection" */
212
+ if (bmv080.setDoObstructionDetection (do_obstruction_detection) == true )
213
+ {
214
+ Serial.print (" BMV080 parameter 'do_obstruction_detection' SET TO: " );
215
+ if (do_obstruction_detection == true )
216
+ {
217
+ Serial.println (" true" );
218
+ }
219
+ else
220
+ {
221
+ Serial.println (" false" );
222
+ }
223
+ }
224
+ else
225
+ {
226
+ Serial.println (" Error setting BMV080 parameter 'do_obstruction_detection'" );
227
+ }
228
+
229
+ /* Set custom parameter "do_vibration_filtering" */
230
+ if (bmv080.setDoVibrationFiltering (do_vibration_filtering) == true )
231
+ {
232
+ Serial.print (" BMV080 parameter 'do_vibration_filtering' SET TO: " );
233
+ if (do_vibration_filtering == true )
234
+ {
235
+ Serial.println (" true" );
236
+ }
237
+ else
238
+ {
239
+ Serial.println (" false" );
240
+ }
241
+ }
242
+ else
243
+ {
244
+ Serial.println (" Error setting BMV080 parameter 'do_vibration_filtering'" );
245
+ }
246
+
247
+ Serial.println ();
248
+ }
0 commit comments