Design a site like this with
Get started

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.


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:


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:


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



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:

  • ESP32 or ESP8266 (read: ESP32 vs ESP8266)
  • 5 mm LED
  • 330 Ohm resistor
  • Pushbutton
  • 10k Ohm resistor
  • Breadboard
  • Jumper wires

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.


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.

from machine import Pin
from time import sleep

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

while True:

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.


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.


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


Published by Gnd_To_Vcc

Here to spread my knowledge . Knowledge should always be spread not stored.

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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: