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