/ GP2Y1014AU0F_demos / GP2Y1014AU / GP2Y1014AU.ino
GP2Y1014AU.ino
 1  // SPDX-FileCopyrightText: 2020 Limor Fried for Adafruit Industries
 2  //
 3  // SPDX-License-Identifier: MIT
 4  
 5  #define dustPin A0 // Black wire
 6  #define ledPin 5  // White wire
 7  
 8  #define ANALOG_VOLTAGE 3.3 // analog top of range
 9  
10  // Yellow, Green wire connects to ground
11  // Blue wire connects to 5V via 150 ohm resistor (see DS)
12  // Red wire connects to 5V
13   
14  void setup()
15  {
16    Serial.begin(115200);
17    while (!Serial) delay(100);
18    
19    Serial.println("GP2Y1010AU0F Sensor demo");
20    pinMode(ledPin, OUTPUT);
21  }
22   
23   
24  void loop()
25  {
26    float output_voltage, dust_density;
27    
28    digitalWrite(ledPin, LOW); // power on the LED
29    delayMicroseconds(280); // Wait 0.28ms according to DS
30    // take analog reading
31    output_voltage = analogRead(dustPin); 
32    delay(1);
33    
34    digitalWrite(ledPin, HIGH); // turn the LED off
35  
36    output_voltage = (output_voltage / 1023) * ANALOG_VOLTAGE;
37    dust_density = (0.18 * output_voltage) - 0.1;
38  
39    Serial.print("Voltage = ");
40    Serial.print(output_voltage);
41    Serial.print(",\tDust Density = ");
42    Serial.print(dust_density);
43    Serial.println(" mg/m3");
44    delay(1000);
45  }