Problem with 2.5 dust sensor

Dear Arduino Community,

I am new to Arduino and this is my first forum post. I am hoping that the community might be able to help with a problem that I have recently encountered.

In my project I would like to display the measurements of this 2.5 laser dust sensor online through my Arduino Uno Wifi Rev2:
https://wiki.dfrobot.com/PM2.5_laser_dust_sensor_SKU_SEN0177

When I upload the sample code to my Arduino Uno (using Software Serial), the programme works very well and I can see the correct measurements in the serial monitor. When I upload the sample code to my Arduino Uno Wifi Rev2 the programme only prints zeros instead of the real measurement values:

16:55:48.796 -> PM1.0: 0 ug/m3
16:55:48.829 -> PM2.5: 0 ug/m3
16:55:48.829 -> PM1 0: 0 ug/m3

16:55:48.864 ->
16:55:50.293 -> PM1.0: 0 ug/m3
16:55:50.327 -> PM2.5: 0 ug/m3
16:55:50.327 -> PM1 0: 0 ug/m3

etc.

What could be the problem?

A big thank you to the community in advance, Chris

Guessing, you have a pin conflict. Compare the pins that the wifi component requires with the ones your dust sensor uses.

FYI: Unlike the classic Uno, the Uno WiFi Rev2 has a spare hardware serial port (Serial1) on pins 0 and 1. Hardware serial is superior to software serial. So the only good reason for using software serial would be if you already were using Serial1 to communicate with another device.

However, you should be able to use software serial, no problem. I don't know what the difference could be between the two boards that would cause the issue you describe. I had a look at the code at the link you shared and didn't spot any problems.

Dear wildbill and port,

Thank you so much for your answers.

I have tried to change the code to use Serial1, but now I am getting the following error code when I try to upload the sketch:

Arduino: 1.8.12 (Windows 10), Board: “Arduino Uno WiFi Rev2, ATMEGA328”

Sketch uses 3549 bytes (7%) of program storage space. Maximum is 48640 bytes.
Global variables use 224 bytes (3%) of dynamic memory, leaving 5920 bytes for local variables. Maximum is 6144 bytes.
avrdude: Short read, read only 0 out of 64 bytes
avrdude: jtag3_edbg_recv(): Unexpected response 0x12
avrdude: retrying with external reset applied
avrdude: jtag3_edbg_send(): Unexpected response 0x81, 0x11
avrdude: jtag3_edbg_recv(): Unexpected response 0x80
avrdude: retrying with external reset applied
avrdude: JTAGEN fuse disabled?
avrdude: initialization failed, rc=-1
Double check connections and try again, or use -F to override
this check.
An error occurred while uploading the sketch

avrdude: jtag3_edbg_send(): Unexpected response 0x81, 0x11
avrdude: jtag3_edbg_recv(): Unexpected response 0x80
avrdude: jtag3_edbg_send(): Unexpected response 0x81, 0x11
avrdude: jtag3_edbg_recv(): Unexpected response 0x80
avrdude: jtag3_edbg_signoff(): unexpected response 0x81, 0x11
avrdude: jtag3_edbg_signoff(): unexpected response 0x01, 0x00

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

Here the full code:

#include <Arduino.h>
#define LENG 31   //0x42 + 31 bytes equal to 32 bytes
unsigned char buf[LENG];

int PM01Value=0;          //define PM1.0 value of the air detector module
int PM2_5Value=0;         //define PM2.5 value of the air detector module
int PM10Value=0;         //define PM10 value of the air detector module


void setup()
{
 Serial1.begin(9600);   //use serial0
 Serial1.setTimeout(1500);    //set the Timeout to 1500ms, longer than the data transmission periodic time of the sensor

}

void loop()
{
 if(Serial1.find(0x42)){    //start to read when detect 0x42
   Serial1.readBytes(buf,LENG);

   if(buf[0] == 0x4d){
     if(checkValue(buf,LENG)){
       PM01Value=transmitPM01(buf); //count PM1.0 value of the air detector module
       PM2_5Value=transmitPM2_5(buf);//count PM2.5 value of the air detector module
       PM10Value=transmitPM10(buf); //count PM10 value of the air detector module
     }
   }
 }

 static unsigned long OledTimer=millis();
   if (millis() - OledTimer >=1000)
   {
     OledTimer=millis();

     Serial1.print("PM1.0: ");
     Serial1.print(PM01Value);
     Serial1.println("  ug/m3");

     Serial1.print("PM2.5: ");
     Serial1.print(PM2_5Value);
     Serial1.println("  ug/m3");

     Serial1.print("PM1 0: ");
     Serial1.print(PM10Value);
     Serial1.println("  ug/m3");
     Serial1.println();
   }

}
char checkValue(unsigned char *thebuf, char leng)
{
 char receiveflag=0;
 int receiveSum=0;

 for(int i=0; i<(leng-2); i++){
 receiveSum=receiveSum+thebuf[i];
 }
 receiveSum=receiveSum + 0x42;

 if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1]))  //check the serial data
 {
   receiveSum = 0;
   receiveflag = 1;
 }
 return receiveflag;
}

int transmitPM01(unsigned char *thebuf)
{
 int PM01Val;
 PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module
 return PM01Val;
}

//transmit PM Value to PC
int transmitPM2_5(unsigned char *thebuf)
{
 int PM2_5Val;
 PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module
 return PM2_5Val;
 }

//transmit PM Value to PC
int transmitPM10(unsigned char *thebuf)
{
 int PM10Val;
 PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module
 return PM10Val;
}

Any other idea that could come to mind about why it is not working?

Thank you so, so much, regards, Chris