{"id":3226,"date":"2011-07-22T12:50:50","date_gmt":"2011-07-22T16:50:50","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=3226"},"modified":"2012-07-30T09:29:16","modified_gmt":"2012-07-30T13:29:16","slug":"atmega8-measures-ambient-temperature-and-relative-humidity-using-hsm-20g-sensor","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/atmega8-measures-ambient-temperature-and-relative-humidity-using-hsm-20g-sensor\/","title":{"rendered":"Atmega8 measures ambient temperature and relative humidity using HSM-20G sensor"},"content":{"rendered":"<p>In one of <a href=\"http:\/\/embedded-lab.com\/blog\/?p=2711\" target=\"_blank\">my previous posts<\/a>, I discussed about Sensirion&#8217;s SHT11 and SHT75 sensors, which are capable of measuring both temperature and relative humidity. They are digital sensors and provide fully calibrated digital outputs for temperature and relative humidity. I also illustrated how to interface those sensors with a PIC microcontroller. <strong>Shawon Shahryiar<\/strong> from <em>Dhaka, Banglades<\/em>h shared this project with us where he describes a method of interfacing the HSM-20G sensor to Atmega8 for measuring the ambient temperature and relative humidity. Unlike Sensirion&#8217;s SHT series, this is an analog sensor that converts the two ambient parameters into standard output voltages.<\/p>\n<div id=\"attachment_3230\" style=\"width: 570px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/100_0406.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3230\" class=\"size-full wp-image-3230\" title=\"100_0406\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/100_0406.jpg\" alt=\"\" width=\"560\" height=\"287\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/100_0406.jpg 560w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/100_0406-550x281.jpg 550w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/a><p id=\"caption-attachment-3230\" class=\"wp-caption-text\">Using HSM-20G sensor with Atmega8<\/p><\/div>\n<p><!--more--><\/p>\n<p><em><strong>Shawon<\/strong> writes,<\/em><\/p>\n<p>The  materials used for the project are a ATMega8A micro, a HSM-20G analog  relative humidity and temperature sensor module and my own-made 28 pin  AVR development board along with some passive components. MikroC for AVR  5.00 compiler was used for coding.<\/p>\n<p>Link for HSM-20G datasheet:<br \/>\n<a rel=\"nofollow\" href=\"http:\/\/www.justmystage.com\/home\/bellseki\/HSM-20G.pdf\" target=\"_blank\">http:\/\/www.justmystage.com?\/home\/bellseki\/HSM-20G.pdf. <\/a><\/p>\n<p>HSM-20G is an analog sensor module unlike SHT75 or SHT21. If you go through the datasheet of HSM-20G, you will find out that  they have given output voltage data from the humidity sensor for certain  relative humidity values and they have plotted a graph too. What I did  is simply used those data to plot and check if their graph is valid or  fake. Now as you may notice the plot is not linear but rather has  curve-like nature. Thus I used MATLAB&#8217;s &#8220;polyfit&#8221; function to get a  equation for the plotted curve to the third degree. The reasons for  using a third degree equation are firstly AVR has a built-in hardware  multiplier that works pretty fast and secondly I won&#8217;t be using the  micro to do any other tasks rather than metering. This is enough for a  fairly good precision. A second degree is not that good enough while a  higher order equation is unnecessary. If you you go through my program,  you will see the equation under the function &#8220;read_humidity()&#8221;. The  micro computes the data it gets from the HSM-20G module for relative  humidity and determines what it should show.<\/p>\n<div id=\"attachment_3233\" style=\"width: 580px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/HumidityCurve.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3233\" class=\"size-full wp-image-3233\" title=\"HumidityCurve\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/HumidityCurve.png\" alt=\"\" width=\"570\" height=\"527\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/HumidityCurve.png 570w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/HumidityCurve-550x508.png 550w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/a><p id=\"caption-attachment-3233\" class=\"wp-caption-text\">Calibration curve for humidity sensor<\/p><\/div>\n<p>The same task I did for temperature measurement but finding its data  was a little cumbersome. If you see the datasheet for the module, you  will notice they have given a circuit for interfacing the module to  other peripherals like a micro or a multimeter and I used that circuit.  However since they don&#8217;t know what value of resistance the user will use  at the temperature circuit side and because it&#8217;s a thermistor built  inside rather than semiconductor type sensor, they have provided  resistance values rather than output voltages at various temperatures.  Therefore and since I used their circuit, I calculated the voltages at  different temperatures from their data. Again I used the voltage and  temperature data as I did for the relative humidity part.<\/p>\n<div id=\"attachment_3234\" style=\"width: 511px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/TempCurve.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3234\" class=\"size-full wp-image-3234\" title=\"TempCurve\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/TempCurve.png\" alt=\"\" width=\"501\" height=\"394\" \/><\/a><p id=\"caption-attachment-3234\" class=\"wp-caption-text\">Calibration curve for temperature sensor<\/p><\/div>\n<p><strong>Circuit diagram<\/strong><\/p>\n<p>The circuit diagram is pretty simple. The two ADC channels (ADC0 and ADC1) of Atmega8 are used to measure the sensor output voltages that correspond to the ambient temperature and relative humidity. The two resistors and a capacitor connected to the output lines of the HSM-20G sensor are recommended in its datasheet. The results are displayed in a 4&#215;20 character LCD.<\/p>\n<div id=\"attachment_3227\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/Atmega8_HSM_20G.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3227\" class=\"size-medium wp-image-3227\" title=\"Atmega8_HSM_20G\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/Atmega8_HSM_20G-550x341.png\" alt=\"\" width=\"550\" height=\"341\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/Atmega8_HSM_20G-550x341.png 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/Atmega8_HSM_20G.png 674w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><p id=\"caption-attachment-3227\" class=\"wp-caption-text\">Circuit diagram for interfacing HSM-20G to Atmega8<\/p><\/div>\n<div id=\"attachment_3231\" style=\"width: 570px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/100_0414.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3231\" class=\"size-full wp-image-3231\" title=\"100_0414\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/100_0414.jpg\" alt=\"\" width=\"560\" height=\"420\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/100_0414.jpg 560w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/100_0414-550x412.jpg 550w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/a><p id=\"caption-attachment-3231\" class=\"wp-caption-text\">Circuit setup with the sensor module on the breadboard<\/p><\/div>\n<p><strong>Software<\/strong><\/p>\n<p>The firmware for this project is developed with MikroC Pro for AVR  5.00 compiler. The program is very simple and straight. There are six function other than the main function. These are described below briefly:<\/p>\n<p>1. <strong><em>void setup_MCU() <\/em><\/strong>This  function setups the micros peripherals for optimum operation by setting  up individual buil-in peripheral registers and library function of the  MikroC for AVR compiler. In this case the ADC and LCD libraries are  used.<\/p>\n<p>2. <strong><em>void display_common() <\/em><\/strong>This function simply  displays the texts &#8220;Temperature&#8221; and &#8220;Relative Humidity&#8221; along with  their units. Since there is only one screen, it&#8217;s called only once.<\/p>\n<p>3. <strong><em>float read_voltage(unsigned short channel)<\/em><\/strong> This  function read the ADC channel specified to it and takes the average of  64 ADC samples. This helps to reduce noisy A\/D inputs and enhance  precision. The result after doing all the maths is returned to the  calling function.<\/p>\n<p>4. <em><strong>float read_humidity() <\/strong><\/em>and <em><strong>float read_temperature()<\/strong><\/em> These compute relative humidity and temperature data using their equations and ADC data, and prepare for display.<\/p>\n<p>5. <em><strong>void display_data(unsigned char x, unsigned char y, float value) <\/strong><\/em>This function specifies the location of the display and what to show from the acquired data.<\/p>\n<p style=\"text-align: center;\"><strong><a href=\"http:\/\/embedded-lab.com\/uploads\/HexFiles\/Atmega8\/HSM_20G.zip\" target=\"_blank\">Download the source code and HEX files of this project<\/a><\/strong><\/p>\n<div id=\"attachment_3232\" style=\"width: 570px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/100_0412.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3232\" class=\"size-full wp-image-3232\" title=\"100_0412\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/100_0412.jpg\" alt=\"\" width=\"560\" height=\"407\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/100_0412.jpg 560w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/100_0412-550x399.jpg 550w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/a><p id=\"caption-attachment-3232\" class=\"wp-caption-text\">Relative humidity and temperature are displayed on the LCD<\/p><\/div>\n<table style=\"width: 560px;\" border=\"0\" cellspacing=\"5\" cellpadding=\"2\" align=\"center\">\n<tbody>\n<tr>\n<td style=\"border-color: #cccccc; background-color: #fdfdfd;\"><em>Many thanks to Shawon Shahryiar for contributing this project to Embedded Lab. He works as an Engineer in the Research and Development Cell at ELECTRO Group in Dhaka, Bangladesh.<\/em><\/p>\n<p><em>Contact him at:<br \/>\n<\/em><\/p>\n<p><em><strong> <\/strong> <\/em><img decoding=\"async\" src=\"http:\/\/embedded-lab.com\/uploads\/images\/ShawonEmail.png\" alt=\"\" \/><\/td>\n<td style=\"border-color: #cccccc; background-color: #fdfdfd;\"><img decoding=\"async\" src=\"http:\/\/embedded-lab.com\/uploads\/images\/Shawon.jpg\" alt=\"\" \/><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>In one of my previous posts, I discussed about Sensirion&#8217;s SHT11 and SHT75 sensors, which are capable of measuring both temperature and relative humidity. They are digital sensors and provide fully calibrated digital outputs for temperature and relative humidity. I also illustrated how to interface those sensors with a PIC microcontroller. Shawon Shahryiar from Dhaka, Bangladesh shared this project with us where he describes a method of interfacing the HSM-20G sensor to Atmega8 for measuring the ambient temperature and relative humidity. Unlike Sensirion&#8217;s SHT series, this is an analog sensor that converts the two ambient parameters into standard output voltages.<\/p>\n","protected":false},"author":1,"featured_media":3227,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[47,310,312,311,35],"class_list":["post-3226","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-avr-projects","tag-atmega8","tag-hsm-20g","tag-mikroc-for-avr","tag-relative-humidity","tag-temperature"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/3226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/comments?post=3226"}],"version-history":[{"count":19,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/3226\/revisions"}],"predecessor-version":[{"id":3274,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/3226\/revisions\/3274"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/3227"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=3226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=3226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=3226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}