|
| 1 | +/* |
| 2 | + Using the BMV080 Particulate Matter PM2.5 Sensor |
| 3 | +
|
| 4 | + This example shows how to use two BMV080 sensors on the same I2C bus. |
| 5 | + |
| 6 | + One sensor must have its AB0 Jumper changed to "0". |
| 7 | + |
| 8 | + The sensors will be in "continuous mode" to get |
| 9 | + particulate matter readings once every second. |
| 10 | +
|
| 11 | + It uses polling of the devices to check if new data is available. |
| 12 | +
|
| 13 | + By: Pete Lewis |
| 14 | + SparkFun Electronics |
| 15 | + Date: September, 2024 |
| 16 | + SparkFun code, firmware, and software is released under the MIT License. |
| 17 | + Please see LICENSE.md for further details. |
| 18 | +
|
| 19 | + Hardware Connections: |
| 20 | + IoT RedBoard --> BMV080 |
| 21 | + QWIIC --> QWIIC |
| 22 | +
|
| 23 | + BMV080 "mode" jumper set to I2C (default) |
| 24 | +
|
| 25 | + Serial.print it out at 115200 baud to serial monitor. |
| 26 | +
|
| 27 | + Open a plotter to see the PM2.5 values from both sensors. |
| 28 | +
|
| 29 | + Feel like supporting our work? Buy a board from SparkFun! |
| 30 | + https://www.sparkfun.com/products/26554 |
| 31 | +*/ |
| 32 | + |
| 33 | +#include "SparkFun_BMV080_Arduino_Library.h" // CTRL+Click here to get the library: http://librarymanager/All#SparkFun_BMV080 |
| 34 | +#include <Wire.h> |
| 35 | + |
| 36 | +SparkFunBMV080I2C bmv080; // Create an instance of the BMV080 class |
| 37 | +#define BMV080_ADDR 0x57 // SparkFun BMV080 Breakout defaults to 0x57 |
| 38 | + |
| 39 | +SparkFunBMV080I2C bmv080_2; // Create an instance of the BMV080 class |
| 40 | +#define BMV080_ADDR2 0x56 // AB0 Jumper set to 0 |
| 41 | + |
| 42 | +bool newDataAvailable = false; // Flag to indicate new data is available |
| 43 | +bool newDataAvailable2 = false; // Flag to indicate new data is available |
| 44 | + |
| 45 | +float pm25 = 0.0; // Variable to store PM2.5 value |
| 46 | +float pm25_2 = 0.0; // Variable to store PM2.5 value |
| 47 | + |
| 48 | +bool isObstructed = false; // Flag to indicate sensor is obstructed |
| 49 | +bool isObstructed2 = false; // Flag to indicate sensor is obstructed |
| 50 | + |
| 51 | +void setup() |
| 52 | +{ |
| 53 | + Serial.begin(115200); |
| 54 | + |
| 55 | + while (!Serial) |
| 56 | + delay(10); // Wait for Serial to become available. |
| 57 | + // Necessary for boards with native USB (like the SAMD51 Thing+). |
| 58 | + // For a final version of a project that does not need serial debug (or a USB cable plugged in), |
| 59 | + // Comment out this while loop, or it will prevent the remaining code from running. |
| 60 | + |
| 61 | + Serial.println(); |
| 62 | + Serial.println("BMV080 Example 1 - Basic Readings"); |
| 63 | + |
| 64 | + Wire.begin(); |
| 65 | + |
| 66 | + if (bmv080.begin(BMV080_ADDR, Wire) == false) |
| 67 | + { |
| 68 | + Serial.println( |
| 69 | + "BMV080 not detected at default I2C address. Check your jumpers and the hookup guide. Freezing..."); |
| 70 | + while (1) |
| 71 | + ; |
| 72 | + } |
| 73 | + Serial.println("BMV080 at 0x57 found!"); |
| 74 | + |
| 75 | + if (bmv080_2.begin(BMV080_ADDR2, Wire) == false) |
| 76 | + { |
| 77 | + Serial.println( |
| 78 | + "BMV080 not detected at 0x56 I2C address. Check your jumpers and the hookup guide. Freezing..."); |
| 79 | + while (1) |
| 80 | + ; |
| 81 | + } |
| 82 | + Serial.println("BMV080 at 0x56 found!"); |
| 83 | + |
| 84 | + // Wire.setClock(400000); //Increase I2C data rate to 400kHz |
| 85 | + |
| 86 | + /* Initialize the Sensor (read driver, open, reset, id etc.)*/ |
| 87 | + bmv080.init(); |
| 88 | + |
| 89 | + /* Set the sensor mode to continuous mode */ |
| 90 | + if (bmv080.setMode(SFE_BMV080_MODE_CONTINUOUS) == true) |
| 91 | + { |
| 92 | + Serial.println("BMV080 set to continuous mode"); |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + Serial.println("Error setting BMV080 mode"); |
| 97 | + } |
| 98 | + |
| 99 | + delay(500); |
| 100 | + |
| 101 | + bmv080_2.init(); |
| 102 | + |
| 103 | + if (bmv080_2.setMode(SFE_BMV080_MODE_CONTINUOUS) == true) |
| 104 | + { |
| 105 | + Serial.println("BMV080_2 set to continuous mode"); |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + Serial.println("Error setting BMV080_2 mode"); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +void loop() |
| 114 | +{ |
| 115 | + if (bmv080.dataAvailable()) |
| 116 | + { |
| 117 | + pm25 = bmv080.getPM25(); |
| 118 | + isObstructed = bmv080.getIsObstructed(); |
| 119 | + newDataAvailable = true; |
| 120 | + //Serial.println("Sensor 1 data available"); |
| 121 | + } |
| 122 | + delay(200); // needs a ~200ms delay in between talking to each sensor |
| 123 | + |
| 124 | + if (bmv080_2.dataAvailable()) |
| 125 | + { |
| 126 | + pm25_2 = bmv080_2.getPM25(); |
| 127 | + isObstructed2 = bmv080_2.getIsObstructed(); |
| 128 | + newDataAvailable2 = true; |
| 129 | + //Serial.println("Sensor 2 data available"); |
| 130 | + } |
| 131 | + |
| 132 | + delay(200); // needs a ~200ms delay in between talking to each sensor |
| 133 | + |
| 134 | + if (newDataAvailable & newDataAvailable2) |
| 135 | + { |
| 136 | + //Serial.print("Sensor 1: "); |
| 137 | + |
| 138 | + Serial.print(pm25); |
| 139 | + |
| 140 | + if (isObstructed == true) |
| 141 | + { |
| 142 | + Serial.print(-1); |
| 143 | + } |
| 144 | + |
| 145 | + //Serial.print("\tSensor 2: "); |
| 146 | + |
| 147 | + Serial.print(","); |
| 148 | + |
| 149 | + Serial.print(pm25_2); |
| 150 | + |
| 151 | + if (isObstructed2 == true) |
| 152 | + { |
| 153 | + Serial.print("-1"); |
| 154 | + } |
| 155 | + |
| 156 | + Serial.println(); |
| 157 | + |
| 158 | + // reset variables |
| 159 | + newDataAvailable = false; |
| 160 | + newDataAvailable2 = false; |
| 161 | + isObstructed = false; |
| 162 | + isObstructed2 = false; |
| 163 | + } |
| 164 | +} |
0 commit comments