Thomas Kirchner / Mbed 2 deprecated DustSensorDisplay5110

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "NOKIA_5110.h"
00003 
00004 /*
00005 Sketch to use with a Nucleo F401RE and a
00006 Sharp Optical Dust Sensor GP2Y1010AU0F to measure dust with a
00007 Nokia 5110 display to show result. Will also show results via USB on PC terminal program
00008 
00009 NOTE THAT THIS SENSOR USES 5V AND THE NUCLEO 3.3V. USE AT YOUR OWN RISK.
00010 I SHALL NOT BE HELD RESPONSIBLE FOR ANY DAMAGE TO YOUR HARDWARE.
00011 
00012 For non-Nucleo mbed platforms you need to change the pins. 
00013 
00014 !!!IMPORTANT!!!
00015 A brief note on the sensor and board voltage   !!!IMPORTANT!!!
00016 STM specify in their datasheet (Feb 2014, DocID025644 Rev 2, page 38) that
00017 pin PA1 is "FT" meaning 5V tolerant. Moreover, I have measured voltages of
00018 no more than 3.71V in my experiments. So you should make your own decision
00019 whether and how you want to connect the sensor. Use at your own risk.
00020 I shall not be liable for any damage. 
00021 
00022 This sketch is based on code by the following authors: Cyrille Médard de Chardon (serialC), Christophe Trefois (Trefex)
00023 Their work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
00024 To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter
00025 to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
00026 Changelog:
00027 2014-Jun-08: Adapted to Nucleo
00028 2014-Jun-13: Added Nokia 5110 display
00029 
00030 Original documentation
00031 http://arduinodev.woofex.net/2012/12/01/standalone-sharp-dust-sensor/
00032 http://www.howmuchsnow.com/arduino/airquality/
00033 
00034 The code for the Nokia 5110 is adapted from work by Chris Yan as Revised: January, 2014
00035 
00036 For the scientifically inclined who are pressed for time there is a nice writeup about dust here:
00037 http://www.who.int/occupational_health/publications/en/oehairbornedust3.pdf
00038 
00039 Pin connections
00040 Sharp dust sensor GP2Y1010AU0F
00041 Data sheet: https://www.sparkfun.com/datasheets/Sensors/gp2y1010au_e.pdf
00042 App note:   http://sharp-world.com/products/device/lineup/data/pdf/datasheet/gp2y1010au_appl_e.pdf
00043 Remember to include the resistor and capacitor (polarity!) as described in the Sharp datasheet
00044     Pin 1: Resistor - 5V (see datasheet)
00045     Pin 2: Gnd (capacitor between 1 and 2 - see datasheet)
00046     Pin 3: Digital D8
00047     Pin 4: Gnd
00048     Pin 5: +5V
00049     Pin 6: Analog A0
00050     Pins are counted left to right when looking at the connector from the front (i.e. so that you can see the pins)
00051 
00052 5110 Display
00053     myPins.sce  = PB_6;
00054     myPins.rst  = D2;
00055     myPins.dc   = D3;
00056     myPins.mosi = PA_7;//SPI_MOSI;
00057     myPins.miso = NC;
00058     myPins.sclk = PA_5;//SPI_SCK;
00059 I am not using the backlight. Feel free use another digital pin to power it
00060 */
00061 
00062 Serial pc(SERIAL_TX, SERIAL_RX); //output for debugging
00063  
00064 DigitalOut myled(LED1);
00065 DigitalOut ledPower(D8);
00066 AnalogIn analog_value(A0);
00067 
00068 
00069 int samplingTime = 280;//280 microseconds
00070 int deltaTime = 40;//40 us to give total pulse width of 0.32ms
00071 int sleepTime = 9680;//LED off for 9680 us to take 1 measurement per second
00072 
00073 float dustDensityCN = 0,dustDensitySharp = 0, voMeasured=0, voCalc=0;
00074  
00075 int main() {
00076   pc.printf("Starting sensor. It can take a few measurements until it becomes stable.\n");
00077     LcdPins myPins;
00078     myPins.sce  = PB_6;
00079     myPins.rst  = D2;
00080     myPins.dc   = D3;
00081     myPins.mosi = PA_7;//SPI_MOSI;
00082     myPins.miso = NC;
00083     myPins.sclk = PA_5;//SPI_SCK;
00084     pc.printf("LCD pins set ok\n");
00085     NokiaLcd myLcd( myPins );
00086     
00087     // Start the LCD
00088     myLcd.InitLcd();
00089     myLcd.SetXY(char(0),char(0));
00090     myLcd.DrawString("SHARP DUST SENSOR"); 
00091 
00092     pc.printf("LCD started\n");
00093 
00094   while(1) { 
00095       myled = !myled;
00096       ledPower=0; // power on the LED. Pull-down to activate
00097       wait_us(samplingTime);
00098       voMeasured = analog_value.read(); // Converts and read the analog input value  
00099       wait_us(deltaTime);
00100       ledPower=1; // turn the LED off. Pull up to turn off
00101       wait_us(sleepTime);
00102 
00103       voCalc = voMeasured*3.3;//Map 0:1 measured range to 0:3.3V
00104   
00105   // Original equation taken from Sharp data sheet measured in mg/m3
00106   // Sharp don't give you a best fit line, so you have to guess
00107      dustDensitySharp = 0.5/2.8 * (float(voCalc) - 0.7);
00108 
00109   // Eqaution calibrated by Chris Nafis (c) 2012
00110   // see http://www.howmuchsnow.com/arduino/airquality/
00111   // measured in parts per 0.01 cf
00112   // [I did not get meaningful values on my sensor with Chris' formula
00113   // For me the Sharp graph works just fine. So make your own tests]
00114      dustDensityCN = (float(voCalc) - 0.0356)*1.2;
00115   
00116       pc.printf(" - Measurment value: %1.3f", voMeasured);
00117       pc.printf(" - Voltage calculated: %1.3f", voCalc);
00118       pc.printf(" - Sharp's Dust Density [mg/m3]: %f", dustDensitySharp);
00119       pc.printf(" - C. Nafis' Dust Density [pp.01cf](x10^4): %f\n", dustDensityCN,"\n");
00120       
00121         myLcd.SetXY(char(1),char(1));
00122         myLcd.DrawString("Raw value:"); 
00123         char measurestring[(((sizeof voMeasured) * 8) + 2)/3 + 2];
00124         sprintf(measurestring, "%1.2f", voMeasured);
00125         myLcd.DrawString(measurestring);
00126 
00127         myLcd.SetXY(char(1),char(2));
00128         myLcd.DrawString("Voltage:  "); 
00129         char voltagestring[(((sizeof voCalc) * 8) + 2)/3 + 2];
00130         sprintf(voltagestring, "%1.3f", voCalc);
00131         myLcd.DrawString(voltagestring);
00132 
00133         myLcd.SetXY(char(1),char(3));
00134         myLcd.DrawString("Sharp dd: "); //Units: refer to printf above
00135         char sharpstring[(((sizeof dustDensitySharp) * 8) + 2)/3 + 2];
00136         sprintf(sharpstring, "%1.2f", dustDensitySharp);
00137         myLcd.DrawString(sharpstring);
00138 
00139         myLcd.SetXY(char(1),char(4));
00140         myLcd.DrawString("Nafis dd: "); //Units: refer to printf above
00141         char nafisstring[(((sizeof dustDensityCN) * 8) + 2)/3 + 2];
00142         sprintf(nafisstring, "%1.2f", dustDensityCN);
00143         myLcd.DrawString(nafisstring);
00144  
00145   wait(1);
00146 }
00147 }