{"id":1140,"date":"2024-01-02T05:55:54","date_gmt":"2024-01-02T05:55:54","guid":{"rendered":"https:\/\/snersbots.co.uk\/?p=1140"},"modified":"2024-01-02T05:55:54","modified_gmt":"2024-01-02T05:55:54","slug":"pico-temperature-sensor","status":"publish","type":"post","link":"https:\/\/snersbots.co.uk\/index.php\/pico-temperature-sensor\/","title":{"rendered":"Pico Temperature Sensor"},"content":{"rendered":"\n<p>My advent calendar didn&#8217;t have chocolate this year. Instead it had a Pico and lots of LEDs. Thanks to <a href=\"https:\/\/thepihut.com\/\">The Pi Hut<\/a> for a very enjoyable project, and for the extensive guides which help a person get up and running with the Pico. All the code &#8211; micropython &#8211; in this post is provided by the Pi Hut, in their exceptionally usable and friendly daily guides that accompany the calendar.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>My favourite two components were the <a href=\"https:\/\/thepihut.com\/products\/adafruit-neopixel-ring-12-x-5050-rgb-led-with-integrated-drivers\">12-LED RGB Ring<\/a> and the <a href=\"https:\/\/thepihut.com\/products\/dht20-aht20-pin-module-i2c-temperature-and-humidity-sensor\">DHT20\/AHT20 temperature and humidity sensor<\/a>. And the actual temperature is shown on the <a href=\"https:\/\/thepihut.com\/products\/rgb-16x2-i2c-lcd-display-3-3v-5v\">16&#215;2 LCD (with I2C backpack)<\/a>. Apologies if the links are broken. I don&#8217;t expect the Pi Hut to keep them on sale forever, but I&#8217;m pretty sure you will be able to find something near enough.<\/p>\n\n\n\n<p>While I enjoyed all the projects provided by the Pi Hut, the temperature sensor did strike a chord with me. I am a bit of a weather geek. I just love weather &#8211; especially snow. That&#8217;s why I made myself this temperature sensor. So I combined the projects from three different days, and made myself a thermometer which you can check from far &#8211; just to get an indication of the temperature, and you can check from close to get the actual temperature.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"300\" src=\"http:\/\/snersbots.co.uk\/wp-content\/uploads\/2024\/01\/picotemp2.jpg\" alt=\"The LCD showing the temperature, along with a Low and a High\" class=\"wp-image-1145\" srcset=\"https:\/\/snersbots.co.uk\/wp-content\/uploads\/2024\/01\/picotemp2.jpg 400w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2024\/01\/picotemp2-300x225.jpg 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><figcaption class=\"wp-element-caption\">The LCD showing the temperature, along with a Lowest and Highest Temperatures recorded since it was last plugged in.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"300\" src=\"http:\/\/snersbots.co.uk\/wp-content\/uploads\/2024\/01\/picotemp1.jpg\" alt=\"The LED ring gives an indication of the temperature at the moment. The display shows half of the Leds are lit, indicating that the temperature is average - in this case 20 degrees.\" class=\"wp-image-1146\" srcset=\"https:\/\/snersbots.co.uk\/wp-content\/uploads\/2024\/01\/picotemp1.jpg 400w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2024\/01\/picotemp1-300x225.jpg 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><figcaption class=\"wp-element-caption\">The LED ring gives an indication of the temperature at the moment. The display shows half of the Leds are lit, indicating that the temperature is average &#8211; in this case 20 degrees.<\/figcaption><\/figure>\n\n\n\n<p>The idea is &#8211; when you are far away from the project, you can quickly see how many leds are lit. If more than half of the leds are lit, it is probably too warm, but if fewer are lit, it is probably too cool. In my case I decided 20 degrees was the ideal temperature, so when exactly half the leds are lit, the temperature is 20.<\/p>\n\n\n\n<p>The code is all provided by the Pi Hut. I just combined code from a few different pages to get the final project that I wanted. And it is Micropython. I used Thonny which has a very nice interface with the Pico. And the wiring guides &#8211; you can probably get with each component you buy. <\/p>\n\n\n\n<pre class=\"wp-block-code has-background\" style=\"background-color:#e9f1f7\"><code>from machine import I2C, Pin\nfrom lcd_api import LcdApi\nfrom pico_i2c_lcd import I2cLcd\nfrom dht20 import DHT20\nfrom neopixel import NeoPixel\nimport time\n\n\n# Define LCD\/sensor I2C pins\/BUS\/address\nSDA = 14\nSCL = 15\nI2C_BUS = 1\nLCD_ADDR = 0x27\nTEMP_ADDR = 0x38\n\n# Define LCD rows\/columns\nLCD_NUM_ROWS = 2\nLCD_NUM_COLS = 16\n\n# Set up LCD I2C\nlcdi2c = I2C(I2C_BUS, sda=machine.Pin(SDA), scl=machine.Pin(SCL), freq=400000)\nlcd = I2cLcd(lcdi2c, LCD_ADDR, LCD_NUM_ROWS, LCD_NUM_COLS)\n\n# Set up temperature sensor I2C\ntempi2c = I2C(I2C_BUS, sda=SDA, scl=SCL)\ndht20 = DHT20(TEMP_ADDR, tempi2c)\n\n\n# Create a temperature\/LED dictionary for our scale\n# Temperature is the key (left), LED index is the value (right)\nLEDdict = {\n  14: 0,\n  15: 1,\n  16: 2,\n  17: 3,\n  18: 4,\n  19: 5,\n  20: 6, # Top-middle LED (index 6 \/ LED #7) for 20\u00b0C\n  21: 7,\n  22: 8,\n  23: 9,\n  24: 10,\n  25: 11,\n}\n\n# Define the ring pin number (2) and number of LEDs (12)\nring = NeoPixel(Pin(2), 12)\n\n\n# Write  static LCD text\nlcd.putstr(\"Current:\")\nlcd.move_to(0, 1) # Move to second row\nlcd.putstr(\"L:       H:\")\n\n#Take initial reading\nmeasurements = dht20.measurements\n\n# Create temp and humidity variables\n# From initial readings\nlowtemp = round(measurements&#91;'t'],1)\nhightemp = round(measurements&#91;'t'],1)\n\n# Write initial low temp value to LCD\nlcd.move_to(3, 1)\nlcd.putstr(str(lowtemp))\n\n# Write initial high temp value to LCD\nlcd.move_to(12, 1) # 12th column, 2nd row\nlcd.putstr(str(hightemp))\n    \nwhile True:\n    \n    # Grab data from the sensor dictionary\n    measurements = dht20.measurements\n    \n    # Create variable for current temp\n    tempnow = round(measurements&#91;'t'],1)\n    \n    # Update current temp on display\n    lcd.move_to(12, 0)\n    lcd.putstr(str(tempnow))\n    \n    # If the lowest temp is HIGHER than current temp\n    if tempnow &lt; lowtemp:\n        \n        # Update the lowest recorded temp\n        lowtemp = tempnow\n        \n        # Update the LCD data\n        lcd.move_to(3, 1) # 3rd column, 2nd row\n        lcd.putstr(str(lowtemp))\n        \n    # If the highest temp is LOWER than current temp\n    if tempnow &gt; hightemp:\n        \n        # Update the highest recorded temp\n        hightemp = tempnow\n        \n        # Update the LCD data\n        lcd.move_to(12, 1) # 12th column, 2nd row\n        lcd.putstr(str(hightemp))\n        \n      # Create a rounded variable for the temperature\n    temperature = round(measurements&#91;'t'])\n    \n    if temperature not in LEDdict:\n        \n        pass\n        print(\"*** Out of temperature range ***\")\n\n    else:\n                \n        for key in LEDdict:\n            if key &lt;= temperature:\n                \n                ring&#91;LEDdict&#91;key]] = (10,0,0) # Light the index LED\n            else:\n                ring&#91;LEDdict&#91;key]] = (0,0,0)\n            ring.write() # Write the LED data\n    \n    \n    # Check every 5 seconds\n    time.sleep(5)\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>My advent calendar didn&#8217;t have chocolate this year. Instead it had a Pico and lots of LEDs. Thanks to The Pi Hut for a very enjoyable project, and for the extensive guides which help a person get up and running with the Pico. All the code &#8211; micropython &#8211; in this post is provided by <a class=\"read-more\" href=\"https:\/\/snersbots.co.uk\/index.php\/pico-temperature-sensor\/\">The full story here&#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":1144,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,75],"tags":[72,74],"class_list":["post-1140","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-electronics","category-other-electronics","tag-micropython","tag-pico"],"_links":{"self":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/1140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/comments?post=1140"}],"version-history":[{"count":6,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/1140\/revisions"}],"predecessor-version":[{"id":1150,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/1140\/revisions\/1150"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/media\/1144"}],"wp:attachment":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/media?parent=1140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/categories?post=1140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/tags?post=1140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}