{"id":2903,"date":"2021-10-26T06:45:01","date_gmt":"2021-10-26T13:45:01","guid":{"rendered":"https:\/\/www.donluc.com\/?p=2903"},"modified":"2021-10-26T06:45:01","modified_gmt":"2021-10-26T13:45:01","slug":"project-15-environment-hc-sr04-ultrasonic-sensor-mk18","status":"publish","type":"post","link":"https:\/\/www.donluc.com\/?p=2903","title":{"rendered":"Project #15: Environment \u2013 HC-SR04 Ultrasonic Sensor \u2013 Mk18"},"content":{"rendered":"<div style=\"width: 720px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-2903-1\" width=\"720\" height=\"480\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2021\/10\/DL2110Mk05W.mp4?_=1\" \/><a href=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2021\/10\/DL2110Mk05W.mp4\">https:\/\/www.donluc.com\/wp-content\/uploads\/2021\/10\/DL2110Mk05W.mp4<\/a><\/video><\/div>\n<p>&#8212;&#8212;<\/p>\n<p>#DonLucElectronics #DonLuc #Environment #MQ #PIR #HCSR04 #RHT03 #RTC #ArduinoUNO #Arduino #AdafruitPowerBoost #Project #Programming #Electronics #Microcontrollers #Consultant<\/p>\n<p>&#8212;&#8212;<\/p>\n<p><a href=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2021\/10\/DL2110Mk05a.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2021\/10\/DL2110Mk05a.png\" alt=\"HC-SR04\" width=\"720\" height=\"569\" class=\"alignnone size-full wp-image-2905\" srcset=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2021\/10\/DL2110Mk05a.png 720w, https:\/\/www.donluc.com\/wp-content\/uploads\/2021\/10\/DL2110Mk05a-300x237.png 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/a><\/p>\n<p>&#8212;&#8212;<\/p>\n<p><a href=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2021\/10\/DL2110Mk05b.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2021\/10\/DL2110Mk05b.png\" alt=\"HC-SR04\" width=\"720\" height=\"480\" class=\"alignnone size-full wp-image-2906\" srcset=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2021\/10\/DL2110Mk05b.png 720w, https:\/\/www.donluc.com\/wp-content\/uploads\/2021\/10\/DL2110Mk05b-300x200.png 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/a><\/p>\n<p>&#8212;&#8212;<\/p>\n<p><a href=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2021\/10\/DL2110Mk05c.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2021\/10\/DL2110Mk05c.png\" alt=\"HC-SR04\" width=\"720\" height=\"480\" class=\"alignnone size-full wp-image-2907\" srcset=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2021\/10\/DL2110Mk05c.png 720w, https:\/\/www.donluc.com\/wp-content\/uploads\/2021\/10\/DL2110Mk05c-300x200.png 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/a><\/p>\n<p>&#8212;&#8212;<\/p>\n<p><a href=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2021\/10\/DL2110Mk05d.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2021\/10\/DL2110Mk05d.png\" alt=\"HC-SR04\" width=\"720\" height=\"480\" class=\"alignnone size-full wp-image-2908\" srcset=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2021\/10\/DL2110Mk05d.png 720w, https:\/\/www.donluc.com\/wp-content\/uploads\/2021\/10\/DL2110Mk05d-300x200.png 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/a><\/p>\n<p>&#8212;&#8212;<\/p>\n<p><strong>HC-SR04 Ultrasonic Sensor<\/strong><\/p>\n<p>This is the HC-SR04 ultrasonic distance sensor. This economical sensor provides 2cm to 400cm of non-contact measurement functionality with a ranging accuracy that can reach up to 3mm. Each HC-SR04 module includes an ultrasonic transmitter, a receiver and a control circuit.<\/p>\n<p>There are only four pins that you need to worry about on the HC-SR04: VCC (Power), Trig (Trigger), Echo (Receive), and GND (Ground). You will find this sensor very easy to set up and use for your next range-finding project. This sensor has additional control circuitry that can prevent inconsistent &#8220;bouncy&#8221; data depending on the application.<\/p>\n<p><strong>DL2110Mk05<\/strong><\/p>\n<p>1 x Arduino UNO &#8211; R3<br \/>\n1 x ProtoScrewShield<br \/>\n1 x RGB LCD Shield 16&#215;2 Character Negative Display<br \/>\n1 x HC-SR04 Ultrasonic Sensor<br \/>\n1 x ChronoDot &#8211; Ultra-Precise Real Time Clock &#8211; v2.1<br \/>\n1 x CR1632 Batteries<br \/>\n1 x MicroSD Card Breakout Board+<br \/>\n1 x MicroSD 2.0 GB<br \/>\n1 x Rocker Switch &#8211; SPST (Round)<br \/>\n1 x 10K Ohm<br \/>\n1 x LED Green<br \/>\n1 x 220 Ohm<br \/>\n1 x Adafruit PowerBoost 500 Shield<br \/>\n1 x Lithium Ion Battery &#8211; 3.7v 2000mAh<br \/>\n4 x Pololu Carrier for MQ Gas Sensors<br \/>\n1 x SparkFun Hydrogen Gas Sensor &#8211; MQ-8<br \/>\n1 x 4.7K Ohm<br \/>\n1 x Pololu Carbon Monoxide &#038; Flammable Gas Sensor &#8211; MQ-9<br \/>\n1 x 22k Ohm<br \/>\n1 x SparkFun Carbon Monoxide Gas Sensor &#8211; MQ-7<br \/>\n1 x 10K Ohm<br \/>\n1 x SparkFun Alcohol Gas Sensor &#8211; MQ-3<br \/>\n1 x 220k Ohm<br \/>\n1 x Temperature and Humidity Sensor- RHT03<br \/>\n1 x PIR Motion Sensor (JST)<br \/>\n1 x SparkFun Solderable Half-Breadboard<br \/>\n1 x Half-Breadboard<br \/>\n1 x SparkFun Cerberus USB Cable<\/p>\n<p><strong>Arduino UNO &#8211; R3<\/strong><\/p>\n<p>CLK &#8211; Digital 13<br \/>\nDO &#8211; Digital 12<br \/>\nDI &#8211; Digital 11<br \/>\nCS &#8211; Digital 10<br \/>\nECH &#8211; Digital 9<br \/>\nTIR &#8211; Digital 8<br \/>\nPIR &#8211; Digital 7<br \/>\nRHT &#8211; Digital 5<br \/>\nRS0 &#8211; Digital 3<br \/>\nLEG &#8211; Digital 2<br \/>\nMQ8 &#8211; Analog 0<br \/>\nMQ9 &#8211; Analog 1<br \/>\nMQ7 &#8211; Analog 2<br \/>\nMQ3 &#8211; Analog 3<br \/>\nSDA &#8211; Analog 4<br \/>\nSCL &#8211; Analog 5<br \/>\nVIN &#8211; +5V<br \/>\nGND &#8211; GND<\/p>\n<p><strong>DL2110Mk05p.ino<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"arduino\" data-enlighter-title=\"\">\r\n\/* \r\n***** Don Luc Electronics \u00a9 *****\r\nSoftware Version Information\r\nProject #15: Environment \u2013 HC-SR04 Ultrasonic Sensor \u2013 Mk18\r\n10-05\r\nDL2110Mk05p.ino\r\n1 x Arduino UNO - R3\r\n1 x ProtoScrewShield\r\n1 x RGB LCD Shield 16x2 Character Negative Display\r\n1 x HC-SR04 Ultrasonic Sensor\r\n1 x ChronoDot - Ultra-Precise Real Time Clock - v2.1\r\n1 x CR1632 Batteries\r\n1 x MicroSD Card Breakout Board+\r\n1 x MicroSD 2.0 GB\r\n1 x Rocker Switch - SPST (Round)\r\n1 x 10K Ohm\r\n1 x LED Green\r\n1 x 220 Ohm\r\n1 x Adafruit PowerBoost 500 Shield\r\n1 x Lithium Ion Battery - 3.7v 2000mAh\r\n4 x Pololu Carrier for MQ Gas Sensors\r\n1 x SparkFun Hydrogen Gas Sensor - MQ-8\r\n1 x 4.7K Ohm\r\n1 x Pololu Carbon Monoxide &amp; Flammable Gas Sensor - MQ-9\r\n1 x 22k Ohm\r\n1 x SparkFun Carbon Monoxide Gas Sensor - MQ-7\r\n1 x 10K Ohm\r\n1 x SparkFun Alcohol Gas Sensor - MQ-3\r\n1 x 220k Ohm\r\n1 x Temperature and Humidity Sensor - RHT03\r\n1 x PIR Motion Sensor (JST)\r\n1 x SparkFun Solderable Half-Breadboard\r\n1 x Half-Breadboard\r\n1 x SparkFun Cerberus USB Cable\r\n*\/\r\n\r\n\/\/ Include the Library Code\r\n\/\/ EEPROM Library to Read and Write EEPROM with Unique ID for Unit\r\n#include &lt;EEPROM.h&gt;\r\n\/\/ RHT Temperature and Humidity Sensor\r\n#include &lt;SparkFun_RHT03.h&gt;\r\n\/\/ Adafruit RGB LCD Shield 16x2\r\n#include &lt;Adafruit_RGBLCDShield.h&gt;\r\n\/\/ Wire\r\n#include &lt;Wire.h&gt;\r\n\/\/ DS3231 RTC Date and Time\r\n#include &lt;RTClib.h&gt;\r\n\/\/ SD Card\r\n#include &lt;SPI.h&gt;\r\n#include &lt;SD.h&gt;\r\n\r\n\/\/ RHT Temperature and Humidity Sensor\r\n\/\/ RHT03 data pin Digital 5\r\nconst int RHT03_DATA_PIN = 5;\r\n\/\/ This creates a RTH03 object, which we&#039;ll use to interact with the sensor\r\nRHT03 rht;\r\nfloat latestHumidity;\r\nfloat latestTempC;\r\n\r\n\/\/ Gas Sensors MQ\r\n\/\/ Hydrogen Gas Sensor - MQ-8\r\nint iMQ8 = A0;\r\nint iMQ8Raw = 0;\r\nint iMQ8ppm = 0;\r\n\/\/ Two points are taken from the curve in datasheet.\r\n\/\/ With these two points, a line is formed which is\r\n\/\/ &quot;approximately equivalent&quot; to the original curve.\r\nfloat H2Curve[3] = {2.3, 0.93,-1.44};\r\n\r\n\/\/ Carbon Monoxide &amp; Flammable Gas Sensor - MQ-9\r\nint iMQ9 = A1;\r\nint iMQ9Raw = 0;\r\nint iMQ9ppm = 0;\r\n\r\n\/\/ Carbon Monoxide Gas Sensor - MQ-7\r\nint iMQ7 = A2;\r\nint iMQ7Raw = 0;\r\nint iMQ7ppm = 0;\r\n\r\n\/\/ Alcohol Gas Sensor - MQ-3\r\nint iMQ3 = A3;\r\nint iMQ3Raw = 0;\r\nint iMQ3ppm = 0;\r\n\r\n\/\/ PIR Motion\r\n\/\/ Motion detector\r\nconst int iMotion = 7;\r\n\/\/ Proximity\r\nint proximity = LOW;\r\nString Det = &quot;&quot;;\r\n\r\n\/\/ Adafruit RGB LCD Shield\r\nAdafruit_RGBLCDShield RGBLCDShield = Adafruit_RGBLCDShield();\r\n\/\/ These #defines make it easy to set the backlight color\r\n#define OFF 0x0\r\n#define RED 0x1\r\n#define YELLOW 0x3\r\n#define GREEN 0x2\r\n#define TEAL 0x6\r\n#define BLUE 0x4\r\n#define VIOLET 0x5\r\n#define WHITE 0x7\r\n\/\/ Momentary Button\r\nint yy = 0;\r\nuint8_t momentaryButton = 0;\r\n\r\n\/\/ DS3231 RTC Date and Time\r\nRTC_DS3231 rtc;\r\nString sDate;\r\nString sTime;\r\n\r\n\/\/ SD Card\r\nconst int chipSelect = 10;\r\nString zzzzzz = &quot;&quot;;\r\n\r\n\/\/ LED Green\r\nint iLEDGreen = 2;\r\n\r\n\/\/ Rocker Switch - SPST (Round)\r\nint iSS1 = 3;\r\n\/\/ State\r\nint iSS1State = 0;\r\n\r\n\/\/ HC-SR04 Ultrasonic Sensor\r\nint iTrig = 8;\r\nint iEcho = 9;\r\n\/\/ Stores the distance measured by the distance sensor\r\nfloat distance = 0;\r\n\r\n\/\/ Software Version Information\r\nString uid = &quot;&quot;;\r\n\/\/ Version\r\nString sver = &quot;15-18&quot;;\r\n\r\nvoid loop()\r\n{\r\n     \r\n  \/\/ Adafruit RGB LCD Shield\r\n  \/\/ Clear\r\n  RGBLCDShield.clear();\r\n\r\n  \/\/ iLEDGreen LOW\r\n  digitalWrite(iLEDGreen,  LOW );\r\n  \r\n  \/\/ RHT Temperature and Humidity Sensor\r\n  isRHT03();\r\n\r\n  \/\/ Gas Sensors MQ\r\n  isGasSensor();\r\n\r\n  \/\/ isPIR Motion\r\n  isPIR();\r\n\r\n  \/\/ DS3231 RTC Date and Time\r\n  isRTC();\r\n  \r\n  \/\/ HC-SR04 Ultrasonic Sensor\r\n  isHCSR04();\r\n\r\n  \/\/ Adafruit RGB LCD Shield\r\n  \/\/ Display\r\n  isDisplay();\r\n\r\n  \/\/ Slide Switch\r\n  \/\/ Read the state of the iSS1 value\r\n  iSS1State = digitalRead(iSS1);\r\n  \r\n  \/\/ If it is the Slide Switch State is HIGH\r\n  if (iSS1State == HIGH) {\r\n\r\n    \/\/ iLEDGreen HIGH\r\n    digitalWrite(iLEDGreen,  HIGH );\r\n    \r\n    \/\/ MicroSD Card\r\n    isSD();\r\n\r\n  } else {\r\n\r\n    \/\/ iLEDGreen LOW\r\n    digitalWrite(iLEDGreen,  LOW );\r\n  \r\n  }\r\n\r\n  \/\/ Delay\r\n  delay( 500 );\r\n \r\n}\r\n<\/pre>\n<p><strong>getEEPROM.ino<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"arduino\" data-enlighter-title=\"\">\r\n\/\/ EEPROM\r\n\/\/ isUID EEPROM Unique ID\r\nvoid isUID()\r\n{\r\n  \r\n  \/\/ Is Unit ID\r\n  uid = &quot;&quot;;\r\n  for (int x = 0; x &lt; 5; x++)\r\n  {\r\n    uid = uid + char(EEPROM.read(x));\r\n  }\r\n  \r\n}\r\n<\/pre>\n<p><strong>getGasSensorMQ.ino<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"arduino\" data-enlighter-title=\"\">\r\n\/\/ Gas Sensors MQ\r\n\/\/ Gas Sensor\r\nvoid isGasSensor() {\r\n\r\n  \/\/ Read in analog value from each gas sensors\r\n  \r\n  \/\/ Hydrogen Gas Sensor - MQ-8\r\n  iMQ8Raw = analogRead( iMQ8 );\r\n\r\n  \/\/ Carbon Monoxide &amp; Flammable Gas Sensor - MQ-9\r\n  iMQ9Raw = analogRead( iMQ9 );  \r\n\r\n  \/\/ Carbon Monoxide Gas Sensor - MQ-7\r\n  iMQ7Raw = analogRead( iMQ7 );\r\n\r\n  \/\/ Alcohol Gas Sensor - MQ-3\r\n  iMQ3Raw = analogRead( iMQ3 );\r\n  \r\n  \/\/ Caclulate the PPM of each gas sensors\r\n\r\n  \/\/ Hydrogen Gas Sensor - MQ-8\r\n  iMQ8ppm = isMQ8( iMQ8Raw );\r\n\r\n  \/\/ Carbon Monoxide &amp; Flammable Gas Sensor - MQ-9\r\n  iMQ9ppm = isMQ9( iMQ9Raw ); \r\n\r\n  \/\/ Carbon Monoxide Gas Sensor - MQ-7\r\n  iMQ7ppm = isMQ7( iMQ7Raw ); \r\n\r\n  \/\/ Alcohol Gas Sensor - MQ-3\r\n  iMQ3ppm = isMQ3( iMQ3Raw ); \r\n\r\n}\r\n\/\/ Hydrogen Gas Sensor - MQ-8 - PPM\r\nint isMQ8(double rawValue) {\r\n\r\n  \/\/ RvRo\r\n  double RvRo = rawValue * (3.3 \/ 1023);\r\n\r\n  return (pow(4.7,( ((log(RvRo)-H2Curve[1])\/H2Curve[2]) + H2Curve[0])));\r\n  \r\n}\r\n\/\/ Carbon Monoxide &amp; Flammable Gas Sensor - MQ-9\r\nint isMQ9(double rawValue) {\r\n\r\n  double RvRo = rawValue * 3.3 \/ 4095;\r\n\r\n  double ppm = 3.027*exp(1.0698*( RvRo ));\r\n  return ppm;\r\n  \r\n}\r\n\/\/ Carbon Monoxide Gas Sensor - MQ-7\r\nint isMQ7(double rawValue) {\r\n\r\n  double RvRo = rawValue * 3.3 \/ 4095;\r\n\r\n  double ppm = 3.027*exp(1.0698*( RvRo ));\r\n  return ppm;\r\n  \r\n}\r\n\/\/ Alcohol Gas Sensor - MQ-3\r\nint isMQ3(double rawValue) {\r\n\r\n  double RvRo = rawValue * 3.3 \/ 4095;\r\n\r\n  double bac = RvRo * 0.21;\r\n  return bac;\r\n  \r\n}\r\n<\/pre>\n<p><strong>getHC-SR04.ino<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"arduino\" data-enlighter-title=\"\">\r\n\/\/ HC-SR04 Ultrasonic Sensor\r\n\/\/ Setup HC-SR04\r\nvoid setupHCSR04() {\r\n\r\n  \/\/ The trigger iTrig will output pulses of electricity\r\n  pinMode(iTrig, OUTPUT);\r\n  \/\/ The echo iEcho will measure the duration of pulses coming back from the distance sensor\r\n  pinMode(iEcho, INPUT);\r\n  \r\n}\r\n\/\/ HC-SR04\r\nvoid isHCSR04() {\r\n\r\n  \/\/ Variable to store the distance measured by the sensor\r\n  distance = isDistance();\r\n    \r\n}\r\n\/\/ Distance\r\nfloat isDistance() {\r\n  \r\n  \/\/ Variable to store the time it takes for a ping to bounce off an object\r\n  float echoTime;\r\n  \/\/ Variable to store the distance calculated from the echo time\r\n  float calculatedDistance;\r\n\r\n  \/\/ Send out an ultrasonic pulse that&#039;s 10ms long\r\n  digitalWrite(iTrig, HIGH);\r\n  delayMicroseconds(10);\r\n  digitalWrite(iTrig, LOW);\r\n\r\n  \/\/ Use the pulseIn command to see how long it takes for the\r\n  \/\/ pulse to bounce back to the sensor\r\n  echoTime = pulseIn(iEcho, HIGH);\r\n\r\n  \/\/ Calculate the distance of the object that reflected the pulse\r\n  \/\/ (half the bounce time multiplied by the speed of sound)\r\n  \/\/ cm = 58.0\r\n  calculatedDistance = echoTime \/ 58.0;\r\n\r\n  \/\/ Send back the distance that was calculated\r\n  return calculatedDistance;\r\n  \r\n}\r\n<\/pre>\n<p><strong>getPIR.ino<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"arduino\" data-enlighter-title=\"\">\r\n\/\/ PIR Motion\r\n\/\/ Setup PIR\r\nvoid setupPIR() {\r\n\r\n  \/\/ Setup PIR Montion\r\n  pinMode(iMotion, INPUT_PULLUP);\r\n  \r\n}\r\n\/\/ isPIR Motion\r\nvoid isPIR() {\r\n\r\n  \/\/ Proximity\r\n  proximity = digitalRead(iMotion);\r\n  if (proximity == LOW) \r\n  {\r\n\r\n    \/\/ PIR Motion Sensor&#039;s LOW, Motion is detected\r\n    Det = &quot;Motion Yes&quot;;\r\n\r\n  }\r\n  else\r\n  {\r\n\r\n    \/\/ PIR Motion Sensor&#039;s HIGH\r\n    Det = &quot;No&quot;;\r\n    \r\n  }\r\n  \r\n}\r\n<\/pre>\n<p><strong>getRGBLCDShield.ino<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"arduino\" data-enlighter-title=\"\">\r\n\/\/ Adafruit RGB LCD Shield\r\n\/\/ Setup RGB LCD Shield\r\nvoid isSetupRGBLCDShield() {\r\n\r\n  \/\/ Adafruit RGB LCD Shield\r\n  \/\/ Set up the LCD&#039;s number of columns and rows: \r\n  RGBLCDShield.begin(16, 2);\r\n\r\n  \/\/ Set the cursor to column 0, line 0\r\n  RGBLCDShield.setBacklight(RED);\r\n  \/\/ Don luc \r\n  RGBLCDShield.setCursor(0,0);\r\n  RGBLCDShield.print(&quot;Don Luc&quot;);\r\n  \/\/ Set the cursor to column 0, line 1\r\n  RGBLCDShield.setCursor(0, 1);\r\n  \/\/ Electronics\r\n  RGBLCDShield.print(&quot;Electronics&quot;);\r\n  \/\/ Delay\r\n  delay(5000);\r\n  \/\/ Clear\r\n  RGBLCDShield.clear();\r\n\r\n  \/\/ Set the cursor to column 0, line 0\r\n  RGBLCDShield.setBacklight(TEAL);\r\n  \/\/ Version \r\n  RGBLCDShield.setCursor(0,0);\r\n  RGBLCDShield.print(&quot;Version: &quot; + sver);\r\n  \/\/ Set the cursor to column 0, line 1\r\n  RGBLCDShield.setCursor(0, 1);\r\n  \/\/ Unit ID\r\n  RGBLCDShield.print(&quot;Unit ID: &quot; + uid);\r\n  \/\/ Delay\r\n  delay(5000);\r\n  \/\/ Clear\r\n  RGBLCDShield.clear();\r\n  \r\n}\r\n\/\/ isDisplay\r\nvoid isDisplay() {\r\n\r\n  \/\/ Momentary Button\r\n  momentaryButton = RGBLCDShield.readButtons();\r\n\r\n  switch ( yy ) {\r\n    case 1:\r\n    \r\n      \/\/ RHT Temperature and Humidity Sensor\r\n      \/\/ Set the cursor to column 0, line 0\r\n      RGBLCDShield.setCursor(0,0);\r\n      \/\/ Temperature C\r\n      RGBLCDShield.print( &quot;Temp C: &quot; );\r\n      RGBLCDShield.print( latestTempC );\r\n      \/\/ Set the cursor to column 0, line 1\r\n      RGBLCDShield.setCursor(0, 1);\r\n      \/\/ Humidity\r\n      RGBLCDShield.print( &quot;Humidity: &quot; );\r\n      RGBLCDShield.print( latestHumidity );\r\n\r\n      break;\r\n    case 2:\r\n    \r\n      \/\/ Set the cursor to column 0, line 0\r\n      \/\/ PIR Motion Sensor\r\n      RGBLCDShield.setCursor(0,0);\r\n      RGBLCDShield.print( &quot;PIR: &quot; );\r\n      RGBLCDShield.print( Det );\r\n      \/\/ Set the cursor to column 0, line 1\r\n      \/\/ HC-SR04 Ultrasonic Sensor\r\n      RGBLCDShield.setCursor(0, 1);\r\n      RGBLCDShield.print( &quot;HC-SR04: &quot; );\r\n      RGBLCDShield.print( distance );\r\n      \r\n      break;\r\n    case 3:\r\n\r\n      \/\/ Gas Sensors 1\r\n      \/\/ Set the cursor to column 0, line 0\r\n      RGBLCDShield.setCursor(0,0);\r\n      \/\/ Hydrogen Gas Sensor - MQ-8\r\n      RGBLCDShield.print( &quot;MQ-8: &quot; );\r\n      RGBLCDShield.print( iMQ8ppm );\r\n      \/\/ Set the cursor to column 0, line 1\r\n      RGBLCDShield.setCursor(0, 1);\r\n      \/\/ Carbon Monoxide &amp; Flammable Gas Sensor - MQ-9\r\n      RGBLCDShield.print( &quot;MQ-9: &quot; );\r\n      RGBLCDShield.print( iMQ9ppm );\r\n      \r\n      break;\r\n    case 4:\r\n\r\n      \/\/ Gas Sensors 2\r\n      \/\/ Set the cursor to column 0, line 0\r\n      RGBLCDShield.setCursor(0,0);\r\n      \/\/ Carbon Monoxide Gas Sensor - MQ-7\r\n      RGBLCDShield.print( &quot;MQ-7: &quot; );\r\n      RGBLCDShield.print( iMQ7ppm );\r\n      \/\/ Set the cursor to column 0, line 1\r\n      RGBLCDShield.setCursor(0, 1);\r\n      \/\/ Alcohol Gas Sensor - MQ-3\r\n      RGBLCDShield.print( &quot;MQ-3: &quot; );\r\n      RGBLCDShield.print( iMQ3ppm );\r\n      \r\n      break;\r\n    case 5:\r\n\r\n      \/\/ DS3231 RTC Date and Time\r\n      \/\/ Date and Time\r\n      DateTime now = rtc.now();\r\n      \/\/ Set the cursor to column 0, line 0\r\n      \/\/ Date \r\n      RGBLCDShield.setCursor(0,0);\r\n      RGBLCDShield.print( sDate );\r\n      \/\/ Set the cursor to column 0, line 1\r\n      RGBLCDShield.setCursor(0, 1);\r\n      \/\/ Time\r\n      RGBLCDShield.print( sTime );\r\n      \r\n      break;\r\n    default:\r\n\r\n      \/\/ Don luc Electronics\r\n      yy = 5;\r\n      RGBLCDShield.setBacklight(RED);\r\n      \/\/ Set the cursor to column 0, line 0\r\n      \/\/ Don luc \r\n      RGBLCDShield.setCursor(0,0);\r\n      RGBLCDShield.print(&quot;Don Luc&quot;);\r\n      \/\/ Set the cursor to column 0, line 1\r\n      RGBLCDShield.setCursor(0, 1);\r\n      \/\/ Electronics\r\n      RGBLCDShield.print(&quot;Electronics&quot;);\r\n\r\n   }\r\n   \r\n   if ( momentaryButton ) {\r\n    \r\n    if ( momentaryButton &amp; BUTTON_UP ) {\r\n      \r\n      yy = 1;\r\n      \/\/ RHT Temperature and Humidity Sensor\r\n      RGBLCDShield.setBacklight(GREEN);\r\n      \r\n    }\r\n    \r\n    if ( momentaryButton &amp; BUTTON_DOWN ) {\r\n      \r\n      yy = 2;\r\n      \/\/ PIR Motion Sensor\r\n      RGBLCDShield.setBacklight(VIOLET);\r\n      \r\n    }\r\n    \r\n    if ( momentaryButton &amp; BUTTON_LEFT ) {\r\n      \r\n      yy = 3;\r\n      \/\/ Gas Sensors 1\r\n      RGBLCDShield.setBacklight(TEAL);\r\n      \r\n    }\r\n    \r\n    if ( momentaryButton &amp; BUTTON_RIGHT ) {\r\n\r\n      yy = 4;\r\n      \/\/ Gas Sensors 2\r\n      RGBLCDShield.setBacklight(YELLOW);\r\n    }\r\n    \r\n    if ( momentaryButton &amp; BUTTON_SELECT ) {\r\n\r\n      yy = 5;\r\n      \/\/ DS3231 RTC Date and Time\r\n      RGBLCDShield.setBacklight(WHITE);\r\n   \r\n    }\r\n    \r\n  }\r\n  \r\n}\r\n<\/pre>\n<p><strong>getRHT.ino<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"arduino\" data-enlighter-title=\"\">\r\n\/\/ RHT Temperature and Humidity Sensor\r\n\/\/ setup RHT Temperature and Humidity Sensor\r\nvoid setupRTH03() {\r\n\r\n  \/\/ RHT Temperature and Humidity Sensor\r\n  \/\/ Call rht.begin() to initialize the sensor and our data pin\r\n  rht.begin(RHT03_DATA_PIN);\r\n  \r\n}\r\n\/\/ RHT Temperature and Humidity Sensor\r\nvoid isRHT03(){\r\n\r\n  \/\/ Call rht.update() to get new humidity and temperature values from the sensor.\r\n  int updateRet = rht.update();\r\n\r\n  \/\/ The humidity(), tempC(), and tempF() functions can be called -- after \r\n  \/\/ a successful update() -- to get the last humidity and temperature value \r\n  latestHumidity = rht.humidity();\r\n  latestTempC = rht.tempC();\r\n\r\n}\r\n<\/pre>\n<p><strong>getRTC.ino<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"arduino\" data-enlighter-title=\"\">\r\n\/\/ DS3231 RTC Date and Time\r\n\/\/ Setup DS3231 RTC\r\nvoid isSetupRTC() {\r\n\r\n  if (! rtc.begin()) {\r\n    while (1);\r\n  }\r\n\r\n  if (rtc.lostPower()) {\r\n    \/\/ Following line sets the RTC to the date &amp; time this sketch was compiled\r\n    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));\r\n    \/\/ This line sets the RTC with an explicit date &amp; time, for example to set\r\n    \/\/ January 21, 2014 at 3am you would call:\r\n    \/\/ rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));\r\n  }\r\n  \r\n}\r\n\/\/ DS3231 RTC Date and Time\r\nvoid isRTC(){\r\n \r\n    \/\/ Date and Time\r\n    sDate = &quot;&quot;;\r\n    sTime = &quot;&quot;;\r\n    \/\/ Date Time\r\n    DateTime now = rtc.now();\r\n\r\n    \/\/ sData\r\n    sDate += String(now.year(), DEC);\r\n    sDate += &quot;\/&quot;;\r\n    sDate += String(now.month(), DEC);\r\n    sDate += &quot;\/&quot;;\r\n    sDate += String(now.day(), DEC);\r\n  \r\n    \/\/ sTime\r\n    sTime += String(now.hour(), DEC);\r\n    sTime += &quot;:&quot;;\r\n    sTime += String(now.minute(), DEC);\r\n    sTime += &quot;:&quot;;\r\n    sTime += String(now.second(), DEC);\r\n    \r\n}\r\n<\/pre>\n<p><strong>getSD.ino<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"arduino\" data-enlighter-title=\"\">\r\n\/\/ MicroSD Card\r\n\/\/ MicroSD Setup\r\nvoid setupSD() {\r\n\r\n  \/\/ MicroSD Card\r\n  if (!SD.begin(chipSelect)) {\r\n\r\n     while (true);\r\n\r\n  }\r\n \r\n}\r\n\/\/ MicroSD Card\r\nvoid isSD() {\r\n\r\n  zzzzzz = &quot;&quot;;\r\n\r\n  \/\/ Don Luc Electronics \u00a9 (1983-2021)\r\n  \/\/ Arduino Data\r\n  \/\/ EEPROM Unique ID\r\n  \/\/ Version\r\n  \/\/ Date\r\n  \/\/ Time\r\n  \/\/ Temperature Celsius\r\n  \/\/ Humidity\r\n  \/\/ Hydrogen Gas Sensor - MQ-8\r\n  \/\/ Carbon Monoxide &amp; Flammable Gas Sensor - MQ-9\r\n  \/\/ Carbon Monoxide Gas Sensor - MQ-7\r\n  \/\/ Alcohol Gas Sensor - MQ-3\r\n  \/\/ PIR Motion\r\n  \/\/ HC-SR04 Ultrasonic Sensor\r\n  \/\/ EEPROM Unique ID|Version|Date|Time|Temperature Celsius|Humidity|MQ-8|MQ-9|MQ-7|MQ-3|PIR Motion|HC-SR04|\r\n  zzzzzz = uid + &quot;|&quot; + sver + &quot;|&quot; + sDate + &quot;|&quot; + sTime + &quot;|&quot; + latestTempC + &quot;|&quot; + latestHumidity + &quot;|&quot;\r\n  + iMQ8ppm + &quot;|&quot; + iMQ9ppm + &quot;|&quot; + iMQ7ppm + &quot;|&quot; + \r\n  iMQ3ppm + &quot;|&quot; + Det + &quot;|&quot; + distance + &quot;|&quot;;\r\n\r\n  \r\n  \/\/ Open the file. Note that only one file can be open at a time,\r\n  \/\/ so you have to close this one before opening another.\r\n\r\n  File dataFile = SD.open(&quot;arddata.txt&quot;, FILE_WRITE);\r\n\r\n  \/\/ If the file is available, write to it:\r\n  if ( dataFile ) {\r\n\r\n    dataFile.println( zzzzzz );\r\n\r\n    dataFile.close();\r\n\r\n  }\r\n  \r\n}\r\n<\/pre>\n<p><strong>setup.ino<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"arduino\" data-enlighter-title=\"\">\r\n\/\/ Setup\r\nvoid setup()\r\n{\r\n    \r\n  \/\/ EEPROM Unique ID\r\n  isUID();\r\n  \r\n  \/\/ RHT Temperature and Humidity Sensor\r\n  \/\/ Setup RTH03 Temperature and Humidity Sensor\r\n  setupRTH03();\r\n\r\n  \/\/ PIR Motion\r\n  \/\/ Setup PIR\r\n  setupPIR();\r\n\r\n  \/\/ Setup DS3231 RTC\r\n  isSetupRTC();\r\n\r\n  \/\/MicroSD Card\r\n  setupSD();\r\n\r\n  \/\/ Initialize the LED Green\r\n  pinMode(iLEDGreen, OUTPUT);\r\n  \/\/ iLEDGreen LOW\r\n  digitalWrite(iLEDGreen,  LOW );\r\n\r\n  \/\/ Slide Switch\r\n  pinMode(iSS1, INPUT);\r\n\r\n  \/\/ Setup HC-SR04\r\n  setupHCSR04();\r\n\r\n  \/\/ Adafruit RGB LCD Shield\r\n  isSetupRGBLCDShield();\r\n\r\n}\r\n<\/pre>\n<p>&#8212;&#8212;<\/p>\n<p><strong>People can contact us:<\/strong> https:\/\/www.donluc.com\/?page_id=1927<\/p>\n<p><strong>Technology Experience<\/strong><\/p>\n<ul>\n<li>Single-Board Microcontrollers (PIC, Arduino, Raspberry Pi,Espressif, etc&#8230;)<\/li>\n<li>IoT<\/li>\n<li>Robotics<\/li>\n<li>Camera and Video Capture Receiver Stationary, Wheel\/Tank and Underwater Vehicle<\/li>\n<li>Unmanned Vehicles Terrestrial and Marine<\/li>\n<li>Research &#038; Development (R &#038; D)<\/li>\n<li>Desktop Applications (Windows, OSX, Linux, Multi-OS, Multi-Tier, etc&#8230;)<\/li>\n<li>Mobile Applications (Android, iOS, Blackberry, Windows Mobile, Windows CE, etc&#8230;)<\/li>\n<li>Web Applications (LAMP, Scripting, Java, ASP, ASP.NET, RoR, Wakanda, etc&#8230;)<\/li>\n<li>Social Media Programming &#038; Integration (Facebook, Twitter, YouTube, Pinterest, etc&#8230;)<\/li>\n<li>Content Management Systems (WordPress, Drupal, Joomla, Moodle, etc&#8230;)<\/li>\n<li>Bulletin Boards (phpBB, SMF, Vanilla, jobberBase, etc&#8230;)<\/li>\n<li>eCommerce (WooCommerce, OSCommerce, ZenCart, PayPal Shopping Cart, etc&#8230;)<\/li>\n<\/ul>\n<p><strong>Instructor and E-Mentor<\/strong><\/p>\n<ul>\n<li>IoT<\/li>\n<li>PIC Microcontrollers<\/li>\n<li>Arduino<\/li>\n<li>Raspberry Pi<\/li>\n<li>Espressif<\/li>\n<li>Robotics<\/li>\n<li>DOS, Windows, OSX, Linux, iOS, Android, Multi-OS<\/li>\n<li>Linux-Apache-PHP-MySQL<\/li>\n<\/ul>\n<p><strong>Follow Us<\/strong><\/p>\n<p><strong>J. Luc Paquin \u2013 Curriculum Vitae &#8211; 2021 English &#038; Espa\u00f1ol<\/strong><br \/>\nhttps:\/\/www.jlpconsultants.com\/luc\/<\/p>\n<p><strong>Web:<\/strong> https:\/\/www.donluc.com\/<br \/>\n<strong>Web:<\/strong> https:\/\/www.jlpconsultants.com\/<br \/>\n<strong>Facebook:<\/strong> https:\/\/www.facebook.com\/neosteam.labs.9\/<br \/>\n<strong>YouTube:<\/strong> https:\/\/www.youtube.com\/channel\/UC5eRjrGn1CqkkGfZy0jxEdA<br \/>\n<strong>Twitter:<\/strong> https:\/\/twitter.com\/labs_steam<br \/>\n<strong>Pinterest:<\/strong> https:\/\/www.pinterest.com\/NeoSteamLabs\/<br \/>\n<strong>Instagram:<\/strong> https:\/\/www.instagram.com\/neosteamlabs\/<\/p>\n<p><strong>Don Luc<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8212;&#8212; #DonLucElectronics #DonLuc #Environment #MQ #PIR #HCSR04 #RHT03 #RTC #ArduinoUNO #Arduino #AdafruitPowerBoost #Project #Programming #Electronics #Microcontrollers #Consultant &#8212;&#8212; &#8212;&#8212; &#8212;&#8212; &#8212;&#8212; &#8212;&#8212; HC-SR04 Ultrasonic Sensor This is the HC-SR04 ultrasonic distance sensor. This economical sensor provides 2cm to 400cm of non-contact measurement functionality with a ranging accuracy that can reach up to 3mm. Each HC-SR04 &#8230; <a title=\"Project #15: Environment \u2013 HC-SR04 Ultrasonic Sensor \u2013 Mk18\" class=\"read-more\" href=\"https:\/\/www.donluc.com\/?p=2903\" aria-label=\"Read more about Project #15: Environment \u2013 HC-SR04 Ultrasonic Sensor \u2013 Mk18\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[106,47,50,49,38,59,5,46,58,10,43],"tags":[86,6,92,102,4,87,19,107,85,183,24,93,103,9,27,88,109,33],"class_list":["post-2903","post","type-post","status-publish","format-standard","hentry","category-environment","category-adafruit","category-arduino","category-consultant","category-digitalelectronics","category-fritzing","category-microcontrollers","category-pololu","category-arduino-programming","category-projects","category-sparkfun","tag-adafruit","tag-arduino","tag-battery","tag-components","tag-consultant","tag-display","tag-electronics","tag-environment","tag-fritzing","tag-hc-sr04","tag-microcontroller","tag-pir-motion-sensor","tag-pololu","tag-programming","tag-projects-2","tag-rht03-humidity-and-temperature-sensor","tag-sparkfun","tag-technology"],"_links":{"self":[{"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/posts\/2903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.donluc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2903"}],"version-history":[{"count":2,"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/posts\/2903\/revisions"}],"predecessor-version":[{"id":3621,"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/posts\/2903\/revisions\/3621"}],"wp:attachment":[{"href":"https:\/\/www.donluc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.donluc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.donluc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}