Skip to content

uPesy only delivers currently in France.

Contents Menu Expand Light mode Dark mode Auto light/dark mode

Using ESP32 I/O with MicroPython

The management of the inputs/outputs of the pins (GPIO) is convenient in MicroPython thanks to the Pin object of the machine module. To interact with a “physical” pin of the ESP32, you will need to link a “virtual Pin” object with it. Therefore, each physical pin will be represented by a variable (more exactly an object) of the Pin type. You just have to use my_pin.a_function() on the “virtual” pin to modify the state of the “physical” pin.

So remember to import this submodule at the beginning of your MicroPython script:

from machine import Pin

Configure pins as input or output with MicroPython

The configuration of an input/output pin is done when creating a Pin object.

To set a pin as an output (for example, for the Pi Pico built-in LED), a Pin object is created by specifying the number of the pin and its output mode (OUT) :

pin_led = Pin(25, mode=Pin.OUT)

You can, if you wish, directly impose an output voltage when creating the object:

pin_led = Pin (25, mode = Pin.OUT, value = 1) # 3.3V on output -> the led will be on

By default, when creating a Pin object, the output voltage is 0V, value is 0.

To configure an input pin, you must change the input mode option (IN) when creating the pin.

pin_24 = Pin(24, mode=Pin.IN)

It is possible to specify the input type you want to have. You can choose between input with a pullup resistance, with a pulldown resistance or without a pull resistance. If these terms are not familiar to you, I encourage you to study the operation and usefulness of pull resistors. We use the optional argument pull to choose the pull resistance type.

pin_24 = Pin(24, mode=Pin.IN, pull=Pin.PULL_DOWN) # Input with pulldown resistor
pin_24 = Pin(24, mode=Pin.IN, pull=Pin.PULL_UP)   # Input with pullup resistor

Various ways in MicroPython to set or read a voltage

MicroPython offers several functions to set output voltage:
  • Pin.on() and Pin.off()

  • Pin.high() and Pin.low()

  • Pin.value(value)

The function names are self-explanatory:

pin_led = Pin(25, mode=Pin.OUT)

# Impose une tension de 3.3V en sortie (état logique haut)
pin_led.on()
pin_led.high()
pin_led.value(1)

# Impose une tension de 0V en sortie (état logique bas)
pin_led.off()
pin_led.low()
pin_led.value(0)

Note

Choose the functions couple that speak to you the most. For example, .on ()|.off() to turn on LEDs and high ()|.low() for a more general case. The Pin.value() function is useful when the value is stored in a Pin.value (my_variable) variable. For example :

pin_led = Pin(25, mode=Pin.OUT)
output_state=1
pin_led.value(output_state) # 3.3V en sortie
output_state=0
pin_led.value(output_state) # 0V en sortie

There is also the Pin.toggle() function that reverses the output state.

pin_led = Pin(25, mode=Pin.OUT, value=1) # 3.3V en sortie
pin_led.toggle() # 0V en sortie

To read the state of a pin, i.e., either 3.3V for a high logic state and 0V for a low logic state, we use the same Pin.value() function without specifying a value.

pin_24 = Pin(24, mode=Pin.IN, pull=Pin.PULL_UP)
pin_24.value() # Retourne 1 ou 0 en fonction de la tension mesurée

Project: Turn on the LED when a push-button is pressed

To practice the control of GPIO pins, here is a script that lights the built-in LED when a push-button is pressed. There are 2 variations of the script: one uses a digital input with a pullup resistor and another with a pulldown resistor.

Note

We generally use pullup resistors rather than pulldown for the digital inputs.

With the internal pulldown resistance of the digital input

Here is the electrical circuit to run the MicroPython script on an ESP32 board.

from machine import Pin

pin_button = Pin(14, mode=Pin.IN, pull=Pin.PULL_DOWN)
pin_led    = Pin(25, mode=Pin.OUT)

while True:
    if pin_button.value() == 1:
        pin_led.on()
    else:
        pin_led.off()

With the internal pullup resistor of the digital input

The electrical schematic is associated with the MicroPython script on an ESP32 board. You just have to connect the button to the ground instead of 3.3V.

from machine import Pin

pin_button = Pin(14, mode=Pin.IN, pull=Pin.PULL_UP)
pin_led    = Pin(25, mode=Pin.OUT)

while True:
    if not pin_button.value():
        pin_led.on()
    else:
        pin_led.off()

Note

You will notice that we can reduce pin_button.value () == 1 to pin_button.value() in the if condition.

We can reduce the code because an if condition can be written only in one line in Python:

from machine import Pin

pin_button = Pin(14, mode=Pin.IN, pull=Pin.PULL_UP)
pin_led    = Pin(25, mode=Pin.OUT)

while True:
    pin_led.on() if not pin_button.value() else pin_led.off()

We could further simplify the code by completely removing the if even if we lose clarity.

from machine import Pin

pin_button = Pin(14, mode=Pin.IN, pull=Pin.PULL_UP)
pin_led    = Pin(25, mode=Pin.OUT)

while True:
    pin_led.value(not pin_button.value())

Note

These minor optimizations can be crucial if we want to run the code as quickly as possible because, as a reminder, MicroPython is very slow compared to the C/C++ language.

We use cookies to make your visit to the site as pleasant as possible. Privacy Policy