1
+ /*
2
+ Using the BMV080 Particulate Matter PM2.5 Sensor
3
+
4
+ This example shows how to use the sensor in "continuous mode" to get
5
+ particulate matter readings once every second.
6
+
7
+ The hardware interrupt of the BMV080 sensor unit cannot be used as trigger
8
+ in duty cycling mode.
9
+
10
+ This example sets up an interrupt triggered by the BMV080's IRQ pin to take readings.
11
+
12
+ By: Pete Lewis
13
+ SparkFun Electronics
14
+ Date: September, 2024
15
+ SparkFun code, firmware, and software is released under the MIT License.
16
+ Please see LICENSE.md for further details.
17
+
18
+ Hardware Connections:
19
+ IoT RedBoard --> BMV080
20
+ QWIIC --> QWIIC
21
+ 14 --> IRQ
22
+
23
+ BMV080 "mode" jumper set to I2C (default)
24
+
25
+ Serial.print it out at 115200 baud to serial monitor.
26
+
27
+ Feel like supporting our work? Buy a board from SparkFun!
28
+ https://www.sparkfun.com/products/?????
29
+ */
30
+
31
+ #include < Wire.h>
32
+ #include " SparkFun_BMV080_Arduino_Library.h" // CTRL+Click here to get the library: http://librarymanager/All#SparkFun_BMV080
33
+
34
+ Bmv080 bmv080; // Create an instance of the BMV080 class
35
+ #define BMV080_ADDR 0x57 // SparkFun BMV080 Breakout defaults to 0x57
36
+ #define IRQ_Pin 14
37
+
38
+ i2c_device_t i2c_device = {}; // I2C device struct instance for Bosch API
39
+
40
+ bool int_flag = false ;
41
+
42
+ void setup ()
43
+ {
44
+ Serial.begin (115200 );
45
+
46
+ while (!Serial) delay (10 ); // Wait for Serial to become available.
47
+ // Necessary for boards with native USB (like the SAMD51 Thing+).
48
+ // For a final version of a project that does not need serial debug (or a USB cable plugged in),
49
+ // Comment out this while loop, or it will prevent the remaining code from running.
50
+
51
+ Serial.println ();
52
+ Serial.println (" BMV080 Example 3 - Interrupt" );
53
+
54
+ Wire.begin ();
55
+
56
+ if (bmv080.begin (BMV080_ADDR, Wire) == false ) {
57
+ Serial.println (" BMV080 not detected at default I2C address. Check your jumpers and the hookup guide. Freezing..." );
58
+ while (1 )
59
+ ;
60
+ }
61
+ Serial.println (" BMV080 found!" );
62
+
63
+ // Wire.setClock(400000); //Increase I2C data rate to 400kHz
64
+
65
+ /* Communication interface initialization */
66
+ i2c_init (&i2c_device);
67
+
68
+ /* Initialize the Sensor (read driver, open, reset, id etc.)*/
69
+ bmv080.init (&i2c_device);
70
+
71
+ /* Set the sensor mode to continuous mode */
72
+ // The hardware interrupt of the BMV080 sensor unit cannot be used as trigger
73
+ // in duty cycling mode.
74
+ if (bmv080.setMode (SFE_BMV080_MODE_CONTINUOUS) == true )
75
+ {
76
+ Serial.println (" BMV080 set to continuous mode" );
77
+ }
78
+ else
79
+ {
80
+ Serial.println (" Error setting BMV080 mode" );
81
+ }
82
+
83
+ enable_external_interrupt (true );
84
+ }
85
+
86
+ void loop ()
87
+ {
88
+ if (int_flag == true )
89
+ {
90
+ Serial.println (" Interrupt triggered" );
91
+ int_flag = false ; // Reset the flag
92
+ do {
93
+ Serial.println (" Reading BMV080" );
94
+ if (bmv080.dataAvailable ())
95
+ {
96
+ float pm25 = bmv080.getPM25 ();
97
+ Serial.print (pm25);
98
+ if (bmv080.getIsObstructed () == true )
99
+ {
100
+ Serial.print (" \t Obstructed" );
101
+ }
102
+ Serial.println ();
103
+ }
104
+ } while (digitalRead (IRQ_Pin) == LOW);
105
+ }
106
+ delay (100 );
107
+ Serial.print (" ." );
108
+ }
109
+
110
+ void enable_external_interrupt (bool enable)
111
+ {
112
+ int checkPin = digitalPinToInterrupt (IRQ_Pin);
113
+
114
+ if (checkPin == -1 ) {
115
+ Serial.println (" Not a valid interrupt pin!" );
116
+ }
117
+ else
118
+ {
119
+ Serial.println (" Valid interrupt pin." );
120
+ }
121
+
122
+ pinMode (IRQ_Pin, INPUT_PULLUP);
123
+
124
+ if (enable)
125
+ { /* Enabel external interrupt */
126
+ attachInterrupt (digitalPinToInterrupt (IRQ_Pin), gpio_isr_handler, FALLING );
127
+ }
128
+ else
129
+ { /* Disable external interrupt */
130
+ detachInterrupt (digitalPinToInterrupt (IRQ_Pin) );
131
+ }
132
+ }
133
+
134
+ void gpio_isr_handler (void )
135
+ {
136
+ int_flag = true ;
137
+ }
0 commit comments