{"id":76146,"date":"2018-10-24T13:13:11","date_gmt":"2018-10-24T13:13:11","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=76146"},"modified":"2019-04-20T10:57:22","modified_gmt":"2019-04-20T10:57:22","slug":"micropython-gpios-esp32-esp8266","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/micropython-gpios-esp32-esp8266\/","title":{"rendered":"MicroPython with ESP32 and ESP8266: Interacting with GPIOs"},"content":{"rendered":"\n<p>In this article we&#8217;re going to take a look on how to interact with the ESP32 and ESP8266 GPIOs using MicroPython. We&#8217;ll show you how to read digital and analog inputs, how to control digital outputs and how to generate PWM signals.<\/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\/2018\/10\/micropython-esp32-esp8266-gpios.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-76189\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/micropython-esp32-esp8266-gpios.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/micropython-esp32-esp8266-gpios.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/micropython-esp32-esp8266-gpios.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/micropython-esp32-esp8266-gpios.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<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>To program the ESP32 and ESP8266 with MicroPython, we use uPyCraft IDE as a programming environment. Follow the next tutorials to install uPyCraft IDE and flash MicroPython firmware on your board:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Install uPyCraft IDE: <a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-windows-pc-instructions\/\" target=\"_blank\" rel=\"noreferrer noopener\">Windows PC<\/a>, <a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-mac-os-x-instructions\/\" target=\"_blank\" rel=\"noreferrer noopener\">MacOS X<\/a>, or&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-linux-ubuntu-instructions\/\" target=\"_blank\" rel=\"noreferrer noopener\">Linux Ubuntu<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/flash-upload-micropython-firmware-esp32-esp8266\/\" target=\"_blank\" rel=\"noreferrer noopener\">Flash\/Upload MicroPython Firmware to ESP32 and ESP8266<\/a><\/li><\/ul>\n\n\n\n<p>Alternatively, if you&#8217;re having trouble using uPyCraftIDE, we recommend using Thonny IDE instead: <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-thonny-micropython-python-ide-esp32-esp8266\/\">Getting Started with Thonny MicroPython (Python) IDE for ESP32 and ESP8266<\/a><\/p>\n\n\n\n<p>If this is your first time dealing with MicroPython you may find these next tutorials useful:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-micropython-esp32-esp8266\/\" target=\"_blank\" rel=\"noreferrer noopener\">Getting Started with MicroPython on ESP32 and ESP8266<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-basics-esp32-esp8266\/\" target=\"_blank\" rel=\"noreferrer noopener\">MicroPython Programming Basics with ESP32 and ESP8266<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Project Overview<\/h2>\n\n\n\n<p>With this tutorial you&#8217;ll learn how to use the ESP32 or ESP8266 GPIOs with MicroPython. You can read the separate guide for each topic:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-digital-inputs-digital-outputs-micropython\/\">Read digital inputs<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-digital-inputs-digital-outputs-micropython\/\">Control digital outputs<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-analog-readings-micropython\/\">Read analog inputs<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-pwm-micropython\/\">Generate PWM signals<\/a><\/li><\/ul>\n\n\n\n<p>We&#8217;ll build a simple example that works as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Read the state of a pushbutton and set the LED state accordingly &#8211; when you press the pushbutton the LED lights up.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"373\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/led_pushbutton.png?resize=750%2C373&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-76180\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/led_pushbutton.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/led_pushbutton.png?resize=300%2C149&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li>Read the voltage from a potentiometer and dim an LED accordingly to the shaft&#8217;s position of the potentiometer.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"152\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/led-fade.png?resize=750%2C152&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-76181\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/led-fade.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/led-fade.png?resize=300%2C61&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Schematic<\/h2>\n\n\n\n<p>The circuit for this project involves wiring two LEDs, a pushbutton, and a potentiometer. Here&#8217;s a list of all the parts needed to build the circuit:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32<\/a> or <a href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP8266<\/a>&nbsp;(read: <a href=\"https:\/\/makeradvisor.com\/esp32-vs-esp8266\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32 vs ESP8266<\/a>)<\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/3mm-5mm-leds-kit-storage-box\/\" target=\"_blank\" rel=\"noreferrer noopener\">2x LEDs<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noreferrer noopener\">2x 330 Ohm resistor<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/pushbuttons-kit\/\" target=\"_blank\" rel=\"noreferrer noopener\">Pushbutton<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/potentiometer-assortment-kit\/\" target=\"_blank\" rel=\"noreferrer noopener\">Potentiometer<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noreferrer noopener\">Breadboard<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\">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\">ESP32 &#8211; Schematic<\/h3>\n\n\n\n<p>Follow the next schematic diagram if you&#8217;re using an ESP32:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"826\" height=\"988\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/interacting-with-GPIOs-ESP32_bb.png?resize=826%2C988&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-76185\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/interacting-with-GPIOs-ESP32_bb.png?w=826&amp;quality=100&amp;strip=all&amp;ssl=1 826w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/interacting-with-GPIOs-ESP32_bb.png?resize=251%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 251w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/interacting-with-GPIOs-ESP32_bb.png?resize=768%2C919&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 826px) 100vw, 826px\" \/><\/figure><\/div>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Note: <\/strong> the ESP32 supports analog reading in several GPIOs: 0, 2, 4, 12, 13, 14, 15, 25, 26, 27 32, 33, 34, 35, 36, and 39.<\/p>\n\n\n\n<p><strong>Recommended reading:<\/strong>&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\" target=\"_blank\">ESP32 Pinout Reference: Which GPIO pins should you use?<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ESP8266 &#8211; Schematic<\/h3>\n\n\n\n<p>Follow the next schematic diagram if you&#8217;re using 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=\"688\" height=\"1054\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/interacting-with-GPIOs-ESP8266_bb.png?resize=688%2C1054&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-76186\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/interacting-with-GPIOs-ESP8266_bb.png?w=688&amp;quality=100&amp;strip=all&amp;ssl=1 688w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/interacting-with-GPIOs-ESP8266_bb.png?resize=196%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 196w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/interacting-with-GPIOs-ESP8266_bb.png?resize=668%2C1024&amp;quality=100&amp;strip=all&amp;ssl=1 668w\" sizes=\"(max-width: 688px) 100vw, 688px\" \/><\/figure><\/div>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Note: <\/strong> the ESP8266 only supports analog reading in pin ADC0 (A0).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code<\/h2>\n\n\n\n<p>Copy the following code to the <em>main.py<\/em> file in the uPyCraft IDE.<\/p>\n\n\n\n<p class=\"rntbox rntcorange\"><strong>Note: <\/strong>  analog reading works differently in ESP32 and ESP8266. The code works right away in ESP32. To use with ESP8266, you have to uncomment and comment the lines described in the MicroPython script.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\"># Complete project details at https:\/\/RandomNerdTutorials.com\n# Created by Rui Santos\n\nfrom machine import Pin, ADC, PWM\nfrom time import sleep\n\nled = Pin(2, Pin.OUT)\nbutton = Pin(15, Pin.IN)\n\n#Configure ADC for ESP32\npot = ADC(Pin(34))\npot.width(ADC.WIDTH_10BIT)\npot.atten(ADC.ATTN_11DB)\n\n#Configure ADC for ESP8266\n#pot = ADC(0)\n\nled_pwm = PWM(Pin(4),5000)\n\nwhile True:\n  button_state = button.value()\n  led.value(button_state)\n\n  pot_value = pot.read()\n  led_pwm.duty(pot_value)\n\n  sleep(0.1)\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP-MicroPython\/ESP32_ESP8266_Interact_GPIOs.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the code works<\/h3>\n\n\n\n<p>Continue reading to learn on how the code works.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Importing Libraries<\/h4>\n\n\n\n<p>To interact with the GPIOs you need to import the <span class=\"rnthl rntliteral\">machine<\/span> module that contains classes to interact with the GPIOs. Import the <span class=\"rnthl rntliteral\">Pin<\/span> class to interact with the pins, the <span class=\"rnthl rntliteral\">ADC<\/span> class to read analog value, and the <span class=\"rnthl rntliteral\">PWM<\/span> class to generate PWM signals.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from machine import Pin, ADC, PWM<\/code><\/pre>\n\n\n\n<p>Import the <span class=\"rnthl rntliteral\">sleep()<\/span> method from the <span class=\"rnthl rntliteral\">time<\/span> module. The <span class=\"rnthl rntliteral\">sleep()<\/span> method allows you to add delays to the code.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from time import sleep<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Instantiating Pins<\/h4>\n\n\n\n<p>After importing all the necessary modules, instantiate a <span class=\"rnthl rntliteral\">Pin<\/span> object called <span class=\"rnthl rntliteral\">led<\/span> on <span class=\"rnthl rntcorange\">GPIO 2<\/span> that is an OUTPUT.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led = Pin(2, Pin.OUT)<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">Pin<\/span> object accepts the following attributes in the following order:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>Pin(Pin number, pin mode, pull, value)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Pin number<\/strong> refers to the GPIO we want to control;<\/li><li><strong>Pin mode<\/strong> can be input (<span class=\"rnthl rntliteral\">IN<\/span>), output (<span class=\"rnthl rntliteral\">OUT<\/span>) or open-drain (<span class=\"rnthl rntliteral\">OPEN_DRAIN<\/span>);<\/li><li>The <strong>pull<\/strong> argument is used if we want to activate a pull up or pull down internal resistor (<span class=\"rnthl rntliteral\">PULL_UP<\/span>, or <span class=\"rnthl rntliteral\">PULL_DOWN<\/span>);<\/li><li>The <strong>value<\/strong>corresponds to the GPIO state (if is is on or off): it can be <span class=\"rnthl rntliteral\">0<\/span> or <span class=\"rnthl rntliteral\">1<\/span> (True or False).&nbsp;Setting <span class=\"rnthl rntliteral\">1<\/span> means the GPIO is on. If we don\u2019t pass any parameter, its state is <span class=\"rnthl rntliteral\">0<\/span> by default (that&#8217;s what we&#8217;ll do in this example).<\/li><\/ul>\n\n\n\n<p>After instantiating the <span class=\"rnthl rntliteral\">led<\/span> object, you need another instance of the <span class=\"rnthl rntliteral\">Pin<\/span> class for the pushbutton. The pushbutton is connected to <span class=\"rnthl rntcgreen\">GPIO 15<\/span> and it&#8217;s set as an input. So, it looks as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>button = Pin(15, Pin.IN)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Instantiating ADC<\/h4>\n\n\n\n<p>In the ESP32, to create an <span class=\"rnthl rntliteral\">ADC<\/span> object for the potentiometer on GPIO 34:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pot = ADC(Pin(34))<\/code><\/pre>\n\n\n\n<p>If you&#8217;re using an ESP8266, it only supports ADC on ADC0 (A0) pin. To instantiate an ADC object with the ESP8266:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pot = ADC(0)<\/code><\/pre>\n\n\n\n<p>The following line applies just to the ESP32. It defines that we want to be able to read voltage in full range.&nbsp;This means we want to read voltage from 0 to 3.3 V.<\/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>The next line means we want readings with 10 bit resolution (from 0 to 1023)<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pot.width(ADC.WIDTH_10BIT)<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">width()<\/span> method accepts other parameters to set other resolutions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>WIDTH_9BIT<\/strong>: range 0 to 511<\/li><li><strong>WIDTH_10BIT<\/strong>: range 0 to 1023<\/li><li><strong>WIDTH_11BIT<\/strong>: range 0 to 2047<\/li><li><strong>WIDTH_12BIT<\/strong>: range 0 to 4095<\/li><\/ul>\n\n\n\n<p>If you don&#8217;t specify the resolution, it will be 12-bit resolution by default on the ESP32.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Instantiating PWM<\/h4>\n\n\n\n<p>Then, create a <span class=\"rnthl rntliteral\">PWM<\/span> object called <span class=\"rnthl rntliteral\">led_pwm<\/span> on <span class=\"rnthl rntcblue\">GPIO 4<\/span> with 5000 Hz.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led_pwm = PWM(Pin(4), 5000)<\/code><\/pre>\n\n\n\n<p>To create a <span class=\"rnthl rntliteral\">PWM<\/span> object, you need to pass as parameters: pin, signal&#8217;s frequency, and duty cycle.<\/p>\n\n\n\n<p>The <strong>frequency <\/strong>can be a value between 0 and 78125. A frequency of 5000 Hz for an LED works just fine.<\/p>\n\n\n\n<p>The <strong>duty cycle<\/strong> can be a value between 0 and 1023. In which 1023 corresponds to 100% duty cycle (full brightness), and 0 corresponds to 0% duty cycle (unlit LED).<\/p>\n\n\n\n<p>We\u2019ll just set the duty in the while loop, so we don\u2019t need to pass the duty cycle parameter at the moment. If you don\u2019t set the duty cycle when instantiating the <span class=\"rnthl rntliteral\">PWM<\/span> object, it will be 0 by default.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Getting the GPIO state<\/h4>\n\n\n\n<p>Then, we have a while loop that is always True. This is similar to the <span class=\"rnthl rntliteral\">loop()<\/span> function in the Arduino IDE.<\/p>\n\n\n\n<p>We start by getting the button state and save it in the <span class=\"rnthl rntliteral\">button_state<\/span> variable. To get the pin state use the <span class=\"rnthl rntliteral\">value()<\/span> method as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>button_state = button.value()<\/code><\/pre>\n\n\n\n<p>This returns 1 or 0 depending on whether the button is pressed or not.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Setting the GPIO state<\/h4>\n\n\n\n<p>To set the pin state, use the <span class=\"rnthl rntliteral\">value(state)<\/span> method in the <span class=\"rnthl rntliteral\">Pin<\/span> object. In this case we&#8217;re setting the <span class=\"rnthl rntliteral\">button_state<\/span> variable as an argument. This way the LED turns on when we press the pushbutton:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led.value(button_state)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Reading analog inputs<\/h4>\n\n\n\n<p>To read an analog input, use the <span class=\"rnthl rntliteral\">read()<\/span> method on an <span class=\"rnthl rntliteral\">ADC<\/span> object (in this case the ADC object is called <span class=\"rnthl rntliteral\">pot<\/span>).<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pot_value = pot.read()<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Controlling duty cycle<\/h4>\n\n\n\n<p>To control the duty cycle, use the <span class=\"rnthl rntliteral\">duty()<\/span> method on the PWM object (<span class=\"rnthl rntliteral\">led_pwm<\/span>). The <span class=\"rnthl rntliteral\">duty()<\/span> method accepts a value between 0 and 1023 (in which 0 corresponds to 0% duty cycle, and 1023 to 100% duty cycle). So, pass as argument the <span class=\"rnthl rntliteral\">pot_value<\/span> (that varies between 0 and 1023). This way you change the duty cycle by rotating the potentiometer.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led_pwm.duty(pot_value)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Testing the Code<\/h3>\n\n\n\n<p>Upload the <em>main.py<\/em> file to your ESP32 or ESP8266. For that, open uPyCraft IDE and copy the code provided to the <em>main.py<\/em> file. Go to <strong>Tools<\/strong> &gt; <strong>Serial<\/strong> and select the serial port. Select your board in <strong>Tools<\/strong> &gt; <strong>Board<\/strong>.<\/p>\n\n\n\n<p>Then, upload the code to the ESP32 or ESP8266 by pressing the <strong>Download and Run<\/strong> button.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"238\" height=\"45\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/uPycraft-ide-tools-menu-download-and-run.png?resize=238%2C45&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-75060\"\/><\/figure><\/div>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Note: <\/strong> to get you familiar with uPyCraft IDE youn can read the following tutorial &#8211; <a rel=\"noreferrer noopener\" href=\"https:\/\/randomnerdtutorials.com\/getting-started-micropython-esp32-esp8266\/\" target=\"_blank\">Getting Started with MicroPython on ESP32 and ESP8266<\/a><\/p>\n\n\n\n<p>After uploading the code, press the ESP32\/ESP8266 on-board EN\/RST button to run the new script.<\/p>\n\n\n\n<p>Now, test your setup. The LED should light up when you press the pushbutton.<\/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=\"500\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/esp32-es8266-micropython-pushbutton.jpg?resize=750%2C500&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-76198\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/esp32-es8266-micropython-pushbutton.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/esp32-es8266-micropython-pushbutton.jpg?resize=300%2C200&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>The LED brightness changes when you 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=\"500\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/esp32-es8266-micropython-pot-pwm.jpg?resize=750%2C500&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-76197\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/esp32-es8266-micropython-pot-pwm.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/esp32-es8266-micropython-pot-pwm.jpg?resize=300%2C200&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>This simple example showed you how to read digital and analog inputs, control digital outputs and generate PWM signals with the ESP32 and ESP8266 boards using MicroPython. <\/p>\n\n\n\n<p>If you like MicroPython, you may like the following projects:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-interrupts-esp32-esp8266\/\">ESP32\/ESP8266 MicroPython Interrrupts<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-ws2812b-addressable-rgb-leds-neopixel-esp32-esp8266\/\">MicroPython: WS2812B Addressable RGB LEDs with ESP32\/ESP8266<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/low-power-weather-station-datalogger-using-esp8266-bme280-micropython\/\">Low Power Weather Station Datalogger using ESP8266 and BME280 with MicroPython<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-micropython-web-server\/\">ESP32\/ESP8266 MicroPython Web Server<\/a><\/li><\/ul>\n\n\n\n<p>We hope you\u2019ve found this article about how to control ESP32 and ESP8266 GPIOs with MicroPython useful. If you want to learn more about MicroPython, make sure you take a look at our eBook: <strong><a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\">MicroPython Programming with ESP32 and ESP8266<\/a>.<\/strong><\/p>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article we&#8217;re going to take a look on how to interact with the ESP32 and ESP8266 GPIOs using MicroPython. We&#8217;ll show you how to read digital and analog &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"MicroPython with ESP32 and ESP8266: Interacting with GPIOs\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/micropython-gpios-esp32-esp8266\/#more-76146\" aria-label=\"Read more about MicroPython with ESP32 and ESP8266: Interacting with GPIOs\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":76189,"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":[281,276,265,309,264],"tags":[],"class_list":["post-76146","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp32","category-esp8266-project","category-0-esp32-micropython","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/micropython-esp32-esp8266-gpios.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\/76146","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=76146"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/76146\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/76189"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=76146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=76146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=76146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}