{"id":2014,"date":"2015-01-23T20:45:37","date_gmt":"2015-01-23T20:45:37","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=2014"},"modified":"2015-03-06T04:38:51","modified_gmt":"2015-03-06T04:38:51","slug":"esp8266-analogue-input","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2015\/01\/esp8266-analogue-input\/","title":{"rendered":"esp8266 analogue input"},"content":{"rendered":"<p><a href=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/01\/esp1_analogue.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/01\/esp1_analogue.jpg\" alt=\"esp1_analogue\" width=\"608\" height=\"528\" class=\"aligncenter size-full wp-image-2015\" srcset=\"https:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/01\/esp1_analogue.jpg 608w, https:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/01\/esp1_analogue-300x261.jpg 300w\" sizes=\"auto, (max-width: 608px) 100vw, 608px\" \/><\/a><\/p>\n<p>The esp8266 contains a 10bit ADC. There are apparently various issues, including instability when transmitting over Wifi, and the lack of a voltage reference. However it is usable. The analogue input is labeled &#8220;tout&#8221; on the pinout below:<\/p>\n<p><a href=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2014\/12\/esp8266_pinout.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2014\/12\/esp8266_pinout.jpg\" alt=\"esp8266_pinout\" width=\"557\" height=\"603\" class=\"aligncenter size-full wp-image-1827\" srcset=\"https:\/\/41j.com\/blog\/wp-content\/uploads\/2014\/12\/esp8266_pinout.jpg 557w, https:\/\/41j.com\/blog\/wp-content\/uploads\/2014\/12\/esp8266_pinout-277x300.jpg 277w, https:\/\/41j.com\/blog\/wp-content\/uploads\/2014\/12\/esp8266_pinout-300x325.jpg 300w\" sizes=\"auto, (max-width: 557px) 100vw, 557px\" \/><\/a><\/p>\n<p>Unfortunately this isn&#8217;t broken out on the cheapest esp8266 boards (the esp1) but it&#8217;s possible to get access to it with some wirewrap wire and careful soldering (see image above).<\/p>\n<p>The ADC input can then be read using some relatively simple C code as shown below. This code was adapted from an old version of the SDK (it doesn&#8217;t seem to be present in the current version).<\/p>\n<p>I&#8217;ve also attached a complete working example that uses the function below: <a href=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/01\/adc_test.tar.gz\">adc_test.tar<\/a><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nuint16 ICACHE_FLASH_ATTR adc_read(void) {\r\n  uint8 i;\r\n  uint16 sar_dout, tout, sardata&#x5B;8];\r\n\r\n  rom_i2c_writeReg_Mask(0x6C,2,0,5,5,1);\r\n\r\n  \/\/ disable interrupts?\r\n  SET_PERI_REG_MASK(0x60000D5C, 0x200000);\r\n  while (GET_PERI_REG_BITS(0x60000D50, 26, 24) &gt; 0); \/\/wait r_state == 0\r\n  sar_dout = 0;\r\n  CLEAR_PERI_REG_MASK(0x60000D50, 0x02); \/\/force_en=0\r\n  SET_PERI_REG_MASK(0x60000D50, 0x02); \/\/force_en=1\r\n\r\n  os_delay_us(2);\r\n  while (GET_PERI_REG_BITS(0x60000D50, 26, 24) &gt; 0); \/\/wait r_state == 0\r\n  read_sar_dout(sardata);\r\n\r\n  \/\/ Could this be averaging 8 readings? If anyone has any info please comment.\r\n  for (i = 0; i &lt; 8; i++) {\r\n    sar_dout += sardata&#x5B;i];\r\n  }\r\n  tout = (sar_dout + 8) &gt;&gt; 4; \/\/tout is 10 bits fraction\r\n\r\n  \/\/ I assume this code re-enables interrupts\r\n  rom_i2c_writeReg_Mask(0x6C,2,0,5,5,1);\r\n  while (GET_PERI_REG_BITS(0x60000D50, 26, 24) &gt; 0); \/\/wait r_state == 0\r\n  CLEAR_PERI_REG_MASK(0x60000D5C, 0x200000);\r\n  SET_PERI_REG_MASK(0x60000D60, 0x1); \/\/force_en=1\r\n  CLEAR_PERI_REG_MASK(0x60000D60, 0x1); \/\/force_en=1\r\n  return tout; \/\/tout is 10 bits fraction\r\n}\r\n<\/pre>\n<p>UPDATE: There&#8217;s also a system_adc_read function in the library symbols, I&#8217;ve not used it but it could be worth looking at.<\/p>\n<p><\/p>\n<h4>This is a continuation of my notes on the esp8266 microcontroller, you can find a complete list of my esp8266 posts <a href=\"http:\/\/41j.com\/blog\/2015\/01\/esp8266-notes-summary\/\">here<\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>The esp8266 contains a 10bit ADC. There are apparently various issues, including instability when transmitting over Wifi, and the lack of a voltage reference. However it is usable. The analogue input is labeled &#8220;tout&#8221; on the pinout below: Unfortunately this isn&#8217;t broken out on the cheapest esp8266 boards (the esp1) but it&#8217;s possible to get [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-2014","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-wu","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2014","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=2014"}],"version-history":[{"count":3,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2014\/revisions"}],"predecessor-version":[{"id":2204,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2014\/revisions\/2204"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=2014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=2014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=2014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}