PM25AQI: store one parameter into variable

I'm working with a PMSA003I sensor and currently just using the PM25_test example from the Adafruit_PM25AQI library. For reference, here is the code:

/* Test sketch for Adafruit PM2.5 sensor with UART or I2C */

#include "Adafruit_PM25AQI.h"

// If your PM2.5 is UART only, for UNO and others (without hardware serial) 
// we must use software serial...
// pin #2 is IN from sensor (TX pin on sensor), leave pin #3 disconnected
// comment these two lines if using hardware serial
//#include <SoftwareSerial.h>
//SoftwareSerial pmSerial(2, 3);

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);


I wanted to know how I could store just one of the parameters into a variable (e.g., store only the pm25_env readings into a variable). I've tried using for example, but the error I get is

no matching function for call to 'Adafruit_PM25AQI::read(uint16_t*)'

Does this mean that the function in the library will only work with the entire &data? Is there a way to modify the function so that it can read single parameters?

I'm quite new to Arduino, any help would be appreciated!

You have the source - of course it is possible.
But why bother?

I'd like to have the just the pm25_env data displayed on an OLED.
Is there a better way to do this (maybe involving ?), or how would I need to modify the source code?

This is what I'm looking at (cpp file), but I'm not sure how to modify the " bool Adafruit_PM25AQI::read(PM25_AQI_Data *data) " function to read pm25_env only (or better yet, create a new function).

I don't have the device, but I looked at the library.
It looks like the library has to read the entire block of data from the sensor, however much you use - it's just how the sensor presents the data.

I don't understand your concern - if you don't want to see the other data, don't display it.
Any modified method would simply call the read function anyway.

That's a good point.

I did think of just displaying pm25_env, but I'm not sure how to do it. (I had seen an example using a different sensor and library that involved just reading one parameter, so I thought I'd try that).

I'm using the heltec.h library, and I've tried using

Heltec.display->drawString(0, 0, data.pm25_env)

but the data is not a String so it doesn't work. I tried storing the data to a String,

String envPM25 = data.pm_25env;

Heltec.display->drawString(0, 0, envPM25);

but the error I get is

conversion from 'uint16_t' {aka 'short unsigned int'} to 'String' is ambiguous

Would I need to use Serial.readString perhaps (if so, I'm not sure how to properly use it)?

Are you saying that your display library can't display integers?

Heltec.display->drawString(0, 0, String(envPM25)); did not work?

Ah, that seems to have done the trick -- thank you both for your input and patience!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.