{"id":2200,"date":"2022-02-07T22:10:51","date_gmt":"2022-02-08T03:10:51","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2200"},"modified":"2022-02-08T22:06:47","modified_gmt":"2022-02-09T03:06:47","slug":"assignment-4-dht-22-temperature-sensor-and-led","status":"publish","type":"post","link":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2200","title":{"rendered":"Assignment 4: DHT 22 Temperature Sensor and LED"},"content":{"rendered":"<p>Before adding p5js to the operation, I set up a first iteration of input and output in Arduino.<\/p>\n<p>The intention of the project is to measure environmental conditions and cue an alert when environmental conditions exceed a desired limit (too hot or too cold). This could have applications as a monitoring device during excessive heat waves. Based on temperature input from the sensor, the arduino\u00a0 turns on a blinking warming light at a set temperature limit. For those seeking to use air-conditioning to a bare minimum (and save $), this system could act as a gauge that advises on serious conditions.\u00a0Especially if someone is reliant on a window unit A\/C instead of central air-conditions, it may encourage someone to seek a cooler environment and avoid risk of heat stroke.<\/p>\n<p>I used the sensor to capture temperature data in warm and cold spots in my apartment. At the time measured, there was about a 7 degree F difference between the kitchen (~72 degrees F) and the bedroom (~65 degrees F). Since this was the temperature differential currently present in my apartment, I used tempF&gt;= 70 to test the code, moving between rooms. In the kitchen, the LED light blinked, indicating the temperature was equal to or greater than 70 degrees F (I also used the Arduino serial monitor to check readings as I moved around).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2218\" src=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/20220207_201448-225x300.jpg\" alt=\"\" width=\"260\" height=\"347\" srcset=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/20220207_201448-225x300.jpg 225w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/20220207_201448-768x1024.jpg 768w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/20220207_201448-1152x1536.jpg 1152w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/20220207_201448-1536x2048.jpg 1536w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/20220207_201448-1200x1600.jpg 1200w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/20220207_201448-scaled.jpg 1920w\" sizes=\"(max-width: 260px) 85vw, 260px\" \/><\/p>\n<p>^DHT 22 Temperature and Humidity Sensor and LED<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/\r\n\r\n#include \"DHT.h\"   \/\/adafruit dht library \r\n#define DHTPIN 2        \/\/ sensor DHT22 data connected to Arduino pin 2\r\n#define DHTTYPE DHT22  \/\/ using sensor DHT22\r\n\r\nDHT dht(DHTPIN, DHTTYPE);  \/\/dht library \r\nint chk;\r\nfloat humid; \/\/stores humidity\r\nfloat tempC; \/\/stores temperature\r\nfloat tempF; \/\/stores temp in F\r\nint blueled=10;\r\n\r\n\r\nvoid setup() {\r\n  pinMode(blueled, OUTPUT);\r\n  Serial.begin(9600);\r\n  dht.begin();\r\n\r\n\r\n}\r\n\r\nvoid loop() {\r\n  \/\/read dht22 data and store to variables humid and temp\r\n  if (isnan(humid) || isnan(tempC) ||isnan(tempF)){\r\n    Serial.println(F(\"Failed to read from DHT sensor\"));\r\n    delay(10000);\r\n    return;\r\n  }\r\n\/\/ troubleshooting issues with getting nan reading from dht22\r\n  humid=dht.readHumidity();\r\n  tempC=dht.readTemperature();\r\n\/\/converting to fahrenheit\r\n  tempF=(tempC*1.8)+32;\r\n\/\/printing\u00a0temp\u00a0and\u00a0humid\u00a0values\u00a0to\u00a0serial\u00a0monitor\r\n  Serial.print(\"Humidity: \");\r\n  Serial.print(humid);\r\n  Serial.print(\" %, Temp: \");\r\n  Serial.print(tempF);\r\n  Serial.println(\"Fahrenheit\");\r\n  delay(2000);\r\n  if (tempF &gt;= 70){\r\n    digitalWrite(blueled,HIGH);\r\n    delay(150);\r\n    digitalWrite(blueled,LOW);\r\n    delay(75);\r\n  }\r\n delay(2000); \r\n\r\n\r\n}<\/pre>\n<p>Arduino was a success. The LED light blinked on and off when temperature was above 70, and stayed off when temperature was less than 70.<\/p>\n<p>&#8212;<\/p>\n<p>When I connected the p5.serial control, the console was printing out the serial print that had been set up on the arduino:<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-medium wp-image-2209\" src=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/p5js_serial-console-255x300.png\" alt=\"\" width=\"255\" height=\"300\" srcset=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/p5js_serial-console-255x300.png 255w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/p5js_serial-console-871x1024.png 871w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/p5js_serial-console-768x903.png 768w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/p5js_serial-console.png 1193w\" sizes=\"(max-width: 255px) 85vw, 255px\" \/><\/p>\n<p>^first, I thought I would break out the second part of the arduino code (the control of the LED light) as data sent to arduino via p5. serial control. The library for DHT22 is all C++ and not clear to me initially how to modify. So as a first step, I tried to print sensor data on p5js.<\/p>\n<p>Reviewed two arduino\/potentiometer\/P5js tutorials (https:\/\/medium.com\/@yyyyyyyuan\/tutorial-serial-communication-with-arduino-and-p5-js-cd39b3ac10ce, https:\/\/itp.nyu.edu\/physcomp\/labs\/labs-serial-communication\/lab-serial-input-to-the-p5-js-ide\/#Draw_a_Graph_With_the_Sensor_Values). I thought it would be interesting to use the information from the sensor to draw a graph, which would highlight changes in temperature or humidity. There was a lot that needed to be done to set up serial port, not sure if this is where everything went wrong, and my final result was undefined. I am wondering if this has to do with the DHT22 library being all C++ or if DHT 22 values need to be mapped to 0 to 255 before this information is read by P5 serial.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-medium wp-image-2210\" src=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/p5js_data-undefined-300x241.png\" alt=\"\" width=\"300\" height=\"241\" srcset=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/p5js_data-undefined-300x241.png 300w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/p5js_data-undefined-1024x823.png 1024w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/p5js_data-undefined-768x618.png 768w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/p5js_data-undefined-1536x1235.png 1536w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/p5js_data-undefined-1200x965.png 1200w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/p5js_data-undefined.png 1960w\" sizes=\"(max-width: 300px) 85vw, 300px\" \/><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let serial; \/\/ variable to hold an instance of the serialport library\r\nlet inData;\r\n \r\nfunction setup() {\r\n  serial = new p5.SerialPort(); \/\/ make a new instance of the serialport library\r\n  serial.on('list', printList); \/\/ set a callback function for the serialport list event\r\n \r\n  serial.list(); \/\/ list the serial ports\r\n}\r\n \r\n\/\/ get the list of ports:\r\nfunction printList(portList) {\r\n  \/\/ portList is an array of serial port names\r\n  for (var i = 0; i &lt; portList.length; i++) {\r\n    \/\/ Display the list the console:\r\n    console.log(i + portList[i]);\r\n  }\r\n}\r\n\r\n\r\nlet portName = 'COM5';  \/\/ fill in your serial port name here\r\n \r\nfunction setup() {\r\n  serial = new p5.SerialPort();       \/\/ make a new instance of the serialport library\r\n  serial.on('list', printList);  \/\/ set a callback function for the serialport list event\r\n  serial.on('connected', serverConnected); \/\/ callback for connecting to the server\r\n  serial.on('open', portOpen);        \/\/ callback for the port opening\r\n  serial.on('data', serialEvent);     \/\/ callback for when new data arrives\r\n  serial.on('error', serialError);    \/\/ callback for errors\r\n  serial.on('close', portClose);      \/\/ callback for the port closing\r\n \r\n  serial.list();                      \/\/ list the serial ports\r\n  serial.open(portName);              \/\/ open a serial port\r\n}\r\n\r\nfunction serverConnected() {\r\n  console.log('connected to server.');\r\n}\r\n \r\nfunction portOpen() {\r\n  console.log('the serial port opened.')\r\n}\r\n \r\nfunction serialEvent() {\r\n  inData=Number(serial.read());\r\n \r\n}\r\n \r\nfunction serialError(err) {\r\n  console.log('Something went wrong with the serial port. ' + err);\r\n}\r\n \r\nfunction portClose() {\r\n  console.log('The serial port closed.');\r\n}\r\n\r\nfunction setup() {\r\n  createCanvas(400, 400);\r\n}\r\n\r\n\r\nfunction draw() {\r\n  background(0);\r\n  fill(255);\r\n  text(\"data:\"+ inData, 30, 50)\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>My references include:<\/p>\n<p>https:\/\/www.arduino.cc\/reference\/en\/libraries\/dht-sensor-library\/<\/p>\n<p>https:\/\/stackoverflow.com\/questions\/40874880\/getting-nan-readings-from-dht-11-sensor<\/p>\n<p>https:\/\/create.arduino.cc\/projecthub\/mafzal\/temperature-monitoring-with-dht22-arduino-15b013<\/p>\n<p>https:\/\/www.instructables.com\/How-to-use-DHT-22-sensor-Arduino-Tutorial\/<\/p>\n<p>https:\/\/itp.nyu.edu\/physcomp\/videos\/videos-serial-communication\/#Serial_to_p5js_in_binary<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"01fILB8n73\"><p><a href=\"https:\/\/www.circuitbasics.com\/how-to-set-up-the-dht11-humidity-sensor-on-an-arduino\/\">How to Set Up the DHT11 Humidity Sensor on an Arduino<\/a><\/p><\/blockquote>\n<p><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;How to Set Up the DHT11 Humidity Sensor on an Arduino&#8221; &#8212; Circuit Basics\" src=\"https:\/\/www.circuitbasics.com\/how-to-set-up-the-dht11-humidity-sensor-on-an-arduino\/embed\/#?secret=9jIOhL6bMb#?secret=01fILB8n73\" data-secret=\"01fILB8n73\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>Notes-<\/p>\n<p>When looking for DHT22 and p5js, this project came up: https:\/\/github.com\/TTurbo0824\/pcom_final\/blob\/master\/test_led_hum_serial.ino<\/p>\n<p>^has a note for incorporating p5js but this just looks like what got uploaded to arduino, not additional p5js data sent<\/p>\n<p>&nbsp;<\/p>\n<p>** 2\/08\/2022 Update:<\/p>\n<p>Serial port appears now to be connected to P5JS. I was able to use the test sketch from last week to check that the values for temperature (in C) came through, then both values for temperature and for humidity.<\/p>\n<p>HOWEVER, while running the sketch in the browser editor, the data from the serial port is not registering.<\/p>\n<p>New P5JS sketch:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let serial; \/\/ variable to hold an instance of the serialport library\r\nlet portName = 'COM5'\r\nlet inData;\r\n \r\nfunction setup() {\r\n  createCanvas(400,300);\r\n  background('rgba(0,255,0, 0.15)');\r\n  \r\n\r\n}\r\n\r\n  serial = new p5.SerialPort(); \/\/ make a new instance of the serialport library\r\n  serial.on('list', printList); \/\/ set a callback function for the serialport list event\r\n \r\n  serial.list(); \/\/ list the serial ports\r\n\r\n \r\n\/\/ get the list of ports:\r\nfunction printList(portList) {\r\n  \/\/ portList is an array of serial port names\r\n  for (var i = 0; i &lt; portList.length; i++) {\r\n    \/\/ Display the list the console:\r\n    console.log(i + portList[i]);\r\n    serial.on('list',printList);\r\n    serial.on('connected', serverConnected); \/\/ callback for connecting to the server\r\n  serial.on('open', portOpen);        \/\/ callback for the port opening\r\n  serial.on('data', serialEvent);     \/\/ callback for when new data arrives\r\n  serial.on('error', serialError);    \/\/ callback for errors\r\n  serial.on('close', portClose);      \/\/ callback for the port closing\r\n    \r\n    \r\nserial.open(portName);              \r\n    \/\/ open a serial port\r\n    \r\n  function serverConnected() {\r\n  console.log('connected to server.');\r\n}\r\n \r\nfunction portOpen() {\r\n  console.log('the serial port opened.')\r\n}\r\n \r\nfunction serialEvent() {\r\n  inData = Number(serial.read());\r\n \r\n}\r\n \r\nfunction serialError(err) {\r\n  console.log('Something went wrong with the serial port. ' + err);\r\n}\r\n \r\nfunction portClose() {\r\n  console.log('The serial port closed.');\r\n}\r\n\r\n    \r\n  }\r\n \r\n\r\n  function graphData(newData) {\r\n  var yPos=map(newData,0,255,0,height)\r\n  stroke(0xA8,0xD9,0xA7);\r\n  line(xPos,height,xPos,height - YPos);\r\n  if (xPos&gt;=width) {\r\n    xPos=0;\r\n    background(0x08, 0x16, 0x40);\r\n  } else {\r\n    xPos++;\r\n  }\r\n  }  \r\n function draw() {\r\n  graphData(inData);\r\n  text(\"temperature in Celsius:\" + inData, 1, 50);\r\n  \r\n} \r\n  \r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before adding p5js to the operation, I set up a first iteration of input and output in Arduino. The intention of the project is to measure environmental conditions and cue an alert when environmental conditions exceed a desired limit (too hot or too cold). This could have applications as a monitoring device during excessive heat &hellip; <a href=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2200\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Assignment 4: DHT 22 Temperature Sensor and LED&#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":[11,1],"tags":[],"_links":{"self":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2200"}],"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=2200"}],"version-history":[{"count":18,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2200\/revisions"}],"predecessor-version":[{"id":2239,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2200\/revisions\/2239"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}