Migrating PMSV7003 Sensor Code from Arduino Uno to Giga: SoftwareSerial Compatibility Issues

Good afternoon, everyone. I am trying to connect the PMS7003 sensor to the Arduino Giga. The code I researched works with Arduino Uno but doesn't work with Arduino Giga. The issue is with the <SoftwareSerial.h> library, which is incompatible with Arduino Giga. I would like to know what alternative I can use for an mbed_giga architecture.
Note that I've already tried the AltSoftSerial, NeoSWSerial, hardware serial libraries, and they don't work.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2,3); // RX, TX
unsigned int pm1 = 0;
unsigned int pm2_5 = 0;
unsigned int pm10 = 0;
void setup() {
Serial.begin(9600);
while (!Serial) ;
mySerial.begin(9600);
}
 
void loop() {
int index = 0;
char value;
char previousValue;
 
while (mySerial.available()) {
value = mySerial.read();
if ((index == 0 && value != 0x42) || (index == 1 && value != 0x4d)){
Serial.println("Cannot find the data header.");
break;
}
 
if (index == 4 || index == 6 || index == 8 || index == 10 || index == 12 || index == 14) {
previousValue = value;
}
else if (index == 5) {
pm1 = 256 * previousValue + value;
Serial.print("{ ");
Serial.print("\"pm1\": ");
Serial.print(pm1);
Serial.print(" ug/m3");
Serial.print(", ");
}
else if (index == 7) {
pm2_5 = 256 * previousValue + value;
Serial.print("\"pm2_5\": ");
Serial.print(pm2_5);
Serial.print(" ug/m3");
Serial.print(", ");
}
else if (index == 9) {
pm10 = 256 * previousValue + value;
Serial.print("\"pm10\": ");
Serial.print(pm10);
Serial.print(" ug/m3");
} else if (index > 15) {
break;
}
index++;
}
while(mySerial.available()) mySerial.read();
Serial.println(" }");
delay(1000);
}

Please do not post in "Uncategorized"; see the sticky topics in Uncategorized - Arduino Forum.

Your topic has been moved.

What does that mean? Does it not compile? Does it not do what you expect it to do?

I'm not familiar with the Giga but it has multiple hardware serial ports. Unless you have a very good reason not to use them, use one of the (other) the hardware serial ports

If the problem is a compile problem, you can try to use e.g. Serial1 (pins 18 and 19). The most simple change for the given code for that would be

//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(2,3); // RX, TX
#define mySerial Serial1
unsigned int pm1 = 0;
unsigned int pm2_5 = 0;
unsigned int pm10 = 0;
void setup() {
Serial.begin(9600);
while (!Serial) ;
mySerial.begin(9600);
}

Not compiled.

1 Like

I'm sorry, I won't make that mistake again with the categories and thank you, my code is working now

  
  // la conexión se realiza de la siguiente manera
  // 5 V - > vcc    GND - > gnd   Pin 18 - > tx    Pin 19 - > rx
  // consultar los puertos uarts https://docs.arduino.cc/tutorials/giga-r1-wifi/cheat-sheet
  //#include <SoftwareSerial.h>
  //SoftwareSerial mySerial(2,3); // RX, TX
  
  #define mySerial Serial2
  
  unsigned int pm1 = 0;
  unsigned int pm2_5 = 0;
  unsigned int pm10 = 0;
  void setup() {
    Serial.begin(9600);
  
  mySerial.begin(9600);
  
  }
    
  void loop() {
  int index = 0;
  char value;
  char previousValue;
   
  while (mySerial.available()) {
  value = mySerial.read();
  if ((index == 0 && value != 0x42) || (index == 1 && value != 0x4d)){
  Serial.println("Cannot find the data header.");
  break;
  }
   
  if (index == 4 || index == 6 || index == 8 || index == 10 || index == 12 || index == 14) {
  previousValue = value;
  }
  else if (index == 5) {
  pm1 = 256 * previousValue + value;
  Serial.print("{ ");
  Serial.print("\"pm1\": ");
  Serial.print(pm1);
  Serial.print(" ug/m3");
  Serial.print(", ");
  }
  else if (index == 7) {
  pm2_5 = 256 * previousValue + value;
  Serial.print("\"pm2_5\": ");
  Serial.print(pm2_5);
  Serial.print(" ug/m3");
  Serial.print(", ");
  }
  else if (index == 9) {
  pm10 = 256 * previousValue + value;
  Serial.print("\"pm10\": ");
  Serial.print(pm10);
  Serial.print(" ug/m3");
  } else if (index > 15) {
  break;
  }
  index++;
  }
  while(mySerial.available()) mySerial.read();
  Serial.println(" }");
  delay(1000);
  }

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