|
| 1 | +/* |
| 2 | + Standalone Sketch to use with a Arduino Fio and a |
| 3 | + Sharp Optical Dust Sensor GP2Y1010AU0F |
| 4 | + |
| 5 | + Blog: http://arduinodev.woofex.net/2012/12/01/standalone-sharp-dust-sensor/ |
| 6 | + Code: https://github.com/Trefex/arduino-airquality/ |
| 7 | + For Pin connections, please check the Blog or the github project page |
| 8 | + Authors: Cyrille Médard de Chardon (serialC), Christophe Trefois (Trefex) |
| 9 | + Changelog: |
| 10 | + 2012-Dec-01: Cleaned up code |
| 11 | + 2012-Dec-13: Converted mg/m3 to ug/m3 which seems to be the accepted standard |
| 12 | + This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. |
| 13 | + To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter |
| 14 | + to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA. |
| 15 | + */ |
| 16 | + |
| 17 | +#define VOLTAGE 5.0 // Arduino supply voltage |
| 18 | + |
| 19 | +int measurePin = A0; |
| 20 | +int ledPower = 10; |
| 21 | + |
| 22 | +int samplingTime = 280; |
| 23 | +int deltaTime = 40; |
| 24 | +int sleepTime = 9680; |
| 25 | + |
| 26 | +float voMeasured = 0; |
| 27 | +float calcVoltage = 0; |
| 28 | +float dustDensity = 0; |
| 29 | + |
| 30 | +const int numReadings = 100; // samples are taken at 100Hz so calculate average over 1sec |
| 31 | + |
| 32 | +int readings[numReadings]; // the readings from the analog input |
| 33 | +int readIndex = 0; // the index of the current reading |
| 34 | +long int total = 0; // the running total |
| 35 | +int latest_reading = 0; // the latest reading |
| 36 | +int average_reading = 0; // the average reading |
| 37 | + |
| 38 | +void setup(){ |
| 39 | + Serial.begin(115200); |
| 40 | + pinMode(ledPower,OUTPUT); |
| 41 | + // Initialise sample buffer |
| 42 | + for (int thisReading = 0; thisReading < numReadings; thisReading++) { |
| 43 | + readings[thisReading] = 0; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// Dust sample interrupt service routine |
| 48 | +void takeReading() { |
| 49 | + // subtract the last reading: |
| 50 | + total = total - readings[readIndex]; |
| 51 | + // read from the sensor: |
| 52 | + latest_reading = analogRead(dustPin); |
| 53 | + readings[readIndex] = latest_reading; |
| 54 | + // add the reading to the total: |
| 55 | + total = total + latest_reading; |
| 56 | + // advance to the next position in the array: |
| 57 | + readIndex = readIndex + 1; |
| 58 | + |
| 59 | + // if we're at the end of the array...wrap around to the beginning: |
| 60 | + if (readIndex >= numReadings) readIndex = 0; |
| 61 | + |
| 62 | + // calculate the average: |
| 63 | + average_reading = total / numReadings; // Seems to work OK with integer maths - but total does need to be long int |
| 64 | +} |
| 65 | + |
| 66 | +uint32_t timer = millis(); |
| 67 | + |
| 68 | +void loop(){ |
| 69 | + // if millis() or timer wraps around, we'll just reset it |
| 70 | + if (timer > millis()) timer = millis(); |
| 71 | + // approximately every second or so, print out the dust reading |
| 72 | + if (millis() - timer > 1000) { |
| 73 | + timer = millis(); // reset the timer |
| 74 | + digitalWrite(ledPower,LOW); // power on the LED |
| 75 | + delayMicroseconds(samplingTime); |
| 76 | + |
| 77 | + //voMeasured = analogRead(measurePin); // read the dust value |
| 78 | + |
| 79 | + delayMicroseconds(deltaTime); |
| 80 | + digitalWrite(ledPower,HIGH); // turn the LED off |
| 81 | + delayMicroseconds(sleepTime); |
| 82 | + |
| 83 | + // 0 - 5.0V mapped to 0 - 1023 integer values |
| 84 | + calcVoltage = latest_reading * (VOLTAGE / 1023.0); |
| 85 | + |
| 86 | + // linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/ |
| 87 | + // Chris Nafis (c) 2012 |
| 88 | + dustDensity = (0.17 * calcVoltage - 0.1)*1000; |
| 89 | + |
| 90 | + Serial.print("delayMu(): "); |
| 91 | + Serial.print("Voltage: "); |
| 92 | + Serial.print(latest_reading); |
| 93 | + |
| 94 | + Serial.print("\t - A0: "); |
| 95 | + Serial.print(calcVoltage); |
| 96 | + |
| 97 | + Serial.print(" - Dust Density [ug/m3]: "); |
| 98 | + Serial.println(dustDensity); |
| 99 | + } |
| 100 | +} |
0 commit comments