Use with a Nucleo F401RE and a Sharp Optical Dust Sensor GP2Y1010AU0F to measure dust with a Nokia 5110 display to show result.
main.cpp@0:99fdd85b4929, 2014-06-14 (annotated)
- Committer:
- kirchnet
- Date:
- Sat Jun 14 02:25:52 2014 +0000
- Revision:
- 0:99fdd85b4929
Use with a Nucleo F401RE and a Sharp Optical Dust Sensor GP2Y1010AU0F to measure dust with a Nokia 5110 display to show results.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kirchnet | 0:99fdd85b4929 | 1 | #include "mbed.h" |
kirchnet | 0:99fdd85b4929 | 2 | #include "NOKIA_5110.h" |
kirchnet | 0:99fdd85b4929 | 3 | |
kirchnet | 0:99fdd85b4929 | 4 | /* |
kirchnet | 0:99fdd85b4929 | 5 | Sketch to use with a Nucleo F401RE and a |
kirchnet | 0:99fdd85b4929 | 6 | Sharp Optical Dust Sensor GP2Y1010AU0F to measure dust with a |
kirchnet | 0:99fdd85b4929 | 7 | Nokia 5110 display to show result. Will also show results via USB on PC terminal program |
kirchnet | 0:99fdd85b4929 | 8 | |
kirchnet | 0:99fdd85b4929 | 9 | NOTE THAT THIS SENSOR USES 5V AND THE NUCLEO 3.3V. USE AT YOUR OWN RISK. |
kirchnet | 0:99fdd85b4929 | 10 | I SHALL NOT BE HELD RESPONSIBLE FOR ANY DAMAGE TO YOUR HARDWARE. |
kirchnet | 0:99fdd85b4929 | 11 | |
kirchnet | 0:99fdd85b4929 | 12 | For non-Nucleo mbed platforms you need to change the pins. |
kirchnet | 0:99fdd85b4929 | 13 | |
kirchnet | 0:99fdd85b4929 | 14 | !!!IMPORTANT!!! |
kirchnet | 0:99fdd85b4929 | 15 | A brief note on the sensor and board voltage !!!IMPORTANT!!! |
kirchnet | 0:99fdd85b4929 | 16 | STM specify in their datasheet (Feb 2014, DocID025644 Rev 2, page 38) that |
kirchnet | 0:99fdd85b4929 | 17 | pin PA1 is "FT" meaning 5V tolerant. Moreover, I have measured voltages of |
kirchnet | 0:99fdd85b4929 | 18 | no more than 3.71V in my experiments. So you should make your own decision |
kirchnet | 0:99fdd85b4929 | 19 | whether and how you want to connect the sensor. Use at your own risk. |
kirchnet | 0:99fdd85b4929 | 20 | I shall not be liable for any damage. |
kirchnet | 0:99fdd85b4929 | 21 | |
kirchnet | 0:99fdd85b4929 | 22 | This sketch is based on code by the following authors: Cyrille Médard de Chardon (serialC), Christophe Trefois (Trefex) |
kirchnet | 0:99fdd85b4929 | 23 | Their work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. |
kirchnet | 0:99fdd85b4929 | 24 | To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter |
kirchnet | 0:99fdd85b4929 | 25 | to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA. |
kirchnet | 0:99fdd85b4929 | 26 | Changelog: |
kirchnet | 0:99fdd85b4929 | 27 | 2014-Jun-08: Adapted to Nucleo |
kirchnet | 0:99fdd85b4929 | 28 | 2014-Jun-13: Added Nokia 5110 display |
kirchnet | 0:99fdd85b4929 | 29 | |
kirchnet | 0:99fdd85b4929 | 30 | Original documentation |
kirchnet | 0:99fdd85b4929 | 31 | http://arduinodev.woofex.net/2012/12/01/standalone-sharp-dust-sensor/ |
kirchnet | 0:99fdd85b4929 | 32 | http://www.howmuchsnow.com/arduino/airquality/ |
kirchnet | 0:99fdd85b4929 | 33 | |
kirchnet | 0:99fdd85b4929 | 34 | The code for the Nokia 5110 is adapted from work by Chris Yan as Revised: January, 2014 |
kirchnet | 0:99fdd85b4929 | 35 | |
kirchnet | 0:99fdd85b4929 | 36 | For the scientifically inclined who are pressed for time there is a nice writeup about dust here: |
kirchnet | 0:99fdd85b4929 | 37 | http://www.who.int/occupational_health/publications/en/oehairbornedust3.pdf |
kirchnet | 0:99fdd85b4929 | 38 | |
kirchnet | 0:99fdd85b4929 | 39 | Pin connections |
kirchnet | 0:99fdd85b4929 | 40 | Sharp dust sensor GP2Y1010AU0F |
kirchnet | 0:99fdd85b4929 | 41 | Data sheet: https://www.sparkfun.com/datasheets/Sensors/gp2y1010au_e.pdf |
kirchnet | 0:99fdd85b4929 | 42 | App note: http://sharp-world.com/products/device/lineup/data/pdf/datasheet/gp2y1010au_appl_e.pdf |
kirchnet | 0:99fdd85b4929 | 43 | Remember to include the resistor and capacitor (polarity!) as described in the Sharp datasheet |
kirchnet | 0:99fdd85b4929 | 44 | Pin 1: Resistor - 5V (see datasheet) |
kirchnet | 0:99fdd85b4929 | 45 | Pin 2: Gnd (capacitor between 1 and 2 - see datasheet) |
kirchnet | 0:99fdd85b4929 | 46 | Pin 3: Digital D8 |
kirchnet | 0:99fdd85b4929 | 47 | Pin 4: Gnd |
kirchnet | 0:99fdd85b4929 | 48 | Pin 5: +5V |
kirchnet | 0:99fdd85b4929 | 49 | Pin 6: Analog A0 |
kirchnet | 0:99fdd85b4929 | 50 | Pins are counted left to right when looking at the connector from the front (i.e. so that you can see the pins) |
kirchnet | 0:99fdd85b4929 | 51 | |
kirchnet | 0:99fdd85b4929 | 52 | 5110 Display |
kirchnet | 0:99fdd85b4929 | 53 | myPins.sce = PB_6; |
kirchnet | 0:99fdd85b4929 | 54 | myPins.rst = D2; |
kirchnet | 0:99fdd85b4929 | 55 | myPins.dc = D3; |
kirchnet | 0:99fdd85b4929 | 56 | myPins.mosi = PA_7;//SPI_MOSI; |
kirchnet | 0:99fdd85b4929 | 57 | myPins.miso = NC; |
kirchnet | 0:99fdd85b4929 | 58 | myPins.sclk = PA_5;//SPI_SCK; |
kirchnet | 0:99fdd85b4929 | 59 | I am not using the backlight. Feel free use another digital pin to power it |
kirchnet | 0:99fdd85b4929 | 60 | */ |
kirchnet | 0:99fdd85b4929 | 61 | |
kirchnet | 0:99fdd85b4929 | 62 | Serial pc(SERIAL_TX, SERIAL_RX); //output for debugging |
kirchnet | 0:99fdd85b4929 | 63 | |
kirchnet | 0:99fdd85b4929 | 64 | DigitalOut myled(LED1); |
kirchnet | 0:99fdd85b4929 | 65 | DigitalOut ledPower(D8); |
kirchnet | 0:99fdd85b4929 | 66 | AnalogIn analog_value(A0); |
kirchnet | 0:99fdd85b4929 | 67 | |
kirchnet | 0:99fdd85b4929 | 68 | |
kirchnet | 0:99fdd85b4929 | 69 | int samplingTime = 280;//280 microseconds |
kirchnet | 0:99fdd85b4929 | 70 | int deltaTime = 40;//40 us to give total pulse width of 0.32ms |
kirchnet | 0:99fdd85b4929 | 71 | int sleepTime = 9680;//LED off for 9680 us to take 1 measurement per second |
kirchnet | 0:99fdd85b4929 | 72 | |
kirchnet | 0:99fdd85b4929 | 73 | float dustDensityCN = 0,dustDensitySharp = 0, voMeasured=0, voCalc=0; |
kirchnet | 0:99fdd85b4929 | 74 | |
kirchnet | 0:99fdd85b4929 | 75 | int main() { |
kirchnet | 0:99fdd85b4929 | 76 | pc.printf("Starting sensor. It can take a few measurements until it becomes stable.\n"); |
kirchnet | 0:99fdd85b4929 | 77 | LcdPins myPins; |
kirchnet | 0:99fdd85b4929 | 78 | myPins.sce = PB_6; |
kirchnet | 0:99fdd85b4929 | 79 | myPins.rst = D2; |
kirchnet | 0:99fdd85b4929 | 80 | myPins.dc = D3; |
kirchnet | 0:99fdd85b4929 | 81 | myPins.mosi = PA_7;//SPI_MOSI; |
kirchnet | 0:99fdd85b4929 | 82 | myPins.miso = NC; |
kirchnet | 0:99fdd85b4929 | 83 | myPins.sclk = PA_5;//SPI_SCK; |
kirchnet | 0:99fdd85b4929 | 84 | pc.printf("LCD pins set ok\n"); |
kirchnet | 0:99fdd85b4929 | 85 | NokiaLcd myLcd( myPins ); |
kirchnet | 0:99fdd85b4929 | 86 | |
kirchnet | 0:99fdd85b4929 | 87 | // Start the LCD |
kirchnet | 0:99fdd85b4929 | 88 | myLcd.InitLcd(); |
kirchnet | 0:99fdd85b4929 | 89 | myLcd.SetXY(char(0),char(0)); |
kirchnet | 0:99fdd85b4929 | 90 | myLcd.DrawString("SHARP DUST SENSOR"); |
kirchnet | 0:99fdd85b4929 | 91 | |
kirchnet | 0:99fdd85b4929 | 92 | pc.printf("LCD started\n"); |
kirchnet | 0:99fdd85b4929 | 93 | |
kirchnet | 0:99fdd85b4929 | 94 | while(1) { |
kirchnet | 0:99fdd85b4929 | 95 | myled = !myled; |
kirchnet | 0:99fdd85b4929 | 96 | ledPower=0; // power on the LED. Pull-down to activate |
kirchnet | 0:99fdd85b4929 | 97 | wait_us(samplingTime); |
kirchnet | 0:99fdd85b4929 | 98 | voMeasured = analog_value.read(); // Converts and read the analog input value |
kirchnet | 0:99fdd85b4929 | 99 | wait_us(deltaTime); |
kirchnet | 0:99fdd85b4929 | 100 | ledPower=1; // turn the LED off. Pull up to turn off |
kirchnet | 0:99fdd85b4929 | 101 | wait_us(sleepTime); |
kirchnet | 0:99fdd85b4929 | 102 | |
kirchnet | 0:99fdd85b4929 | 103 | voCalc = voMeasured*3.3;//Map 0:1 measured range to 0:3.3V |
kirchnet | 0:99fdd85b4929 | 104 | |
kirchnet | 0:99fdd85b4929 | 105 | // Original equation taken from Sharp data sheet measured in mg/m3 |
kirchnet | 0:99fdd85b4929 | 106 | // Sharp don't give you a best fit line, so you have to guess |
kirchnet | 0:99fdd85b4929 | 107 | dustDensitySharp = 0.5/2.8 * (float(voCalc) - 0.7); |
kirchnet | 0:99fdd85b4929 | 108 | |
kirchnet | 0:99fdd85b4929 | 109 | // Eqaution calibrated by Chris Nafis (c) 2012 |
kirchnet | 0:99fdd85b4929 | 110 | // see http://www.howmuchsnow.com/arduino/airquality/ |
kirchnet | 0:99fdd85b4929 | 111 | // measured in parts per 0.01 cf |
kirchnet | 0:99fdd85b4929 | 112 | // [I did not get meaningful values on my sensor with Chris' formula |
kirchnet | 0:99fdd85b4929 | 113 | // For me the Sharp graph works just fine. So make your own tests] |
kirchnet | 0:99fdd85b4929 | 114 | dustDensityCN = (float(voCalc) - 0.0356)*1.2; |
kirchnet | 0:99fdd85b4929 | 115 | |
kirchnet | 0:99fdd85b4929 | 116 | pc.printf(" - Measurment value: %1.3f", voMeasured); |
kirchnet | 0:99fdd85b4929 | 117 | pc.printf(" - Voltage calculated: %1.3f", voCalc); |
kirchnet | 0:99fdd85b4929 | 118 | pc.printf(" - Sharp's Dust Density [mg/m3]: %f", dustDensitySharp); |
kirchnet | 0:99fdd85b4929 | 119 | pc.printf(" - C. Nafis' Dust Density [pp.01cf](x10^4): %f\n", dustDensityCN,"\n"); |
kirchnet | 0:99fdd85b4929 | 120 | |
kirchnet | 0:99fdd85b4929 | 121 | myLcd.SetXY(char(1),char(1)); |
kirchnet | 0:99fdd85b4929 | 122 | myLcd.DrawString("Raw value:"); |
kirchnet | 0:99fdd85b4929 | 123 | char measurestring[(((sizeof voMeasured) * 8) + 2)/3 + 2]; |
kirchnet | 0:99fdd85b4929 | 124 | sprintf(measurestring, "%1.2f", voMeasured); |
kirchnet | 0:99fdd85b4929 | 125 | myLcd.DrawString(measurestring); |
kirchnet | 0:99fdd85b4929 | 126 | |
kirchnet | 0:99fdd85b4929 | 127 | myLcd.SetXY(char(1),char(2)); |
kirchnet | 0:99fdd85b4929 | 128 | myLcd.DrawString("Voltage: "); |
kirchnet | 0:99fdd85b4929 | 129 | char voltagestring[(((sizeof voCalc) * 8) + 2)/3 + 2]; |
kirchnet | 0:99fdd85b4929 | 130 | sprintf(voltagestring, "%1.3f", voCalc); |
kirchnet | 0:99fdd85b4929 | 131 | myLcd.DrawString(voltagestring); |
kirchnet | 0:99fdd85b4929 | 132 | |
kirchnet | 0:99fdd85b4929 | 133 | myLcd.SetXY(char(1),char(3)); |
kirchnet | 0:99fdd85b4929 | 134 | myLcd.DrawString("Sharp dd: "); //Units: refer to printf above |
kirchnet | 0:99fdd85b4929 | 135 | char sharpstring[(((sizeof dustDensitySharp) * 8) + 2)/3 + 2]; |
kirchnet | 0:99fdd85b4929 | 136 | sprintf(sharpstring, "%1.2f", dustDensitySharp); |
kirchnet | 0:99fdd85b4929 | 137 | myLcd.DrawString(sharpstring); |
kirchnet | 0:99fdd85b4929 | 138 | |
kirchnet | 0:99fdd85b4929 | 139 | myLcd.SetXY(char(1),char(4)); |
kirchnet | 0:99fdd85b4929 | 140 | myLcd.DrawString("Nafis dd: "); //Units: refer to printf above |
kirchnet | 0:99fdd85b4929 | 141 | char nafisstring[(((sizeof dustDensityCN) * 8) + 2)/3 + 2]; |
kirchnet | 0:99fdd85b4929 | 142 | sprintf(nafisstring, "%1.2f", dustDensityCN); |
kirchnet | 0:99fdd85b4929 | 143 | myLcd.DrawString(nafisstring); |
kirchnet | 0:99fdd85b4929 | 144 | |
kirchnet | 0:99fdd85b4929 | 145 | wait(1); |
kirchnet | 0:99fdd85b4929 | 146 | } |
kirchnet | 0:99fdd85b4929 | 147 | } |