Sponsor Link:

UTSource.net Reviews

It is a trustworthy website for ordering electronic components with cheap price and excellent quality.

Mounting the circuit

This project's circuit is so simple that you won't even need a breadboard at all. The input dust sensor connects directly to the Arduino microcontroller with three jumper wires. As a precaution before starting, remove any electrical power source through your Arduino. Then, connect the yellow wire (signal) on the dust sensor to D8 (digital pin 8) on your Arduino board through the JST connector. After, connect the red wire (VCC/+) of the sensor's JST connector to the 5v (+5 volts) which is located on your Arduino power pins row. Lastly, connect the black wire (GND/-) from your Grove Dust Sensor, again, through the JST, to one of your Arduino's GND (-) pins. That is it for the hardware setup regarding this project! 

Arduino Grove - Dust Sensor (PPD42NS) Project Code

int pin = 8;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 2000; 
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;

void setup() {
  Serial.begin(9600);
  pinMode(8,INPUT);
  starttime = millis(); 
}

void loop() {
  duration = pulseIn(pin, LOW);
  lowpulseoccupancy = lowpulseoccupancy+duration;
  if ((millis()-starttime) >= sampletime_ms) //if the sampel time = = 30s
  {
    ratio = lowpulseoccupancy/(sampletime_ms*10.0);  
    concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; 
    Serial.print("Concentration = ");
    Serial.print(concentration);
    Serial.println(" pcs/0.01cf");
    Serial.println("\n");
    lowpulseoccupancy = 0;
    starttime = millis();
  }
}

About the code

This code consists of multiple functions which is really helpful in Arduino, especially when dealing with complex sensors and interfaces. This code starts off with declaring a pin for this sensor, which is D8 (digital pin 8) as an integer. For the next two lines, we declare extended size variables for number storage only. It can be used later to assign values or readings to that variable. We can write and store data in unsigned long variables such as data coming out from sensors, math calculations, readings over time, etc.. The variables in those two lines are: duration and starttime. Moving on, there are additionally two more unsigned long variables with a reading currently assigned (sampletime_ms and lowpulseoccupancy), it will be used further down the code. The benefit with these variables are that they can carry more data as they have extensive size, proving to be useful in gathering data. The following float variables (ratio and concentration) are for decimal numbers so with a pre-set value declared, it could be used to read decimal numbers later in this sketch. Now, we move onto the void setup section, beginning with setting up the serial communication to output data from the sensor at a rate of 9600 bauds. Followed by the next line, we set D8 (digital pin 8) as an input pin, so that data from the sensor will be sent to the Arduino microcontroller. After, we command the variable, starttime, to read the current time in milliseconds, so that the variable can be involved with timing, further down the lines. That is it for the setup section, and now, it is time to get into the void loop part. The first line of the void loop is one of the Advanced I/O functions where it reads the pulse, whether it is HIGH or LOW (1 or 0). In this code, the pulseIn function is set LOW, so this function will start timing the LOW pulse until it changes to HIGH. Following that function, the time collected will be assigned to the unsigned long variable of duration, reading the time in microseconds. Now, we declare the variable of lowpulseoccupancy to read its own reading itself, plus the readings of the variable duration. We have now come to the major part of the code, where we calculate and transform raw data into what we can read, units. The if statement says, that if the variable starttime, subtracted by the current, counting time, is greater than or equal to the value of the variable sampletime_ms (2000 milliseconds), it's programmed to do the rest of the code below that if statement. Subsequently, we make use of one of our past declared variables by using the variable ratio to assign it to a value, which is lowpulseoccupancy, which we came across, and, with a little bit of math, we can make a new reading assigned to that variable of ratio. So, variable ratio equals, lowpulseoccupancy (which was added to the readings of the variable duration), divided by variable sampletime_ms multiplied by 10, to get the integer percentage. Succeeding that, we convert our values we have calculated into a unit of pcs/0.01cf. Our variable we'll be using for this will be named concentration. Starting off, concentration consists of 1.1 multiplied by the value of the variable ratio to the power of 3. Then, that continuous equation is subtracted by 3.8, times the variable ratio to the power of 2. Further on, it is added by 520, multiplied by the variable ratio again, and finally, that consecutive reading is added on to 0.62. The pow() function, in the sketch, is used to raise an integer, value or a variable to the power of something else. As we end this code, we print the readings into the serial monitor, with the phrase "Concentration =" on the serial monitor before we print the variable concentration, because that variable converted the raw data from the sensor into a suitable unit of measurement. Further more, on the same line on the serial monitor, we print the units of that value (pcs/0.01cf) to completely end the readings. So, the final readings on the serial monitor will read "Concentration = (reading) pcs/0.01cf". Lastly, the variable lowpulseoccupancy is reseted to 0 for a new reading to be gained as per the void loop, as well as the variable starttime (current time). Finally, those lines wrap up our code, it is short in length, but complicated when it's stripped apart.

Amazing opportunities

Also, be sure to check out PCBWay, a leading manufacturer and distributor in PCB design and manufacturing. They have amazing prices and excellent quality in their services, so don't miss out on them! Plus, PCBWay has an amazing website, online Gerber viewer function and a gift shop so make sure to check out their links below:

PCBWay Free Online Gerber Viewer Function:  https://www.pcbway.com/project/OnlineGerberViewer.html

PCBWay Gift Shop: https://www.pcbway.com/projects/gifts.html

Make sure you check out the review for this module by clicking here.

Enjoy! Contact us for any inquiries!