{"id":8039,"date":"2023-01-20T19:58:11","date_gmt":"2023-01-20T18:58:11","guid":{"rendered":"https:\/\/www.fosforoscienza.it\/?p=8039"},"modified":"2023-02-27T12:23:36","modified_gmt":"2023-02-27T11:23:36","slug":"microbit-e-il-sensore-dht11","status":"publish","type":"post","link":"https:\/\/www.fosforoscienza.it\/index.php\/2023\/01\/20\/microbit-e-il-sensore-dht11\/","title":{"rendered":"Microbit e il sensore DHT11"},"content":{"rendered":"\n<p>Impariamo a prendere confidenza con i sensori e con <strong>Microbit<\/strong>. In questo tutorial imparerai come creare il piccolo circuito e vedrai un codice <strong>Python<\/strong> di esempio.<\/p>\n\n\n\n<p>Puoi usare <strong>Microbit<\/strong> direttamente o oppure comodit\u00e0 utilizzarlo con la basetta di espansione, \u00e8 indifferente scegli come ti \u00e8 pi\u00f9 comodo a seconda dei cavi a tua disposizione. Se hai i coccodrilli puoi andare dritto per la prima via, altrimenti se hai i cavetti maschio\/femmina la seconda \u00e8 decisamente la strada da prendere. In questo caso noi prenderemo questa seconda via.<\/p>\n\n\n\n<p>In questo caso abbiamo collegato la terra a microbit e il pin di alimentazione ai 3 volt. Le informazioni passeranno entrambi sul Pin 0<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.fosforoscienza.it\/wp-content\/uploads\/2023\/01\/53887940-40379a00-405e-11e9-9129-5bdb6262e8a3.png\" alt=\"Microbit collegamento DHT11\" class=\"wp-image-8043\" width=\"474\" height=\"516\"\/><figcaption>Microbit collegamento DHT11<\/figcaption><\/figure><\/div>\n\n\n<p>Il sensore DHT11 disponibile sia a 4 che a 3 pin, nella versione presente nel kit trovate come quello in figura la versione con 3 collegamenti.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.fosforoscienza.it\/wp-content\/uploads\/2023\/01\/KY-015DHT11TemperatursensorModul_1024x.jpg-1024x1024.webp\" alt=\"Sensore DHT11\" class=\"wp-image-8024\" width=\"358\" height=\"358\" srcset=\"https:\/\/www.fosforoscienza.it\/wp-content\/uploads\/2023\/01\/KY-015DHT11TemperatursensorModul_1024x.jpg.webp 1024w, https:\/\/www.fosforoscienza.it\/wp-content\/uploads\/2023\/01\/KY-015DHT11TemperatursensorModul_1024x.jpg-300x300.webp 300w, https:\/\/www.fosforoscienza.it\/wp-content\/uploads\/2023\/01\/KY-015DHT11TemperatursensorModul_1024x.jpg-150x150.webp 150w, https:\/\/www.fosforoscienza.it\/wp-content\/uploads\/2023\/01\/KY-015DHT11TemperatursensorModul_1024x.jpg-768x768.webp 768w, https:\/\/www.fosforoscienza.it\/wp-content\/uploads\/2023\/01\/KY-015DHT11TemperatursensorModul_1024x.jpg-600x600.webp 600w, https:\/\/www.fosforoscienza.it\/wp-content\/uploads\/2023\/01\/KY-015DHT11TemperatursensorModul_1024x.jpg-200x200.webp 200w, https:\/\/www.fosforoscienza.it\/wp-content\/uploads\/2023\/01\/KY-015DHT11TemperatursensorModul_1024x.jpg-560x560.webp 560w\" sizes=\"auto, (max-width: 358px) 100vw, 358px\" \/><figcaption>Sensore DHT11<\/figcaption><\/figure><\/div>\n\n\n<p>Per la parte di programmazione abbiamo usato <strong>Visual<\/strong> <strong>Studio<\/strong> <strong>Code<\/strong>, usate pure l&#8217;editor che preferite.<\/p>\n\n\n\n<p>L&#8217;esempio riporta come leggere i valori del sensore e mandarli a schermo sui <strong>25<\/strong> <strong>led<\/strong> di <strong>Microbit<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" style=\"font-size:.875rem;line-height:1.25rem\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"from microbit import *\nimport dht\nimport time\n\nwhile True:\n    sensore = dht.DHT11(pin1)\n    sensore.measure()\n    temp = sensore.temperature()\n    hum = sensore.humidity()\n    display.scroll(&quot;Temp: &quot; + str(temp) + &quot; Hum: :&quot; + str(hum))\n    time.sleep(2)\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\"><code><span class=\"line\"><span style=\"color: #81A1C1\">from<\/span><span style=\"color: #D8DEE9FF\"> microbit <\/span><span style=\"color: #81A1C1\">import<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">*<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">import<\/span><span style=\"color: #D8DEE9FF\"> dht<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">import<\/span><span style=\"color: #D8DEE9FF\"> time<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">while<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">True<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    sensore <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> dht<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">DHT11<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">pin1<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    sensore<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">measure<\/span><span style=\"color: #ECEFF4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    temp <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> sensore<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">temperature<\/span><span style=\"color: #ECEFF4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    hum <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> sensore<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">humidity<\/span><span style=\"color: #ECEFF4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    display<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">scroll<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Temp: <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">str<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">temp<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\"> Hum: :<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">str<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">hum<\/span><span style=\"color: #ECEFF4\">))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    time<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">sleep<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Nella sfida lanciata dovete usare <strong>3 sensori in contemporanea<\/strong> e visualizzare cos\u00ec tre dati diversi appartenenti a tre tipo di terreno.<a href=\"https:\/\/www.fosforoscienza.it\/index.php\/terreni-e-terrari\/\"> (sfida)<\/a><\/p>\n\n\n\n<p>Ecco un video tutorial che vi aiuta.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Microbit con Sensore DHT11in Python \u2022 Smart&amp;Tink\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/1vE7mgmM_pE?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Scopri come far funzionare un sensore DHT11 con Microbit.<\/p>\n","protected":false},"author":1,"featured_media":7825,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[41,19],"tags":[],"class_list":["post-8039","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-smarttink","category-tecnologia","category-41","category-19","description-off"],"_links":{"self":[{"href":"https:\/\/www.fosforoscienza.it\/index.php\/wp-json\/wp\/v2\/posts\/8039","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.fosforoscienza.it\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fosforoscienza.it\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fosforoscienza.it\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fosforoscienza.it\/index.php\/wp-json\/wp\/v2\/comments?post=8039"}],"version-history":[{"count":5,"href":"https:\/\/www.fosforoscienza.it\/index.php\/wp-json\/wp\/v2\/posts\/8039\/revisions"}],"predecessor-version":[{"id":8247,"href":"https:\/\/www.fosforoscienza.it\/index.php\/wp-json\/wp\/v2\/posts\/8039\/revisions\/8247"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.fosforoscienza.it\/index.php\/wp-json\/wp\/v2\/media\/7825"}],"wp:attachment":[{"href":"https:\/\/www.fosforoscienza.it\/index.php\/wp-json\/wp\/v2\/media?parent=8039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fosforoscienza.it\/index.php\/wp-json\/wp\/v2\/categories?post=8039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fosforoscienza.it\/index.php\/wp-json\/wp\/v2\/tags?post=8039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}