Skip to content

Commit 61e099d

Browse files
committed
duty cycle example
1 parent 9ed57f8 commit 61e099d

File tree

4 files changed

+282
-20
lines changed

4 files changed

+282
-20
lines changed

examples/Example_01_BasicReadings/Example_01_BasicReadings.ino

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
This example shows how to use the sensor in "continuous mode" to get
55
particulate matter readings once every second.
66
7-
When the sensor is ready to report new data, it will trigger an interrupt
8-
with the IRQ line going low.
7+
It uses polling of the device to check if new data is available.
98
109
By: Pete Lewis
1110
SparkFun Electronics
@@ -16,7 +15,6 @@
1615
Hardware Connections:
1716
IoT RedBoard --> BMV080
1817
QWIIC --> QWIIC
19-
14 --> IRQ
2018
2119
BMV080 "mode" jumper set to I2C (default)
2220
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
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("\tObstructed");
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+
}

src/sfeBmv080.cpp

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,12 @@ void use_sensor_output(bmv080_output_t bmv080_output, void* callback_parameters)
5656

5757
void bmv080_service_routine(const bmv080_handle_t handle, void* callback_parameters)
5858
{
59-
// if ( (bmv080_handle != NULL) && (print_handle != NULL))
60-
// {
61-
//Serial.print("s");
62-
/* The interrupt is served by the BMV080 sensor driver */
63-
bmv080_status_code_t bmv080_current_status = bmv080_serve_interrupt(handle, (bmv080_callback_data_ready_t)use_sensor_output, callback_parameters);
64-
//bmv080_status_code_t bmv080_current_status = bmv080_serve_interrupt(bmv080_handle, (bmv080_callback_data_ready_t)use_sensor_output, (void*)print_handle);
65-
if (bmv080_current_status != E_BMV080_OK)
66-
{
67-
//printf("e %d\r\n", (int32_t)bmv080_current_status);
68-
}
69-
//}
59+
/* The interrupt is served by the BMV080 sensor driver */
60+
bmv080_status_code_t bmv080_current_status = bmv080_serve_interrupt(handle, (bmv080_callback_data_ready_t)use_sensor_output, callback_parameters);
61+
if (bmv080_current_status != E_BMV080_OK)
62+
{
63+
printf("Fetching measurement data failed with BMV080 status %d\r\n", (int32_t)bmv080_current_status);
64+
}
7065
}
7166

7267

@@ -84,11 +79,6 @@ sfeTkError_t sfeBmv080::begin(sfeTkII2C *theBus)
8479
// Set bus pointer
8580
_theBus = theBus;
8681

87-
// Set the bus pointer for the I2C device struct instance member
88-
//_i2c_device.instance = theBus;
89-
90-
i2c_init(&_i2c_device);
91-
9282
sfeTkError_t err;
9383
err = isConnected();
9484
// Check whether the ping was successful
@@ -134,7 +124,8 @@ bool sfeBmv080::setMode(uint8_t mode)
134124
}
135125
else if(mode == SFE_BMV080_MODE_DUTY_CYCLE)
136126
{
137-
bmv080_current_status = bmv080_start_duty_cycling_measurement(bmv080_handle_class, (bmv080_callback_tick_t)millis, E_BMV080_DUTY_CYCLING_MODE_0);
127+
bmv080_duty_cycling_mode_t duty_cycling_mode = E_BMV080_DUTY_CYCLING_MODE_0;
128+
bmv080_current_status = bmv080_start_duty_cycling_measurement(bmv080_handle_class, (bmv080_callback_tick_t)millis, duty_cycling_mode);
138129
}
139130

140131
// check if the mode was set correctly
@@ -260,4 +251,35 @@ bool sfeBmv080::getID()
260251
printf("BMV080 sensor ID: %s\n", id);
261252
return true;
262253
}
254+
}
255+
256+
uint16_t sfeBmv080::getDutyCyclingPeriod()
257+
{
258+
uint16_t duty_cycling_period = 0;
259+
bmv080_status_code_t bmv080_current_status = bmv080_get_parameter(bmv080_handle_class, "duty_cycling_period", (void*)&duty_cycling_period);
260+
if (bmv080_current_status != E_BMV080_OK)
261+
{
262+
printf("Error getting BMV080 Duty Cycling Period: %d\n", bmv080_current_status);
263+
return 0;
264+
}
265+
else
266+
{
267+
printf("BMV080 Duty Cycling Period Read: %d\n", duty_cycling_period);
268+
return duty_cycling_period;
269+
}
270+
}
271+
272+
bool sfeBmv080::setDutyCyclingPeriod(uint16_t duty_cycling_period)
273+
{
274+
bmv080_status_code_t bmv080_current_status = bmv080_set_parameter(bmv080_handle_class, "duty_cycling_period", (void*)&duty_cycling_period);
275+
if (bmv080_current_status != E_BMV080_OK)
276+
{
277+
printf("Error setting BMV080 Duty Cycling Period: %d\n", bmv080_current_status);
278+
return false;
279+
}
280+
else
281+
{
282+
printf("BMV080 Duty Cycling Period Set: %d\n", duty_cycling_period);
283+
return true;
284+
}
263285
}

src/sfeBmv080.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ class sfeBmv080
9999
/// @return True if new data is available, false otherwise
100100
bool dataAvailable();
101101

102+
/// @brief Get the duty cycling period
103+
/// @return The duty cycling period in seconds
104+
uint16_t getDutyCyclingPeriod();
105+
106+
/// @brief Set the duty cycling period
107+
/// @param period The duty cycling period in seconds
108+
/// @return True if successful, false otherwise
109+
bool setDutyCyclingPeriod(uint16_t duty_cycling_period);
110+
102111
private:
103112
bmv080_handle_t bmv080_handle_class = NULL;
104113
bool _dataAvailable = false;

0 commit comments

Comments
 (0)