ESP32/ESP8266 Digital Inputs and Digital Outputs with MicroPython

This tutorial shows how to control the ESP32 and ESP8266 GPIOs as digital inputs and digital outputs using MicroPython firmware. As an example, you’ll learn how to read the value of a pushbutton and light up an LED accordingly.

esp32 esp8266 micropython inputs and outputs

Prerequisites

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:

If this is your first time dealing with MicroPython, we recommend following this guide: Getting Started with MicroPython on ESP32 and ESP8266

Project Overview

To show you how to use digital inputs and digital outputs, we’ll build a simple project example with a pushbutton and an LED. We’ll read the state of the pushbutton and light up the LED accordingly as illustrated in the following figure.

Digital Inputs

To get the value of a GPIO, first you need to create a Pin object and set it as an input. For example:

button = Pin(4, Pin.IN)

Then, to get is value, you need to use the value() method on the Pin object without passing any argument. For example, to get the state of a Pin object called button, use the following expression:

button.value()

We’ll show you in more detail how everything works in the project example.

Digital Outputs

To set a GPIO on or off, first you need to set it as an output. For example:

led = Pin(5, Pin.OUT)

To control the GPIO, use the value() method on the Pin object and pass 1 or 0 as argument. For example, the following command sets a Pin object (led) to HIGH:

led.value(1)

To set the GPIO to LOW, pass 0 as argument:

led.value(0)

Schematic

Before proceeding, you need to assemble a circuit with an LED and a pushbutton. We’ll connect the LED to GPIO 5 and the pushbutton to GPIO 4.

Parts Required

Here’s a list of the parts to you need to build the circuit:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematic – ESP32

Follow the next schematic diagram if you’re using an ESP32 board:

Schematic – ESP8266

Follow the next schematic diagram if you’re using an ESP8266 board:

On the ESP8266, the pin marked as D1 corresponds to GPIO 5 and the pin marked as D2 corresponds to GPIO 4.

Script

The following code reads the state of the pushbutton and lights up the LED accordingly. The code works for both the ESP32 and ESP8266 boards.

# Complete project details at https://RandomNerdTutorials.com

from machine import Pin
from time import sleep

led = Pin(5, Pin.OUT)
button = Pin(4, Pin.IN)

while True:
  led.value(button.value())
  sleep(0.1)

View raw code

How the Code Works

You start by importing the Pin class from the machine module, and the sleep class from the time module.

from machine import Pin
from time import sleep

Then, create a Pin object called led on GPIO 5. LEDs are outputs, so pass as second argument Pin.OUT.

led = Pin(5, Pin.OUT)

We also create an object called button on GPIO 4. Buttons are inputs, so use Pin.IN.

button = Pin(4, Pin.IN)

Use button.value() to return/read the button state.Then, pass the button.value() expression as an argument to the LED value.

led.value(button.value())

This way, when we press the button, button.value() returns 1. So, this is the same as having led.value(1). This sets the LED state to 1, lighting up the LED. When the pushbutton is not being pressed, button.value() returns 0. So, we have led.value(0), and the LED stays off.

Demonstration

Save the code to your ESP board using Thonny IDE or uPyCraft IDE. Then, the LED should light up when you press the button and stay off when you release it.

Wrapping Up

To wrap up, to read the value of a GPIO, we simply need to use the value() method on the corresponding Pin object. To set the value of a GPIO, we just need to pass as argument 1 or 0 to the value() method to set it on or off, respectively.

We hope you’ve found this tutorial useful. If you’re just getting started with MicroPython, you may also like the following resources:

Thanks for reading.


Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our weekly newsletter!

4 thoughts on “ESP32/ESP8266 Digital Inputs and Digital Outputs with MicroPython”

  1. Dear Rui Santos,
    There is no problem running Micro Python code during ESP32 is connected to computer. However, once unplug the board from computer it look like erase all the flash memory. I have to download code again to run it and keep connect with computer. Download from Thonny IDE and uPyCraft IDE do the same. Is there a way permanently store micro Python code to ESP32 ?
    Thanks for your help.
    Se

    • Hi.
      Did you actually download the code to the ESP32, or did you just hit the “Run” button on the IDE?
      If you press the “run” button, the code just runs and it doesn’t download to the board.
      Regards,
      Sara

Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.