{"id":530,"date":"2015-05-13T07:07:07","date_gmt":"2015-05-13T14:07:07","guid":{"rendered":"http:\/\/internetofhomethings.com\/homethings\/?p=530"},"modified":"2016-05-02T10:36:33","modified_gmt":"2016-05-02T17:36:33","slug":"8-esp8266-analog-inputs-for-22-cents","status":"publish","type":"post","link":"https:\/\/internetofhomethings.com\/homethings\/?p=530","title":{"rendered":"8 ESP8266 analog inputs for 22 cents"},"content":{"rendered":"<h3 style=\"text-align: center;\"><strong>Want More Analog Inputs?<\/strong><\/h3>\n<p>Do you have a project needing more than one analog input? If your using an ESP8266, that would seem to be a problem as it only offers a single input. Before you commit to using an Arduino, Spark Core, Raspberry PI or other higher priced micro-controller, one with multiple analog inputs, consider this&#8230;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-312 size-medium\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/03\/ESP8266image-300x228.png\" alt=\"ESP8266image\" width=\"300\" height=\"228\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/03\/ESP8266image-300x228.png 300w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/03\/ESP8266image.png 997w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Adding an\u00a08-1 multiplexer chip to your ESP8266-07 or -12 circuit can expand your system capability from one to eight analog inputs. \u00a0I recently tested this out, using a 74HC4051 CMOS analog multiplexer component I got from an AliExpress supplier.<\/p>\n<p>The cost?<\/p>\n<p>$2.19 for 10 chips, or $0.22 per chip.<\/p>\n<p>They are available in a 16-pin DIP package \u00a0with 2.54 pitch for easy use with a solder-less breadboard.<a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/muxpic.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-582 size-thumbnail\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/muxpic-150x150.jpg\" alt=\"muxpic\" width=\"150\" height=\"150\" \/><\/a>So what&#8217;s the downside? Well, selecting one of the 8 analog inputs will require 3 GPIO pins. For many applications, that should not pose a problem when using the ESP8266-07 or -12. These versions have 7 easily accessible GPIO pins available, leaving you with 4 GPIO pins for any digital input or output requirement after adding the 8 analog inputs.<\/p>\n<p>Here is the circuit I used to test this configuration:<\/p>\n<p><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/ESP8266-schematic-amux-test_rev1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1607\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/ESP8266-schematic-amux-test_rev1.jpg\" alt=\"ESP8266-schematic - amux-test_rev1\" width=\"1315\" height=\"584\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/ESP8266-schematic-amux-test_rev1.jpg 1315w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/ESP8266-schematic-amux-test_rev1-300x133.jpg 300w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/ESP8266-schematic-amux-test_rev1-768x341.jpg 768w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/ESP8266-schematic-amux-test_rev1-1024x455.jpg 1024w\" sizes=\"auto, (max-width: 1315px) 100vw, 1315px\" \/><\/a><\/p>\n<p>The left side of this drawing is the standard circuit I have been using for all of my ESP8266 projects. A typical USB to serial adapter is used when developing and flashing the circuit. When deployed, I use a USB plug with a wall adapter to provide a 5V source, without the Tx\/Rx signals.<\/p>\n<p>The analog multiplexer is controlled by 3 GPIO pins; 14,12 and 13. Setting the states of these 3 digital outputs determine which analog input will be measured.<\/p>\n<p><center>[wptg_comparison_table id=&#8221;5&#8243;]<\/center><br \/>\nThe <a href=\"https:\/\/github.com\/internetofhomethings\/ESP8266AnalogMux\" target=\"_blank\">sketch<\/a> used to\u00a0test this circuit was developed using the ESP8266 Arduino IDE.<\/p>\n<p>The serial output loops through each analog input, setting the multiplexer and reading an input every 2.5 seconds. The sketch also behaves as a web server. All 8 analog values can be retrieved in a single JSON string returned from an http GET request.<\/p>\n<h3 style=\"text-align: center;\"><strong>ESP8266 ADC Performance Evaluation<\/strong><\/h3>\n<p>Using this circuit and software, I proceeded to collect some data to characterize the ESP8266 analog input range. First, the multiplexer selector\u00a0was verified by connecting each input to ground and verifying the Analog count returned was 0. Likewise, each input was connected to 3.3V with a reading of 1024 returned when reading the ESP8266 ADC\u00a0pin.<\/p>\n<p>All good so far&#8230;<\/p>\n<p>Then I connected the A0 input to a variable resistor\u00a0to vary the input voltage voltage.<\/p>\n<p><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/ESP8266-schematic-amux-Test-Circuit-updated.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1608\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/ESP8266-schematic-amux-Test-Circuit-updated.jpg\" alt=\"ESP8266-schematic - amux-Test-Circuit-updated\" width=\"1438\" height=\"588\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/ESP8266-schematic-amux-Test-Circuit-updated.jpg 1438w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/ESP8266-schematic-amux-Test-Circuit-updated-300x123.jpg 300w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/ESP8266-schematic-amux-Test-Circuit-updated-768x314.jpg 768w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/ESP8266-schematic-amux-Test-Circuit-updated-1024x419.jpg 1024w\" sizes=\"auto, (max-width: 1438px) 100vw, 1438px\" \/><\/a><\/p>\n<p>At the same time, an external Digital Multimeter (DMM) was used to monitor the voltage applied to the ESP ADC input via the multiplexer. For each voltage setting, I recorded the DMM reading and the ADC count returned from the ESP8266. It would have been great to have a calibrated bus controlled DMM to automate this process, but for the purpose of this DIY home project, I simply entered the values into an excel spreadsheet manually.<\/p>\n<p>Thirty-seven voltage settings were applied in the range between 0.2V and 1.2V. Any voltage below 0.2V returned a 0 bit count while voltages above 1.2V returned 1024. I observed approximately 10 values returned from the ESP8266 for each voltage applied and recorded the high and low bit count readings for these measurements. The average of the high and low reading was then used to chart the results.<\/p>\n<p><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/graph.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-586\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/graph.jpg\" alt=\"ESP8266 ADC voltage vs bit count\" width=\"733\" height=\"444\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/graph.jpg 733w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/graph-300x182.jpg 300w\" sizes=\"auto, (max-width: 733px) 100vw, 733px\" \/><\/a><\/p>\n<p>As you can see, the results were reasonably linear. But there is compression\u00a0at the voltage extremes. For practical use, I would not use the non-linear ends, limiting the voltage swing from\u00a00.25 to 1.15 V, a 0.9 V range.<\/p>\n<p>The data collected can be reduced to a &#8220;best fit&#8221; equation. This would provide a method of converting the raw ADC value count to a voltage. Ideally, a data point should be collected for each ADC count value. The equipment I used for this exercise\u00a0was not calibrated or accurate enough to do this. Given these\u00a0uncertainties\u00a0in\u00a0my test setup, I am not sure if the variances I observed for consecutive readings&#8230;and they did vary somewhat&#8230;was result of the input voltage source or the ESP8266 ADC.<\/p>\n<p>Still&#8230;with the data I had collected, the formula was determined to be:<\/p>\n<p style=\"text-align: center;\"><strong>Vain = (0.94* ADCcount + 200) mV<\/strong><\/p>\n<p style=\"text-align: left;\">The slope was determined by dividing the voltage difference by the ADC count\u00a0\u00a0difference at each end of the voltage range. And 200 mV, the intercept, is the voltage when the ADC started reporting 0 for the count.<\/p>\n<p style=\"text-align: left;\">While there does not appear to be a definitive specification, this is consistent\u00a0with what I have read regarding the ESP8266. The 10-bit ADC resolution is supposed to be approximately 1 mV with a 1 V range.<\/p>\n<h3 style=\"text-align: center;\"><strong>Resolving Sensor Incompatibilities<\/strong><\/h3>\n<p style=\"text-align: left;\">So what can you do if your sensor range is outside the ESP8266 0.2 to 1.2 V\u00a0window?<\/p>\n<p style=\"text-align: left;\">Scaling of course.<\/p>\n<p style=\"text-align: left;\">This can be accomplished passively, using a resistor divider, or actively, with the addition of an op-amp. The interface of choice would be an active op-amp based circuit.<\/p>\n<p style=\"text-align: left;\">For example, suppose your sensor has a 0-5V range. This would need to be scaled to fit in the ESP8266 0.2 to 1.2 V input range. Obviously, resolution will be compromised as we go from a larger range to a smaller one, but it can be done. And with a single power supply.<\/p>\n<p style=\"text-align: left;\"><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/LevelShifter.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-591\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/LevelShifter-1024x692.jpg\" alt=\"ESP8266 Analog Input Voltage Level Shifter\" width=\"620\" height=\"419\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/LevelShifter-1024x692.jpg 1024w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/LevelShifter-300x203.jpg 300w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<p style=\"text-align: left;\">Resistor values calculated <a href=\"http:\/\/www.daycounter.com\/Calculators\/Op-Amp\/NonInverting-Op-Amp-Resistor-Calculator.phtml\" target=\"_blank\">here<\/a>. The 24K\/1K resistor divider supplies the 0.2V offset with the gain set for the 1.15 V output with a 5V maximum input applied. This circuit would need to be modified for your specific sensor voltage range.<\/p>\n<h3 style=\"text-align: center;\"><strong>In summary<\/strong><\/h3>\n<p style=\"text-align: left;\">So there you have it. Eight analog inputs measured using a single\u00a0ESP8266 SoC. Possible simply by adding a 22 cent chip.<\/p>\n<p style=\"text-align: left;\">And I got to mention one more option. Just in case you want even more&#8230;<\/p>\n<p style=\"text-align: left;\">A 16 channel version\u00a0of the multiplexer\u00a0is also available (74HCT4067). That&#8217;s a whole lot of\u00a0analog!<\/p>\n<p style=\"text-align: left;\">I hope you find this information useful.<\/p>\n<p style=\"text-align: left;\">\n","protected":false},"excerpt":{"rendered":"<p>Want More Analog Inputs? Do you have a project needing more than one analog input? If your using an ESP8266, that would seem to be a problem as it only &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,10,19],"tags":[11,33,32],"class_list":["post-530","post","type-post","status-publish","format-standard","hentry","category-alltheposts","category-esp8266","category-internet-of-things","tag-esp8266","tag-expand-esp8266-capability","tag-more-esp8266-analog-inputs"],"_links":{"self":[{"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/posts\/530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=530"}],"version-history":[{"count":29,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/posts\/530\/revisions"}],"predecessor-version":[{"id":1609,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/posts\/530\/revisions\/1609"}],"wp:attachment":[{"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}