{"id":24,"date":"2016-09-23T00:40:55","date_gmt":"2016-09-22T23:40:55","guid":{"rendered":"http:\/\/connectranet.co.uk\/wp\/?p=24"},"modified":"2016-10-02T10:20:01","modified_gmt":"2016-10-02T09:20:01","slug":"sharp-dust-sensor-with-arduino-isr-code-to-correct-timing","status":"publish","type":"post","link":"https:\/\/connectranet.co.uk\/wp\/?p=24","title":{"rendered":"Sharp Dust Sensor with Arduino &#8211; ISR Code to correct timing"},"content":{"rendered":"<p>This code works on an Arduino Nano connected to a Sharp Dust Sensor and a 2 line LCD. It shows how to get the correct timing for the pulse that controls the LED in the sensor. The display shows the minimum and maximum voltages measured from the sensor output, in 5 second sampling periods.<\/p>\n<p>This version doesn&#8217;t do any conversion of the output voltage to a dust density.<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"padding-left: 30px;\">\/*\r\n Interface to Sharp GP2Y1010AU0F Particle Sensor\r\n Modified from Program by Christopher Nafis \r\n Written April 2012\r\n\r\n Changes (Sept 2016):\r\n LCD Display\r\n ISR code for ADC\r\n \r\n *\/\r\n#include &lt;LiquidCrystal.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nconst byte adcPin = 0;\r\n\/\/ Variables that can be changed by an ISR must be declared as volatile\r\nvolatile int adcReading;\r\nvolatile boolean adcDone;\r\nboolean adcStarted;\r\n\r\nint dustPin=0;\r\nint ledPower=2;\r\nint delayTime=280;\r\nint delayTime2=40;\r\nfloat offTime=9580;\r\nint dustVal=0;\r\nint i=0;\r\nint c=0;\r\nint adcMin=1023;\r\nint adcMax=0;\r\nfloat ppm=0.0;\r\nchar s[32];\r\nfloat voltage = 0.0;\r\nfloat vMin = 0.0;\r\nfloat vMax = 0.0;\r\nfloat dustdensity = 0.0;\r\nfloat ppmpercf = 0.0;\r\n\r\n\/\/ Define the pins used by the LCD\r\nLiquidCrystal lcd(7,8,9,10,11,12);\r\n\r\nvoid setup(){ \r\n pinMode(ledPower,OUTPUT);\r\n lcd.begin(16,2);\r\n lcd.print(\"Starting\");\r\n delay(1000);\r\n i=0;\r\n c=0;\r\n ppm=0.0;\r\n \/\/ set the analog reference (high two bits of ADMUX) and select the\r\n \/\/ channel (low 4 bits). this also sets ADLAR (left-adjust result)\r\n \/\/ to 0 (the default).\r\n ADMUX = bit (REFS0) | (adcPin &amp; 0x07);\r\n }\r\n\r\n\/\/ ADC complete Interupt Service Routine\r\nISR (ADC_vect)\r\n {\r\n byte low, high;\r\n \/\/ read the result registers and store value in adcReading\r\n \/\/ Must read ADCL first\r\n low = ADCL;\r\n high = ADCH;\r\n adcReading = (high &lt;&lt; 8) | low;\r\n adcDone = true; \r\n } \r\n\/\/ end of ADC_vect\r\n \r\n\r\nvoid loop(){\r\n do {\r\n i=i+1;\r\n digitalWrite(ledPower,LOW); \/\/ power on the LED\r\n delayMicroseconds(delayTime); \/\/ wait 280uS\r\n \/\/ Check the conversion hasn't been started already\r\n if (!adcStarted)\r\n {\r\n adcStarted = true;\r\n \/\/ start the conversion\r\n ADCSRA |= bit (ADSC) | bit (ADIE);\r\n } \r\n delayMicroseconds(delayTime2); \/\/ wait another 40uS or so to give 320uS pulse width\r\n digitalWrite(ledPower,HIGH); \/\/ turn the LED off\r\n \/\/ give the ADC time to complete then process the reading\r\n delayMicroseconds(100); \r\n \r\n if (adcDone) \r\n\/\/ adcDone is set to True by the ISR, called when the conversion is complete.\r\n {\r\n adcStarted = false;\r\n if (adcMin &gt; adcReading) adcMin = adcReading; \/\/ 5 second Minimum\r\n if (adcMax &lt; adcReading) adcMax = adcReading; \/\/ 5 second Maximum\r\n c = c+1; \/\/ count of readings\r\n adcDone = false;\r\n }\r\n \r\n delayMicroseconds(offTime); \/\/ wait for the remainder of the 10mS cycle time\r\n \r\n } while (i &lt;500); \/\/repeat for 5 seconds\r\n \r\n \/\/ Convert ADC max\/min readings in last 5 seconds (0-1023) to voltages\r\n vMin = adcMin * 5.0 \/ 1024;\r\n vMax = adcMax * 5.0 \/ 1024;\r\n String dataString = \"vMin:\";\r\n dataString += dtostrf(vMin, 9, 4, s);\r\n lcd.setCursor(0,1);\r\n lcd.print(dataString);\r\n dataString = \"vMax:\";\r\n dataString += dtostrf(vMax, 9, 4, s);\r\n lcd.setCursor(0,0);\r\n lcd.print(dataString);\r\n i=0;\r\n c=0;\r\n adcMin=1023;\r\n adcMax=0;\r\n \r\n }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This code works on an Arduino Nano connected to a Sharp Dust Sensor and a 2 line LCD. It shows how to get the correct timing for the pulse that controls the LED in the sensor. The display shows the minimum and maximum voltages measured from the sensor output, in 5 second sampling periods. This &hellip; <a href=\"https:\/\/connectranet.co.uk\/wp\/?p=24\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Sharp Dust Sensor with Arduino &#8211; ISR Code to correct timing&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[2,3],"tags":[],"class_list":["post-24","post","type-post","status-publish","format-standard","hentry","category-arduino","category-sharp-dust-sensor"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7Vhuy-o","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/connectranet.co.uk\/wp\/index.php?rest_route=\/wp\/v2\/posts\/24","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/connectranet.co.uk\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/connectranet.co.uk\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/connectranet.co.uk\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/connectranet.co.uk\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=24"}],"version-history":[{"count":5,"href":"https:\/\/connectranet.co.uk\/wp\/index.php?rest_route=\/wp\/v2\/posts\/24\/revisions"}],"predecessor-version":[{"id":35,"href":"https:\/\/connectranet.co.uk\/wp\/index.php?rest_route=\/wp\/v2\/posts\/24\/revisions\/35"}],"wp:attachment":[{"href":"https:\/\/connectranet.co.uk\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/connectranet.co.uk\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/connectranet.co.uk\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}