Skip to content

Commit c3be028

Browse files
committed
example 9 open close
-shows how to open the instance of the object, and then close it down entirely, and then repeat.
1 parent d51cb51 commit c3be028

File tree

3 files changed

+211
-0
lines changed

3 files changed

+211
-0
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/*
2+
Using the BMV080 Particulate Matter PM2.5 Sensor
3+
4+
This example shows how to open an instance of the object, take 5 readings,
5+
then close the object.
6+
7+
It then repeats this process every 5 seconds.
8+
9+
This example shows how to use the sensor in "continuous mode" to get
10+
particulate matter readings once every second.
11+
12+
It uses polling of the device to check if new data is available.
13+
14+
By: Pete Lewis
15+
SparkFun Electronics
16+
Date: September, 2024
17+
SparkFun code, firmware, and software is released under the MIT License.
18+
Please see LICENSE.md for further details.
19+
20+
Hardware Connections:
21+
IoT RedBoard --> BMV080
22+
QWIIC --> QWIIC
23+
24+
BMV080 "mode" jumper set to I2C (default)
25+
26+
Serial.print it out at 115200 baud to serial monitor.
27+
28+
Feel like supporting our work? Buy a board from SparkFun!
29+
https://www.sparkfun.com/products/26554
30+
*/
31+
32+
#include "SparkFun_BMV080_Arduino_Library.h" // CTRL+Click here to get the library: http://librarymanager/All#SparkFun_BMV080
33+
#include <Wire.h>
34+
35+
36+
#define BMV080_ADDR 0x57 // SparkFun BMV080 Breakout defaults to 0x57
37+
38+
// Some Dev boards have their QWIIC connector on Wire or Wire1
39+
// This #ifdef will help this sketch work across more products
40+
41+
#ifdef ARDUINO_SPARKFUN_THINGPLUS_RP2040
42+
#define wirePort Wire1
43+
#else
44+
#define wirePort Wire
45+
#endif
46+
47+
int testTimes = 0; // Test times
48+
49+
void setup()
50+
{
51+
Serial.begin(115200);
52+
53+
while (!Serial)
54+
delay(10); // Wait for Serial to become available.
55+
// Necessary for boards with native USB (like the SAMD51 Thing+).
56+
// For a final version of a project that does not need serial debug (or a USB cable plugged in),
57+
// Comment out this while loop, or it will prevent the remaining code from running.
58+
59+
Serial.println();
60+
Serial.println("BMV080 Example 09 - Open and Close");
61+
}
62+
63+
void loop()
64+
{
65+
Serial.print("Test times: ");
66+
Serial.println(testTimes);
67+
testOnce();
68+
testTimes++;
69+
delay(5000);
70+
}
71+
72+
73+
void testOnce()
74+
{
75+
Serial.println();
76+
Serial.println("BMV080 Begin testing readings");
77+
78+
SparkFunBMV080 bmv080; // Create an instance of the BMV080 class
79+
80+
wirePort.begin();
81+
82+
if (bmv080.begin(BMV080_ADDR, wirePort) == false)
83+
{
84+
Serial.println(
85+
"BMV080 not detected at default I2C address. Check your jumpers and the hookup guide. Freezing...");
86+
while (1)
87+
;
88+
}
89+
Serial.println("BMV080 found!");
90+
91+
// wirePort.setClock(400000); //Increase I2C data rate to 400kHz
92+
93+
/* Initialize the Sensor (read driver, open, reset, id etc.)*/
94+
// Do what init() does, but check each call to each function individually
95+
// to see if there are any issues with the sensor
96+
97+
uint16_t major, minor, patch;
98+
if (bmv080.driverVersion(major, minor, patch) == true)
99+
{
100+
Serial.print("BMV080 Driver Version: ");
101+
Serial.print(major);
102+
Serial.print(".");
103+
Serial.print(minor);
104+
Serial.print(".");
105+
Serial.println(patch);
106+
}
107+
else
108+
{
109+
Serial.println("Error getting BMV080 driver version");
110+
}
111+
112+
if (bmv080.open() == true)
113+
{
114+
Serial.println("BMV080 Opened");
115+
}
116+
else
117+
{
118+
Serial.println("Error opening BMV080");
119+
}
120+
121+
if (bmv080.reset() == true)
122+
{
123+
Serial.println("BMV080 Reset");
124+
}
125+
else
126+
{
127+
Serial.println("Error resetting BMV080");
128+
}
129+
130+
char idOut[13];
131+
if (bmv080.ID(idOut) == true)
132+
{
133+
Serial.print("BMV080 ID: ");
134+
Serial.println(idOut);
135+
}
136+
else
137+
{
138+
Serial.println("Error getting BMV080 ID");
139+
}
140+
141+
/* Set the sensor mode to continuous mode */
142+
if (bmv080.setMode(SF_BMV080_MODE_CONTINUOUS) == true)
143+
{
144+
Serial.println("BMV080 set to continuous mode");
145+
}
146+
else
147+
{
148+
Serial.println("Error setting BMV080 mode");
149+
}
150+
151+
// Take 5 readings
152+
153+
// Poll the sensor 50 times, once every 100ms
154+
// The sensor is setup to report readings once per second
155+
// So this will result in 5 readings.
156+
for(int i = 0 ; i < 50 ; i++)
157+
{
158+
if (bmv080.readSensor())
159+
{
160+
float pm10 = bmv080.PM10();
161+
float pm25 = bmv080.PM25();
162+
float pm1 = bmv080.PM1();
163+
164+
Serial.print("PM10: ");
165+
Serial.print(pm10);
166+
Serial.print("\t");
167+
Serial.print("PM2.5: ");
168+
Serial.print(pm25);
169+
Serial.print("\t");
170+
Serial.print("PM1: ");
171+
Serial.print(pm1);
172+
173+
if (bmv080.isObstructed() == true)
174+
{
175+
Serial.print("\tObstructed");
176+
}
177+
178+
Serial.println();
179+
}
180+
delay(100);
181+
}
182+
183+
Serial.println("BMV080 End testing readings");
184+
185+
bmv080.close();
186+
187+
wirePort.end();
188+
}

src/sfTk/sfDevBMV080.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,19 @@ bool sfDevBMV080::open()
253253
return (status == E_BMV080_OK);
254254
}
255255

256+
//---------------------------------------------------------------------
257+
bool sfDevBMV080::close()
258+
{
259+
if (_theBus == nullptr)
260+
return false;
261+
262+
// Close the device
263+
264+
bmv080_status_code_t status = bmv080_close(&_bmv080_handle_class);
265+
266+
return (status == E_BMV080_OK);
267+
}
268+
256269
//---------------------------------------------------------------------
257270
bool sfDevBMV080::reset()
258271
{

src/sfTk/sfDevBMV080.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@ class sfDevBMV080
114114
*/
115115
bool open(void);
116116

117+
/**
118+
* @brief Closes communication with the BMV080 sensor
119+
*
120+
* @return true if the sensor was successfully closed and handle created
121+
* @return false if the sensor could not be closed or handle creation failed
122+
*
123+
* @see open()
124+
*/
125+
bool close(void);
126+
117127
/**
118128
* @brief Resets the BMV080 sensor to its default state
119129
*

0 commit comments

Comments
 (0)