{"id":2297,"date":"2022-02-21T21:17:22","date_gmt":"2022-02-22T02:17:22","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2297"},"modified":"2022-02-21T21:17:22","modified_gmt":"2022-02-22T02:17:22","slug":"assignment-6-humidity-sensor-and-fan","status":"publish","type":"post","link":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2297","title":{"rendered":"Assignment 6: Humidity Sensor and Fan"},"content":{"rendered":"<p>I decided to use smoothed humidity data from the DHT-22 sensor to turn a fan with DC motor on and off.<\/p>\n<p>Code below combines DHT-22 data smoothing and motor response:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;DHT.h&gt;\r\n#include &lt;DHT_U.h&gt;\r\n#define DHTPIN 2        \/\/ sensor DHT22 data connected to Arduino pin 2\r\n#define DHTTYPE DHT22  \/\/ using sensor DHT22\r\nDHT dht(DHTPIN, DHTTYPE);  \/\/dht library \r\nint chk;\r\nint humid; \/\/stores humidity\r\nint tempC; \/\/stores temperature\r\nint tempF; \/\/stores temp in F\r\n\/\/data smoothing\r\nconst int numReadings = 10;\r\nint tempReadings [numReadings];\r\nint tempReadIndex = 0;\r\nint tempTotal = 0;\r\nint tempAverage = 0; \r\nint humReadings [numReadings];\r\nint humReadIndex=0;\r\nint humTotal = 0;\r\nint humAverage = 0; \r\nint inputPin= 2; \/\/put sensor pin here\r\n\/\/detecting sensor change\r\nint last; \r\n\/\/response to sensor change\r\n\r\n\r\n\/\/for fan and motor\r\nint speedPin=5;\r\nint dir1=4;\r\nint dir2=3;\r\nint mSpeed=0;\r\n\r\nvoid setup() {\r\n  \/\/ put your setup code here, to run once:\r\n\/\/pins for fan and motot\r\npinMode(speedPin,OUTPUT);\r\npinMode(dir1,OUTPUT);\r\npinMode(dir2,OUTPUT);\r\n\r\n\r\n\/\/dht sensor\r\ndht.begin(); \r\n\r\nfor (int thisReading = 0; thisReading &lt; numReadings; thisReading++) {\r\n    tempReadings[thisReading]= 0;\r\n    humReadings[thisReading]=0;\r\n\r\n\r\n\r\nSerial.begin(9600);\r\n\r\n}\r\n}\r\n\r\nvoid loop() {\r\n  \r\n  humid=dht.readHumidity();\r\n  tempC=dht.readTemperature();\r\n  delay(2000);\r\n  \r\n  humTotal = humTotal - humReadings[humReadIndex];\r\n  humReadings[humReadIndex] = humid;\r\n  humTotal = humTotal + humReadings [humReadIndex];\r\n  humReadIndex = humReadIndex + 1;\r\n  if (humReadIndex &gt;= numReadings) {\r\n    humReadIndex=0;\r\n  }\r\n  humAverage = humTotal \/ numReadings;\r\n  \r\n  if (humAverage&gt;=50)\r\n{\r\n  digitalWrite(dir1,HIGH);\r\n  digitalWrite(dir2,LOW);\r\n  analogWrite(speedPin,255);\r\n}\r\n\r\nif (humAverage&lt;50)\r\n{\r\n  analogWrite(speedPin,0);\r\n}\r\n}\r\n<\/pre>\n<p>I finished writing the code and set up the sensor and motor. Before I could test, the wire on the motor broke off. I will need to see if I can reattach or replace with materials in the IDEATE room. I will come to office hours tomorrow to review.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-medium wp-image-2298\" src=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/20220221_210140-300x240.jpg\" alt=\"\" width=\"300\" height=\"240\" srcset=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/20220221_210140-300x240.jpg 300w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/20220221_210140-1024x821.jpg 1024w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/20220221_210140-768x616.jpg 768w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/20220221_210140-1536x1231.jpg 1536w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/20220221_210140-2048x1641.jpg 2048w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/20220221_210140-1200x962.jpg 1200w\" sizes=\"(max-width: 300px) 85vw, 300px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I decided to use smoothed humidity data from the DHT-22 sensor to turn a fan with DC motor on and off. Code below combines DHT-22 data smoothing and motor response: #include &lt;DHT.h&gt; #include &lt;DHT_U.h&gt; #define DHTPIN 2 \/\/ sensor DHT22 data connected to Arduino pin 2 #define DHTTYPE DHT22 \/\/ using sensor DHT22 DHT dht(DHTPIN, &hellip; <a href=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2297\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Assignment 6: Humidity Sensor and Fan&#8221;<\/span><\/a><\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2297"}],"collection":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2297"}],"version-history":[{"count":1,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2297\/revisions"}],"predecessor-version":[{"id":2299,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2297\/revisions\/2299"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}