-
Notifications
You must be signed in to change notification settings - Fork 27
/
sharp_gp2y1023au0f_demo.ino
93 lines (79 loc) · 2.71 KB
/
sharp_gp2y1023au0f_demo.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/////////////////////////////////////////////////////////////////////////////
// Sharp GP2Y1023AU0F Dust Sensor Demo
//
// Board Connection:
// GP2Y1023AU0F Arduino
// Pin 1 - Vo Pin 7
// Pin 2 - LED-GND C1(-) and GND
// Pin 3 - GND GND
// Pin 4 - VLED C1(+) and R1
// Pin 5 - Vcc 5V
//
// C1 = 220 uF capacitor
// R1 = 150 ohm resistor
//
// Serial monitor setting:
// 9600 baud
//
// References:
// http://www.socle-tech.com.tw/doc/IC%20Channel%20Product/SHARP_GP2Y1023AU0F.pdf
// https://github.com/sharpsensoruser/sharp-sensor-demos/blob/master/docs/Sharp%20GP2Y1010AU0F%20-%20Dust%20Density%20Conversion.pdf
/////////////////////////////////////////////////////////////////////////////
// Arduino pin numbers.
const int pwmInputPin = 7; // Arduino digital pin 7 connect to sensor Vo.
// Set the typical output voltage in microseconds when there is zero dust.
unsigned long Voc = 1400;
// Use the typical sensitivity in units of microseconds per 100ug/m3.
const float K = 1400;
/////////////////////////////////////////////////////////////////////////////
// Helper functions to print a data value to the serial monitor.
void printValue(String text, unsigned long value, bool isLast = false) {
Serial.print(text);
Serial.print("=");
Serial.print(value);
if (!isLast) {
Serial.print(", ");
}
}
void printFValue(String text, float value, String units, bool isLast = false) {
Serial.print(text);
Serial.print("=");
Serial.print(value);
Serial.print(units);
if (!isLast) {
Serial.print(", ");
}
}
/////////////////////////////////////////////////////////////////////////////
// Arduino setup function.
void setup() {
// Set pin for input.
pinMode(pwmInputPin, INPUT);
// Start the hardware serial port for the serial monitor.
Serial.begin(9600);
// Wait two seconds for startup.
delay(2000);
Serial.println("");
Serial.println("GP2Y1023AU0F Demo");
Serial.println("=================");
}
// Arduino main loop.
void loop() {
// Get the pulse width in microseconds. This is the Vo reading.
unsigned long timeoutUS = 15000;
unsigned long Vo = pulseIn(pwmInputPin, LOW, timeoutUS);
if ( Vo == 0 )
return;
printValue("Vo", Vo);
// Offset adjustment.
if ( Vo < Voc )
Voc = Vo;
printValue("Voc", Voc);
// Convert to Dust Density in units of ug/m3. Note that GP2Y1023 model
// is high sensitivity device and densities beyond 250 ug/m3 are not
// within the usable range.
float dustDensity = (Vo - Voc) / K * 100.0;
printFValue("DustDensity", dustDensity, "ug/m3", true);
Serial.println("");
delay(500);
} // END PROGRAM