How do I code PMSA003i with a virtual pin?

I’m using and adafruit feather huzzah32 as a bluetooth module and while I can get it to connect to the blynk app, I have no idea how to write the sensor data to a virtual pin since that’s the only way to communicate with an I2C connection. Can anyone help me modify this to make it send data to a virtual pin?

#include "Adafruit_PM25AQI.h"

Adafruit_PM25AQI aqi = Adafruit_PM25AQI();

void setup() {
  // Wait for serial monitor to open
  while (!Serial) delay(10);

  Serial.println("Adafruit PMSA003I Air Quality Sensor");

  // Wait one second for sensor to boot up!

  // If using serial, initialize it and set baudrate before starting!
  // Uncomment one of the following

  // There are 3 options for connectivity!
  if (! aqi.begin_I2C()) {      // connect to the sensor over I2C
  //if (! aqi.begin_UART(&Serial1)) { // connect to the sensor over hardware serial
  //if (! aqi.begin_UART(&pmSerial)) { // connect to the sensor over software serial 
    Serial.println("Could not find PM 2.5 sensor!");
    while (1) delay(10);

  Serial.println("PM25 found!");

void loop() {
  PM25_AQI_Data data;
  if (! {
    Serial.println("Could not read from AQI");
    delay(500);  // try again in a bit!
  Serial.println("AQI reading success");

  Serial.println(F("Concentration Units (standard)"));
  Serial.print(F("PM 1.0: ")); Serial.print(data.pm10_standard);
  Serial.print(F("\t\tPM 2.5: ")); Serial.print(data.pm25_standard);
  Serial.print(F("\t\tPM 10: ")); Serial.println(data.pm100_standard);
  Serial.println(F("Concentration Units (environmental)"));
  Serial.print(F("PM 1.0: ")); Serial.print(data.pm10_env);
  Serial.print(F("\t\tPM 2.5: ")); Serial.print(data.pm25_env);
  Serial.print(F("\t\tPM 10: ")); Serial.println(data.pm100_env);
  Serial.print(F("Particles > 0.3um / 0.1L air:")); Serial.println(data.particles_03um);
  Serial.print(F("Particles > 0.5um / 0.1L air:")); Serial.println(data.particles_05um);
  Serial.print(F("Particles > 1.0um / 0.1L air:")); Serial.println(data.particles_10um);
  Serial.print(F("Particles > 2.5um / 0.1L air:")); Serial.println(data.particles_25um);
  Serial.print(F("Particles > 5.0um / 0.1L air:")); Serial.println(data.particles_50um);
  Serial.print(F("Particles > 10 um / 0.1L air:")); Serial.println(data.particles_100um);


You need to start by cleaning-up your void loop and getting rid of the delay and replacing it with a BlynkTimer.

Then you need to read the documentation on the Blynk.virtualWrite(vPin) command to send the data to the Blynk app.

BTW, Bluetooth is an awful connection method for this type of thing, and you probably won’t get the functionality you’re hoping for.


Yeah thanks. I can use wifi, no big deal. I read the documentation. I guess I need to figure out if I need to set up 3 parameters since the sensor checks three different particle sizes and I wanted to find out how to write the function properly. The sketch I uploaded was from Arduino. I just was wondering how the code should look when sending the data that this sketch reads, in the form of a blynk function.

Think of the Blynk.virtualWrite(vPin) command as being the same as a serial print comm@nd, just that it “prints” the data to Blynk.

Take a look at the Sketch Builder examples for something like the DHT sensor, it will give you a good idea of the code structure and how to send data (humidity and temperature in the example) to different virtual pins.


Thank you very much, Pete. That made a big difference. For some reason I couldn’t get that through my thick skull. The sketch builder filled in some gaps too. Much appreciated.

1 Like