Example Code for Arduino-Dust Detection

Last revision 2025/12/26

Standalone Sketch to use with a Arduino UNO and a Sharp Optical Dust Sensor GP2Y1010AU0F. Users can learn how to interface the Sharp Optical Dust Sensor with Arduino UNO to read analog output and calculate dust density.

Hardware Preparation

Software Preparation

  • Development Tool: Arduino IDE, Version: Latest stable version, Download Link: Arduino Software (IDE)
  • Libraries: No additional libraries required.

Wiring Diagram

Sensor Pin Arduino Pin
Vled 5V (150ohm resistor)
LED-GND GND
LED Digital pin 2
S-GND GND
Vo Analog pin 0
Vcc 5V

Other Preparation Work

  1. Ensure all components are correctly wired according to the Wiring Diagram.
  2. Connect the Arduino UNO to your computer using a USB cable.
  3. Open the Arduino IDE, select Tools > Board > Arduino AVR Boards > Arduino UNO and Tools > Port (select the correct COM port for your Arduino).

Sample Code

/*!
 * @file  SEN0144.ino
 * @brief Standalone Sketch to use with a Arduino UNO and a
 * @n Sharp Optical Dust Sensor GP2Y1010AU0F
 * @copyright  Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @license  The MIT License (MIT)
 * @author  DFRobot
 * @version  V3.0
 * @date  2023-08-03
 */
/**user define**/
int voutPin = A0;  //Connect Vo of dust sensor Vo to Arduino A0 pin
int ledPin = 2;    //Connect LED(3pin) of dust sensor to Arduino D2 pin
/**system define**/
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
int voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
void setup()
{
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT);
}
void loop()
{
  digitalWrite(ledPin,LOW); // power on the LED
  delayMicroseconds(samplingTime);
  voMeasured = analogRead(voutPin); // read the dust value
  delayMicroseconds(deltaTime);
  digitalWrite(ledPin,HIGH); // turn the LED off
  delayMicroseconds(sleepTime);
  // 0 - 5V mapped to 0 - 1023 integer values
  // recover voltage
  calcVoltage = (float)voMeasured * (5.0 / 1024.0);
  // linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/
  // Chris Nafis (c) 2012
  if ( calcVoltage >= 0.6 ) {
      dustDensity = 0.17 * calcVoltage - 0.1;
  } else {
      dustDensity = 0;
  }
  Serial.print("Raw Signal Value (0-1023): ");
  Serial.print(voMeasured);
  Serial.print(" - Voltage: ");
  Serial.print(calcVoltage);
  Serial.print("V");
  Serial.print(" - Dust Density: ");
  if( calcVoltage > 3.5 ) {
     Serial.print(">");  // unit: mg/m3
  }
  Serial.print(dustDensity);
  Serial.println(" mg/m3");
  delay(1000);
}

Result

  • After uploading the code, open the Serial Monitor in Arduino IDE (set baud rate to 9600).
  • Expected output format: Raw Signal Value (0-1023): xxx - Voltage: x.xxV - Dust Density: x.xx mg/m3.
  • If calcVoltage > 3.5V, the output will show >x.xx mg/m3 (e.g., Raw Signal Value (0-1023): 716 - Voltage: 3.5V - Dust Density: >0.595 mg/m3).

Was this article helpful?

TOP