{"id":84186,"date":"2019-04-18T14:08:00","date_gmt":"2019-04-18T14:08:00","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=84186"},"modified":"2019-05-06T13:37:40","modified_gmt":"2019-05-06T13:37:40","slug":"esp32-esp8266-analog-readings-micropython","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-esp8266-analog-readings-micropython\/","title":{"rendered":"ESP32\/ESP8266 Analog Readings with MicroPython"},"content":{"rendered":"\n<p>This tutorial shows how to read analog values with the ESP32 and ESP8266 boards using MicroPython firmware. As an example, we&#8217;ll read the values from a potentiometer.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog_in_esp32_esp8266_micropython.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 Analog Readings with MicroPython\" class=\"wp-image-84192\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog_in_esp32_esp8266_micropython.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog_in_esp32_esp8266_micropython.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog_in_esp32_esp8266_micropython.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog_in_esp32_esp8266_micropython.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p>Getting analog readings with ESP32 and ESP8266 is a bit different, so there is a section for each board in this tutorial. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>To follow this tutorial you need MicroPython firmware installed in your ESP32 or ESP8266 boards. You also need an IDE to write and upload the code to your board. We suggest using Thonny IDE or uPyCraft IDE:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Thonny IDE:<ul><li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-thonny-micropython-python-ide-esp32-esp8266\/\">Installing and getting started with Thonny IDE<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/flashing-micropython-firmware-esptool-py-esp32-esp8266\/\">Flashing MicroPython Firmware with esptool.py<\/a><\/li><\/ul><\/li><li>uPyCraft IDE:<ul><li>Install uPyCraft IDE (<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-windows-pc-instructions\/\">Windows<\/a>,&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-mac-os-x-instructions\/\">Mac OS X<\/a>,&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-linux-ubuntu-instructions\/\">Linux<\/a>)<\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/flash-upload-micropython-firmware-esp32-esp8266\/\">Flash\/Upload MicroPython Firmware to ESP32 and ESP8266<\/a><\/li><\/ul><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Analog Readings &#8211; ESP8266<\/h2>\n\n\n\n<p>ESP8266 only has one analog pin called <span class=\"rnthl rntcorange\">A0<\/span>. The ESP8266 analog pin has 10-bit resolution. It reads the voltage from 0 to 3.3V and then, assigns a value between 0 and 1023.<\/p>\n\n\n\n<p class=\"rntbox rntclgreen\"><strong>Note: <\/strong> some versions of the  ESP8266 only read a maximum of 1V on the ADC pin. Make sure you don\u2019t exceed the maximum recommended voltage for your board.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Analog Readings &#8211; ESP32<\/h2>\n\n\n\n<p>There are several pins on the ESP32 that can act as analog pins &#8211; these are called ADC pins. All the following GPIOs can act as ADC pins: 0, 2, 4, 12, 13, 14, 15, 25, 26, 27, 32, 33, 34, 35, 36, and 39.<\/p>\n\n\n\n<p class=\"rntbox rntclblue\">Learn more about the ESP32 GPIOs: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\">ESP32 Pinout Reference: Which GPIO pins should you use?<\/a><\/p>\n\n\n\n<p>ESP32 ADC pins have 12-bit resolution by default. These pins read voltage between 0 and 3.3V and then return a value between 0 and 4095. The resolution can be changed on the code. For example, you may want to have just 10-bit resolution to get a value between 0 and 1023.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"700\" height=\"320\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog-readings.png?resize=700%2C320&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"analog reading esp32 esp8266 micropython\" class=\"wp-image-84196\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog-readings.png?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog-readings.png?resize=300%2C137&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/figure><\/div>\n\n\n\n<p>The following table shows some differences between analog reading on the ESP8266 and the ESP32.<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td><\/td><td><strong>ESP8266<\/strong><\/td><td><strong>ESP32<\/strong><\/td><\/tr><tr><td><strong>Analog pins<\/strong><\/td><td>A0 (ADC 0)<\/td><td>GPIOs: 0, 2, 4, 12, 13, 14, 15,<br> 25, 26, 27, 32, 33, 34, 35, 36,<br> and 39. <\/td><\/tr><tr><td><strong>Resolution<\/strong><\/td><td>10-bit (0-1023)<\/td><td>12-bit (0-4095)<\/td><\/tr><tr><td><strong>Change resolution<\/strong><\/td><td>No<\/td><td>Yes<\/td><\/tr><\/tbody><\/table>\n\n\n\n<h2 class=\"wp-block-heading\">Schematic<\/h2>\n\n\n\n<p>Analog reading works differently in ESP32 and ESP8266. There is a different schematic and a different script for each board.<\/p>\n\n\n\n<p>To follow this tutorial, you need to wire a potentiometer to your ESP8266 or ESP32 board.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Parts Required<\/h3>\n\n\n\n<p>Here&#8217;s a list of the parts to you need to build the circuit:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">ESP32<\/a>&nbsp;or&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\">ESP8266<\/a>&nbsp;(read:&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/esp32-vs-esp8266\/\" target=\"_blank\">ESP32 vs ESP8266<\/a>)<\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/potentiometer-assortment-kit\/\">Potentiometer<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">Breadboard<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\">Jumper wires<\/a><\/li><\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Schematic &#8211; ESP32<\/h3>\n\n\n\n<p>Follow the next schematic diagram if you\u2019re using an ESP32 board:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"1200\" height=\"511\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog_input_pot_esp32.png?resize=1200%2C511&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Analog Readings with MicroPython circuit schematic diagram\" class=\"wp-image-84187\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog_input_pot_esp32.png?w=1254&amp;quality=100&amp;strip=all&amp;ssl=1 1254w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog_input_pot_esp32.png?resize=300%2C128&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog_input_pot_esp32.png?resize=768%2C327&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog_input_pot_esp32.png?resize=1024%2C436&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p>In this example we\u2019re using <span class=\"rnthl rntcorange\">GPIO 34<\/span> to read analog values from the potentiometer, but you can choose any other GPIO that supports ADC. Read our <a href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\">ESP32 Pinout Guide<\/a> to learn more about the ESP32 GPIOs. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Schematic \u2013 ESP8266<\/h3>\n\n\n\n<p>Follow the next schematic diagram if you\u2019re using an ESP8266 board:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"537\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog_input_pot_esp8266_bb.png?resize=1200%2C537&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 Analog Readings with MicroPython circuit schematic diagram\" class=\"wp-image-84188\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog_input_pot_esp8266_bb.png?w=1200&amp;quality=100&amp;strip=all&amp;ssl=1 1200w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog_input_pot_esp8266_bb.png?resize=300%2C134&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog_input_pot_esp8266_bb.png?resize=768%2C344&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog_input_pot_esp8266_bb.png?resize=1024%2C458&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p>The ESP8266 supports analog reading only on the <span class=\"rnthl rntcorange\">A0<\/span> pin.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Script<\/h2>\n\n\n\n<p>There are a few differences when it comes to analog reading in ESP32 and ESP8266 regarding the code. You should write a slightly different script depending on the board you\u2019re using. Make sure you follow the code for your specific board.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Script &#8211; ESP32<\/h3>\n\n\n\n<p>The following script for the ESP32 reads analog values from <span class=\"rnthl rntcorange\">GPIO 34<\/span>.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete project details at https:\/\/RandomNerdTutorials.com\/micropython-programming-with-esp32-and-esp8266\/\n\nfrom machine import Pin, ADC\nfrom time import sleep\n\npot = ADC(Pin(34))\npot.atten(ADC.ATTN_11DB)       #Full range: 3.3v\n\nwhile True:\n  pot_value = pot.read()\n  print(pot_value)\n  sleep(0.1)\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP-MicroPython\/raw\/master\/code\/GPIOs\/Analog_Read_Pot\/read_pot_esp32.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How the code works<\/h4>\n\n\n\n<p>To read analog inputs, import the <span class=\"rnthl rntliteral\">ADC<\/span> class in addition to the <span class=\"rnthl rntliteral\">Pin<\/span> class from the <span class=\"rnthl rntliteral\">machine<\/span> module. We also import the <span class=\"rnthl rntliteral\">sleep<\/span> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from machine import Pin, ADC\nfrom time import sleep<\/code><\/pre>\n\n\n\n<p>Then, create an <span class=\"rnthl rntliteral\">ADC<\/span> object called pot on <span class=\"rnthl rntcorange\">GPIO 34<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pot = ADC(Pin(34))<\/code><\/pre>\n\n\n\n<p>The following line defines that we want to be able to read voltage in full range.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pot.atten(ADC.ATTN_11DB)<\/code><\/pre>\n\n\n\n<p>This means we want to read voltage from 0 to 3.3V. This corresponds to setting the attenuation ratio of 11db. For that, we use the <span class=\"rnthl rntliteral\">atten()<\/span> method and pass as argument: <span class=\"rnthl rntliteral\">ADC.ATTN_11DB<\/span>.<\/p>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">atten()<\/span> method can take the following arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><span class=\"rnthl rntliteral\">ADC.ATTN_0DB<\/span> \u2014 the full range voltage: 1.2V<\/li><li><span class=\"rnthl rntliteral\">ADC.ATTN_2_5DB<\/span> \u2014 the full range voltage: 1.5V<\/li><li><span class=\"rnthl rntliteral\">ADC.ATTN_6DB<\/span> \u2014 the full range voltage: 2.0V<\/li><li><span class=\"rnthl rntliteral\">ADC.ATTN_11DB<\/span> \u2014 the full range voltage: 3.3V<\/li><\/ul>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">while<\/span> loop, read the pot value and save it in the <span class=\"rnthl rntliteral\">pot_value<\/span> variable. To read the value from the pot, simply use the <span class=\"rnthl rntliteral\">read()<\/span> method on the <span class=\"rnthl rntliteral\">pot<\/span> object.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pot_value = pot.read()<\/code><\/pre>\n\n\n\n<p>Then, print the pot value.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>print(pot_value)<\/code><\/pre>\n\n\n\n<p> At the end, add a delay of 100 ms.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>sleep(0.1)<\/code><\/pre>\n\n\n\n<p>When you rotate the potentiometer, you get values from 0 to 4095 &#8211; that\u2019s because the ADC pins have a 12-bit resolution by default. You may want to get values in other ranges. You can change the resolution using the <span class=\"rnthl rntliteral\">width()<\/span> method as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>ADC.width(bit)<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">bit<\/span> argument can be one of the following parameters:<br><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><span class=\"rnthl rntliteral\">ADC.WIDTH_9BIT<\/span>: range 0 to 511<\/li><li><span class=\"rnthl rntliteral\">ADC.WIDTH_10BIT<\/span>: range 0 to 1023<\/li><li><span class=\"rnthl rntliteral\">ADC.WIDTH_11BIT<\/span>: range 0 to 2047<\/li><li><span class=\"rnthl rntliteral\">ADC.WIDTH_12BIT<\/span>: range 0 to 4095 <\/li><\/ul>\n\n\n\n<p> For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>ADC.width(ADC.WIDTH_12BIT)<\/code><\/pre>\n\n\n\n<p> In summary:<br><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>To read an analog value you need to import the <span class=\"rnthl rntliteral\">ADC<\/span> class;<\/li><li>To create an <span class=\"rnthl rntliteral\">ADC<\/span> object simply use <span class=\"rnthl rntliteral\">ADC(Pin(GPIO))<\/span>, in which <span class=\"rnthl rntliteral\">GPIO<\/span> is the number of the GPIO you want to read the analog values;<\/li><li>To read the analog value, simply use the <span class=\"rnthl rntliteral\">read()<\/span> method on the <span class=\"rnthl rntliteral\">ADC<\/span> object.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Script \u2013 ESP8266<\/h3>\n\n\n\n<p>The following script for the ESP8266 reads analog values from <span class=\"rnthl rntcorange\">A0<\/span> pin.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete project details at https:\/\/RandomNerdTutorials.com\/micropython-programming-with-esp32-and-esp8266\/\n\nfrom machine import Pin, ADC\nfrom time import sleep\n\npot = ADC(0)\n\nwhile True:\n  pot_value = pot.read()\n  print(pot_value)\n  sleep(0.1)\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP-MicroPython\/raw\/master\/code\/GPIOs\/Analog_Read_Pot\/read_pot_esp8266.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How the code works<\/h4>\n\n\n\n<p>To read analog inputs, import the <span class=\"rnthl rntliteral\">ADC<\/span> class in addition to the <span class=\"rnthl rntliteral\">Pin<\/span> class from the <span class=\"rnthl rntliteral\">machine<\/span> module. We also import the <span class=\"rnthl rntliteral\">sleep<\/span> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from machine import Pin, ADC\nfrom time import sleep<\/code><\/pre>\n\n\n\n<p>Then, create an <span class=\"rnthl rntliteral\">ADC<\/span> object called <span class=\"rnthl rntliteral\">pot<\/span> on <span class=\"rnthl rntcorange\">A0<\/span> pin.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pot = ADC(0)<\/code><\/pre>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Note: <\/strong> ADC0 (A0) is the only pin on the ESP8266 that supports analog reading.<\/p>\n\n\n\n<p>In the loop, read the pot value and save it in the <span class=\"rnthl rntliteral\">pot_value<\/span> variable. To read the value from the pot, use the <span class=\"rnthl rntliteral\">read()<\/span> method on the <span class=\"rnthl rntliteral\">pot<\/span> object.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pot_value = pot.read()<\/code><\/pre>\n\n\n\n<p>Then, print the <span class=\"rnthl rntliteral\">pot_value<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>print(pot_value)<\/code><\/pre>\n\n\n\n<p>At the end, add a delay of 100 ms.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>sleep(0.1)<\/code><\/pre>\n\n\n\n<p>In summary:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>To read an analog value you use the <span class=\"rnthl rntliteral\">ADC<\/span> class;<\/li><li>To create an <span class=\"rnthl rntliteral\">ADC<\/span> object simply call <span class=\"rnthl rntliteral\">ADC(0)<\/span>. The ESP8266 only supports ADC reading on <span class=\"rnthl rntcorange\">A0<\/span> pin.<\/li><li>To read the analog value, use the <span class=\"rnthl rntliteral\">read()<\/span> method on the <span class=\"rnthl rntliteral\">ADC<\/span> object.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<br><\/h2>\n\n\n\n<p>After saving the code to your ESP board using <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-thonny-micropython-python-ide-esp32-esp8266\/\">Thonny IDE<\/a> or <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-micropython-esp32-esp8266\/\">uPyCraft IDE<\/a>, rotate the potentiometer.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"448\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/esp32-micropython.jpg?resize=750%2C448&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 Analog Readings with MicroPython circuit schematic diagram\" class=\"wp-image-84189\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/esp32-micropython.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/esp32-micropython.jpg?resize=300%2C179&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>Check the shell of your MicroPython IDE to read the values from the potentiometer. If you\u2019re using an ESP32 you should get readings between 0 and 4095 \u2014 or readings between 0 and 1023 with an ESP8266.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"926\" height=\"587\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog_readings_shell.png?resize=926%2C587&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 Analog Readings with MicroPython demonstration\" class=\"wp-image-84190\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog_readings_shell.png?w=926&amp;quality=100&amp;strip=all&amp;ssl=1 926w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog_readings_shell.png?resize=300%2C190&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog_readings_shell.png?resize=768%2C487&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 926px) 100vw, 926px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial we&#8217;ve shown you how to read analog values using MicroPython with the ESP32 and ESP8266 boards. There are several GPIOs on the ESP32 that can read analog values. On the other side, the ESP8266 only supports analog readings on the <span class=\"rnthl rntcorange\">A0<\/span> (ADC0) pin.<\/p>\n\n\n\n<p>Reading analog values with the ESP32 and ESP8266 is slightly different, but in summary, you need to create an <span class=\"rnthl rntliteral\">ADC<\/span> object, and then use the <span class=\"rnthl rntliteral\">read()<\/span> method to get the values.<\/p>\n\n\n\n<p>We hope you&#8217;ve found this tutorial useful. If you&#8217;re just getting started with MicroPython, you may also like the following resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\"><strong>[eBook] MicroPython Programming with EPS32\/ESP8266<\/strong><\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-gpios-esp32-esp8266\/\">ESP32\/ESP8266 GPIOs Explained with MicroPython<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-digital-inputs-digital-outputs-micropython\/\">ESP32\/ESP8266 Digital Inputs and Digital Outputs with MicroPython<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-pwm-micropython\/\">ESP32\/ESP8266 PWM with MicroPython \u2013 Dim LED<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-micropython-web-server\/\">ESP32\/ESP8266 MicroPython Web Server \u2013 Control Outputs<\/a><\/li><\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to read analog values with the ESP32 and ESP8266 boards using MicroPython firmware. As an example, we&#8217;ll read the values from a potentiometer. Getting analog readings &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32\/ESP8266 Analog Readings with MicroPython\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-analog-readings-micropython\/#more-84186\" aria-label=\"Read more about ESP32\/ESP8266 Analog Readings with MicroPython\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":84192,"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":""},"categories":[276,281,230,309,264],"tags":[],"class_list":["post-84186","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32","category-esp32-project","category-guide","category-0-esp32-micropython","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/analog_in_esp32_esp8266_micropython.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/84186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=84186"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/84186\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/84192"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=84186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=84186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=84186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}