New to Arduino and cant get two sensors to output to serial monitor correctly

My apologies if I am not formatting my message correctly. I am trying to get several air quality sensors to work, but I am having trouble getting them to work at the same time. Individually both codes output properly though. whenever I run the program it provides insane values and then does 2 checksum errors and then outputs insane values again. I have all my RX and TX lines from each sensor hooked up to the original RX and TX pin on the arduino. Should I create new pins for RX and TX for each sensor?

// Include Libraries 
#include <DHT.h>
#include <SoftwareSerial.h> 

// Create Constants 
#define DHTPIN 6                                // What pin we are connected to on teh Arduino Board (DIGITAL) 
#define DHTTYPE DHT22                           // What DHT Sensor we are using 
DHT dht(DHTPIN, DHTTYPE);

SoftwareSerial pmsSerial(0,1);

// Initalize Variables                          
int chk;
float HUM;                                      // Store the humidity value 
float TMP;                                      // Store the temperature value 


void setup() {                                  // Place all code that needs to run just once here
  
// Debugging Output 
Serial.begin(9600); 

// Sensor baud rate set to 9600
pmsSerial.begin(9600); 

// DHT sensor baud rate is also 96000
dht.begin();
}

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() {                                                // Code that needs to be run repeatedly 
  if (readPMSdata(&pmsSerial)) {
    // reading data was successful!

HUM = dht.readHumidity();                                    // Read Humidity and store it in variable HUM
TMP = dht.readTemperature();                                 // Read Temperature and store it in variable TMP



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

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];
  }
 
  /* debugging
  for (uint8_t i=2; i<32; i++) {
    Serial.print("0x"); Serial.print(buffer[i], HEX); Serial.print(", ");
  }
  Serial.println();
  */
  
  // 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;
}

and what is incorrect?

Yes

1 Like

How would I go about doing that? Should I use digital pins and then define them as either input or output?

First of all don't use pins 0 and 1 to communicate with your sensors, those pins are used for your board's native communication, use pins 2 and up.
Create as many communication ports as you need:

SoftwareSerial pmsSerial_1(2,3);
SoftwareSerial pmsSerial_2(4,5);
etc....

And then you read the one that interests you:

if (readPMSdata(&pmsSerial_1)) {
if (readPMSdata(&pmsSerial_2)) {
etc...
1 Like

What type of Arduino are you using?

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