arduino code for dust sensor
arduino code for dust sensor
V-LED: This is the power pin for the LED. Connect it to the 5V pin of the Arduino using a
150Ω current-limiting resistor.
LED-GND: This is the ground pin for the LED. Connect it to the Arduino's ground pin.
LED: This pin controls the LED and can be used to turn it on or off. Connect it to any digital
pin on the Arduino.
S-GND: This is the ground pin for the pulse dust sensor module and should be connected to
the ground pin of the Arduino.
VOUT: This is the output pin of the dust sensor module. Connect it to any of the Arduino's
analog pins.
VCC: This is the power pin of the dust sensor module. Connect it to either the 5V or 3.3V pin
of the Arduino.Arduino
GP2Y1014AU0F Dust Sensor
Circuit Connection Diagram
Now that we fully understand how the GP2Y1014AU0F Dust Sensor
operates, we can connect all the necessary wires to the Arduino and
write the code to retrieve data from the sensor module. The wiring
diagram for connecting the GP2Y1014AU0F Dust Sensor to the
Arduino is shown below.
We begin the code by defining the analog input pin for the sensor, as
well as a digital pin to control the LED. Then, we define three
variables to store the timing values needed to calculate the dust
concentration measured by the sensor. Additionally, we define three
float variables to hold the sensor's output results.
void setup(){
Serial.begin(9600);
pinMode(ledPower,OUTPUT);
}
Next is the loop function, where we begin the measurement process
as recommended by the datasheet. The entire measurement process
takes about 10ms, with a sampling period of 0.28ms. First, we set
the LED pin to low, which turns the LED on, and use the
delayMicroseconds() function to create a 280µS (0.28ms) delay, as
suggested by the datasheet. Then, we take our sample and
introduce another 40µS delay to ensure the pulse width remains at
0.32ms (320µS). Finally, we wait for the remaining 9680µS before
the next cycle.
Next, we calculate the voltage from the ADC value. Ensure that your
Arduino is powered by a proper +5V supply and not through USB, as
using USB power could lead to inaccurate results.