Abnormal sensor readings from a pm2.5 sensor by Plantower [solved]

I have connected a pm2.5 particle sensor by Plantower to an Oled display which displays the data from the sensor. The readings fluctuate significantly, as outside the data readings flatline to 0 but inside skyrockets up to the 300s (which is completely unlivable) but also drops down by several hundreds even when in the same spot. I'm wondering if it has something to do with my code or the sensor? The following code takes the data from the sensor and displays it on the oled, also calculating it into an AQI reading. This is the code:

// NOTES
// OLED wiring, SDA = pin 4, SCL = pin 5
// Sensor wiring, data (txd) = pin 2

#include <SoftwareSerial.h>
SoftwareSerial pmsSerial(2, 3);

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

// this is the Width and Height of Display which is 128 xy 32
#define LOGO16_GLCD_HEIGHT 64
#define LOGO16_GLCD_WIDTH 128

#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

void setup() {
  delay(3000);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  display.display();
  delay(2000);
  display.clearDisplay();
  // sensor baud rate is 9600
  pmsSerial.begin(9600);
}

struct pms5003data {
  uint16_t framelen;
  uint16_t pm10_standard, pm25_standard, pm100_standard;
  uint16_t pm10_env, pm25_env, pm100_env;
  uint16_t particles_03um, particles_05um, particles_10um, particles_25um, particles_50um, particles_100um;
  uint16_t unused;
  uint16_t checksum;
};

struct pms5003data data;

void loop() {
  if (readPMSdata(&pmsSerial)) {
    String vString5 = String(data.pm25_env);
    String vString6 = String(data.pm100_env);

    display.clearDisplay();

    showText("PM2.5:", 1, 14, 1, false);
    showText(vString5 + " ug/M^2", 50, 14, 1, false);

    showText("PM10 :", 1, 24, 1, false);
    showText(vString6 + " ug/M^2", 50, 24, 1, false);

    int aqival = calcAQI25(data.pm25_env);
    showText("AQI:" + String(aqival), 1, 1, 1, false);
    String aqi_text = make_aqi_words(aqival);
    showText(aqi_text, 50, 1, 1, false);

    display.display();

    delay(1000);  //readings only once per second
  }
}

boolean readPMSdata(Stream *s) {
  if (!s->available()) {
    return false;
  }

  // Read a byte at a time until we get to the special '0x42' start-byte
  if (s->peek() != 0x42) {
    s->read();
    return false;
  }

  // Now read all 32 bytes
  if (s->available() < 32) {
    return false;
  }

  uint8_t buffer[32];
  uint16_t sum = 0;
  s->readBytes(buffer, 32);

  // get checksum ready
  for (uint8_t i = 0; i < 30; i++) {
    sum += buffer[i];
  }

  // The data comes in endian'd, this solves it so it works on all platforms
  uint16_t buffer_u16[15];
  for (uint8_t i = 0; i < 15; i++) {
    buffer_u16[i] = buffer[2 + i * 2 + 1];
    buffer_u16[i] += (buffer[2 + i * 2] << 8);
  }

  // put it into a nice struct :)
  memcpy((void *)&data, (void *)buffer_u16, 30);

  if (sum != data.checksum) {
    Serial.println("Checksum failure");
    return false;
  }
  // success!
  return true;
}

void showText(String text, int x, int y, int size, boolean d) {
  display.setTextSize(size);
  display.setTextColor(WHITE);
  display.setCursor(x, y);
  display.println(text);
  if (d) {
    display.display();
  }
}


int calcAQI25(int pm25) {

  // Uses formula AQI = ( (pobs - pmin) x (aqimax - aqimin) ) / (pmax - pmin)  + aqimin
  float pmin, pmax, amin, amax;

  if (pm25 <= 12) {
    pmin = 0;
    pmax = 12;
    amin = 0;
    amax = 50;
    goto aqicalc;
  }
  if (pm25 <= 35.5) {
    pmin = 12;
    pmax = 35.5;
    amin = 50;
    amax = 100;
    goto aqicalc;
  }
  if (pm25 <= 55.5) {
    pmin = 35.5;
    pmax = 55.5;
    amin = 100;
    amax = 150;
    goto aqicalc;
  }
  if (pm25 <= 150.5) {
    pmin = 55.5;
    pmax = 150.5;
    amin = 150;
    amax = 200;
    goto aqicalc;
  }
  if (pm25 <= 250.5) {
    pmin = 150.5;
    pmax = 250.5;
    amin = 200;
    amax = 300;
    goto aqicalc;
  }
  if (pm25 <= 350.5) {
    pmin = 250.5;
    pmax = 350.5;
    amin = 300;
    amax = 400;
    goto aqicalc;
  }
  if (pm25 <= 500.5) {
    pmin = 350.5;
    pmax = 500.5;
    amin = 400;
    amax = 500;
    goto aqicalc;
  } else {
    return 999;
  }

aqicalc:
  float aqi = (((pm25 - pmin) * (amax - amin)) / (pmax - pmin)) + amin;
  return aqi;
}

String make_aqi_words(float aqival) {
  if (aqival <= 12) { return "GOOD!"; }
  if (aqival <= 35.5) { return "MEDIUM"; }
  if (aqival <= 55.5) { return "BAD"; }
  if (aqival <= 150.5) { return "VERY BAD"; }
  if (aqival <= 250.5) { return "REALLY BAD"; }
  if (aqival <= 350.5) { return "DEADLY"; }
  if (aqival <= 500.5) { return "GO. GO NOW!"; }
  return "Error";
}
``

If you use serial.Print() to display the raw values from the sensor, you will know if the sensor is bad or your code is bad.

Depends on what data you are printing. The pm25_env data are the only useful measurements, and only those below 10 um in size. The sensor does not actually "see" larger particles, and is believed to estimate those from typical distributions of particle sizes.
See https://www.researchgate.net/publication/320555036_Particle_Distribution_Dependent_Inaccuracy_of_the_Plantower_PMS5003_low-cost_PM-sensor

Here is what I display:

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

#include "Adafruit_PM25AQI.h"
// for Feather ESP32-S2 TFT
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>

// Use dedicated hardware SPI pins
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
Adafruit_PM25AQI aqi = Adafruit_PM25AQI();

void setup() {

#if defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2_TFT)
  // turn on backlite
  pinMode(TFT_BACKLITE, OUTPUT);
  digitalWrite(TFT_BACKLITE, HIGH);
  // power up I2C port

  pinMode(TFT_I2C_POWER, OUTPUT);
  digitalWrite(TFT_I2C_POWER, HIGH);
#endif


  // Wait for serial monitor to open
  //Serial.begin(115200);
  // while (!Serial) delay(10);
  //  Serial.println("Adafruit PMSA003I Air Quality Sensor");

  // Wait for sensor to boot up
  delay(3000);
  // initialize TFT
  tft.init(135, 240); // Init ST7789 240x135
  tft.setRotation(3);
  tft.fillScreen(ST77XX_BLACK);
  tft.setTextSize(3);
  tft.setTextColor(ST77XX_GREEN);
  tft.println("PM2.5");
  if (! aqi.begin_I2C()) {      // connect to the sensor over I2C
    tft.println("Could not find PM 2.5 sensor!");
    while (1) delay(10);
  }

  delay(5000);
  // Serial.println("PM25, particle counts <= 1 um");
}

PM25_AQI_Data data;
int pm25_max = 0;
int pm25_min = 10000;

void loop() {
  if (! aqi.read(&data)) {
    //    Serial.println("Could not read from AQI");
    delay(500);  // try again in a bit!
    return;
  }
  // use pm25_env data, ignore "standard"
  // The PMS5003 however is not able at all to directly measure large particles > 3um
  // see https://www.researchgate.net/publication/320555036_Particle_Distribution_Dependent_Inaccuracy_of_the_Plantower_PMS5003_low-cost_PM-sensor
  int pm25 = data.pm25_env;
  if (pm25_min > pm25) pm25_min = pm25;
  if (pm25_max < pm25) pm25_max = pm25;
  tft.fillScreen(ST77XX_BLACK);
  tft.setCursor(0, 0);
  tft.print("PM 2.5: ");
  tft.println(pm25);
  tft.println();
  tft.print(pm25_min);
  tft.print(", ");
  tft.println(pm25_max);
  tft.println();
  tft.print(data.particles_03um); tft.print(',');
  tft.print(data.particles_05um); tft.print(',');
  tft.println(data.particles_10um);

  delay(30000);
}

I bought a new sensor and its now working, but thank you for the help