{"id":120,"date":"2020-04-21T22:51:20","date_gmt":"2020-04-21T20:51:20","guid":{"rendered":"https:\/\/mic.st\/blog\/?p=120"},"modified":"2020-04-22T19:30:24","modified_gmt":"2020-04-22T17:30:24","slug":"connect-temperature-and-humidity-sensor-am2320-dht11-or-dht22-to-raspberry-pi","status":"publish","type":"post","link":"https:\/\/mic.st\/blog\/connect-temperature-and-humidity-sensor-am2320-dht11-or-dht22-to-raspberry-pi\/","title":{"rendered":"Connect AM2320 temperature and humidity sensor to Raspberry Pi"},"content":{"rendered":"\n<p>This year I wanted to grow a lot of vegetables in the garden behind our apartment. The last few years I already grewed tomatoes and cucumber on my balcony. Of course a garden is so much better to grow stuff because there is just a lot more soil and room. However, it is safer to help the germination by using an indoor greenhouse. Luckily I got a pretty nice one as a birthday present \ud83d\ude00. I installed some LED lights inside that should help the seeds to grow once they have germinated. As a side effect, the warm up the greenhouse. To have better insights about temperatures on the different levels of the greenhouse I wanted to use a Raspberry Pi and connect some temperature sensor to it. Consequently, I would install some simple air conditioning if it appears to be too hot inside.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Temperature and Humidity sensor for the Raspberry Pi<\/h2>\n\n\n\n<p>Luckily there is the AM2320 (and its variants) that can be easily connected to a Raspberry Pi. It is not only a temperature sensor, it can measure humidity as well. There are presoldered boards with three pin outbreaks that do not even require to solder any resistors to it. I just bought three of them on ebay because they have almost the same price as the sensor itself.<\/p>\n\n\n\n<p>There is an easy to use library by the great people from Adafruit that you can use for reading the sensors. It includes code for all the supported variants.<\/p>\n\n\n\n<p><em>See my other blogpost to learn about the minimal setup of a Raspberry Pi that I used for this ongoing project: <a href=\"https:\/\/mic.st\/blog\/minimal-setup-of-raspberry-pi-zero-w-with-ssh-over-wifi-connection\/\">https:\/\/mic.st\/blog\/minimal-setup-of-raspberry-pi-zero-w-with-ssh-over-wifi-connection\/<\/a><\/em><\/p>\n\n\n\n<p>So how do we connect to the sensor? If you have the three pin variant, you just have to connect +3.3V and GND to your Pi and chose one of the GPIO you want to use for the signal. That&#8217;s all. If you want to connect multiple AM2320 you just have to connect them to different GPIO pins.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Software<\/h2>\n\n\n\n<p>The Adafruit library we will use is a python library. Therefore we need <em>python<\/em> to run our script as well as <em>git<\/em> to download the library.<\/p>\n\n\n\n<p>Firstly, connect to your Raspberry Pi via <code>ssh<\/code> and run  <code>sudo apt-get update<\/code><\/p>\n\n\n\n<p>After that, run the following commands to get all the needed dependencies you need to run python and git:<br><code>sudo apt-get install build-essential python-dev python-openssl git<\/code><\/p>\n\n\n\n<p>Now we want to download the library and have a first try with the included demo file. Therefore you need to clone the repository for the library. Do that by running the following command in your terminal:<br><code>git clone https:\/\/github.com\/adafruit\/Adafruit_Python_DHT.git<\/code><\/p>\n\n\n\n<p>Change into the directory with <code>cd Adafruit_Python_DHT<\/code> and run <code>sudo python setup.py install<\/code> to install the needed drivers.<\/p>\n\n\n\n<p>If the install command does not work, you need to install <code>python-setuptools<\/code> first via:<br><code>sudo apt-get install python-setuptools<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Read out temperature and humidty with the demo script<\/h2>\n\n\n\n<p>Now you should be able to read the temperature and humidity from your sensor with the demo file in the library. You will find that in the <em>examples<\/em> directory. Change to this directory by calling <code>cd examples<\/code>. <\/p>\n\n\n\n<p>Finally, run <code>python AdafruitDHT.py 2304 4<\/code>. If you look at this call, there are two parameters. The first parameter is for the sensor you use, the second one is for the GPIO pin where you connected your sensor.<\/p>\n\n\n\n<p>If you open the example file (AdafruitDHT.py) with some texteditor you see the supported sensors and the needed arguments for loading their drivers (just run <code>nano AdafruitDHT.py<\/code> to see the file&#8217;s content): <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">sensor_args = { <span class=\"hljs-string\">'11'<\/span>: Adafruit_DHT.DHT11,\n                <span class=\"hljs-string\">'22'<\/span>: Adafruit_DHT.DHT22,\n                <span class=\"hljs-string\">'2302'<\/span>: Adafruit_DHT.AM2302 }<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Consequently, if we wanted to use a DHT22 at GPIO7, we would run <code>python AdafruitDHT.py 22 7<\/code>. If you use an AM2302 at GPIO Pin 3 you would run <code>python AdafruitDHT.py 2302 3<\/code> and so on.<\/p>\n\n\n\n<p>As a result you should see some console output similar to:<br> <code>Temp=22.0* Humidity=33.4%<\/code><\/p>\n\n\n\n<p>This was a short introduction on how to use some widely used temperature and humidty sensors with the Raspberry Pi. In another post you will learn how to include the library in your existing project.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Sources: <a href=\"https:\/\/tutorials-raspberrypi.de\/raspberry-pi-luftfeuchtigkeit-temperatur-messen-dht11-dht22\/\" target=\"_blank\" rel=\"noopener\">https:\/\/tutorials-raspberrypi.de\/raspberry-pi-luftfeuchtigkeit-temperatur-messen-dht11-dht22\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This year I wanted to grow a lot of vegetables in the garden behind our apartment. The last few years I already grewed tomatoes and cucumber on my balcony. Of course a garden is so much better to grow stuff because there is just a lot more soil and room. However, it is safer to&hellip;<\/p>\n","protected":false},"author":1,"featured_media":267,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[19,20],"tags":[29,30,31,33,34,21,32],"class_list":["post-120","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-raspberry-pi","category-electronics","tag-am2320","tag-dht11","tag-dht22","tag-humidity-sensor","tag-python","tag-raspberry","tag-temperature-sensor"],"_links":{"self":[{"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/posts\/120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/comments?post=120"}],"version-history":[{"count":0,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/posts\/120\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/media\/267"}],"wp:attachment":[{"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/media?parent=120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/categories?post=120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/tags?post=120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}