Example Code for Arduino-Continuous Data Reading in SPI Mode

Last revision 2026/02/05

The article demonstrates how to achieve continuous data reading in SPI mode using an Arduino setup with a BMV080 PM2.5 sensor, covering hardware preparation, software setup, wiring, and example code for successful implementation.

Hardware Preparation

Software Preparation

Wiring Diagram

SEN0663-SPI Wiring Diagram

Connect the BMV080 sensor to the ESP32-E as illustrated, with the key wiring correspondences detailed below:

  • Sensor Pin "3V3" → ESP32-E 3.3V
  • Sensor Pin "GND" → ESP32-E GND
  • Sensor SPI Pin "SCK" → ESP32-E SCK (default GPIO18)
  • Sensor SPI Pin "MOSI" → ESP32-E MOSI (default GPIO23)
  • Sensor SPI Pin "MISO" → ESP32-E MISO (default GPIO19)
  • Sensor SPI Pin "CSB" → ESP32-E GPIO13

Other Preparation Work

  • Remove the 0Ω resistor at the "I2C" position on the board (as indicated in the figure below) and re-solder it to the "SPI" side pad;
  • Solder one 0Ω resistor each to the pads beneath the "H" silkscreen corresponding to "CSB" and "MISO" on the right side (to ensure stable SPI signal transmission).

Sample Code

#include "DFRobot_BMV080.h"

// Set loop task stack size (ESP32 requires sufficient memory to run the sensor, preventing crashes)
SET_LOOP_TASK_STACK_SIZE(60 * 1024);

// Key SPI configuration: Define chip select (CS) pin, modify according to actual ESP32-E wiring (example uses pin 13)
#define SPI_CS_PIN 13
DFRobot_BMV080_SPI sensor(&SPI, SPI_CS_PIN);

float pm1, pm2_5, pm10;

void setup() {
  char id[13];
  Serial.begin(115200);
  while (!Serial) delay(100);  

  Serial.println("Starting SPI mode to read PM values");
  while (sensor.begin() != 0) {
    Serial.println("Sensor initialization failed! Please check wiring!");
    delay(1000);
  }
  Serial.println("Sensor initialization successful");

  while (sensor.openBmv080()) {
    Serial.println("Sensor opening failed");
    delay(1000);
  }
  Serial.println("Sensor opened successfully");

  // Read and print chip ID (used to confirm normal hardware recognition)
  sensor.getBmv080ID(id);
  Serial.print("Chip ID: ");
  Serial.println(id);

  // Set to continuous measurement mode (basic mode for reading PM values)
  if (0 == sensor.setBmv080Mode(CONTINUOUS_MODE)) {
    Serial.println("Continuous measurement mode enabled");
  } else {
    Serial.println("Mode setting failed");
  }
}

void loop() {
  if (sensor.getBmv080Data(&pm1, &pm2_5, &pm10)) {
    Serial.print("PM1: " + String(pm1) + " ug/m³  ");
    Serial.print("PM2.5: " + String(pm2_5) + " ug/m³  ");
    Serial.print("PM10: " + String(pm10) + " ug/m³");

    // Check for obstruction
    if (sensor.ifObstructed()) {
      Serial.print("  Note: Sensor is obstructed, data may be invalid");
    }
    Serial.println();
  }
  delay(100);  
}

Result

SEN0663-SPI Result

Was this article helpful?

TOP