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.
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:
- Thonny IDE:
- uPyCraft IDE:
- Install uPyCraft IDE (Windows, Mac OS X, Linux)
- Flash/Upload MicroPython Firmware to ESP32 and ESP8266
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:
- ESP32 or ESP8266 (read: ESP32 vs ESP8266)
- 5 mm LED
- 330 Ohm resistor
- Pushbutton
- 10k Ohm resistor
- Breadboard
- Jumper wires
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/micropython-programming-with-esp32-and-esp8266/
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)
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:
- [eBook] MicroPython Programming with EPS32/ESP8266
- ESP32/ESP8266 GPIOs Explained with MicroPython
- ESP32/ESP8266 Analog Readings with MicroPython
- ESP32/ESP8266 PWM with MicroPython – Dim LED
- ESP32/ESP8266 MicroPython Web Server – Control Outputs
Thanks for reading.
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
Dear Sara Santos,
Thanks for your kind reply on my e-mail.
You are quite right. I did not download the code.
It is solved now !!
Thanks again,
Se
I’m glad it is solved now.
Regards,
Sara 😀