[EN] machine.Pin

This article is to learn how to use the Pin class which is a subclass in the machine class of Micropython for use with ESP8266 or ESP32 microcontrollers.


Microcontroller’s pin or GPIO is responsible for inputting data or outputting data to connect devices or circuits outside the chip. Accessing these pins with Micropython must be done via the machine.Pin class with these steps.

  1. Import machine class
  2. Assign the pin
  3. Inputting or outputting the signal

Invocation of the Pin class can be done in two ways: through the machine class with the import machine and then using machine.Pin as the pin access point or specify to retrieve Pin from machine import Pin.

The command for defining pin functions has the following form of usage: where the work mode is set to machine.Pin.IN for data import and machine.Pin.OUT for data export. If you want to use a Pullup circuit or a connected circuit that uses a Pullup, specify machine.Pin.PULLUP. It can be assembled to create a pin object. In addition, if you want to initialize the pin from the start, you can set the mode to machine.Pin.OUT and let the value be either 0 or 1.

pin = machine.Pin( gpio, mode )
pin = machine.Pin( gpio, mode, machine.Pin.PULLUP )
pin = machine.Pin( gpio, machine.Pin.OUT, value=default )

The command to read a value and assign a value to a pin has the following syntax: on() is the same as value(1) and off() is the same as value( 0 ).

var = pin.value()
pin.value( value )

In addition to being used as part of the import and export of data, programmers can use the input pin as an interrupt actuator pin as well, as mentioned in the previous article.

GPIO’s properties

The functions of GPIO of microcontroller esp8266 and esp32 that must be known are as follows.


  1. The pins that esp8266 provides for the designer or developer to use are 0,1,2,3,4,5, 12, 13, 14, 15 and 16.
  2. Pins 1 and 3 are Tx and Rx when used in serial communication (UART) on REPL systems.
  3. Pin 16 is used for wake up in power-saving mode by connecting to Rst pin.
  4. Pin A0 is used to operate a 10-bit ADC which supports 0-1VDC voltage.
  5. SPI communication (SPI0) works with communication speed up to 80MHz using the following pins.
    1. GPIO12 for performing MISO functions or importing data.
    2. GPIO13 for performing MOSI functions or exporting data.
    3. GPIO14 for performing SCK functions or generate a clock signal.
  6. I2C bus communication can be used on all pins because it works like Software I2C.

The esp8266 serial communication has the following pins:



  1. esp32 has pins to be used as follows: Each pin can drive a maximum load of 40mA.
    1. Pins 0-19
    2. Pins 21-23
    3. Pins 25-27
    4. Pins 32-39 is a pin that supports ADC. It has a value range of 0 – 4095, but the voltage must be in the range of 0-1VDC. Normally, the board has a 0-3v3 voltage attenuation circuit to 0-1VDC, but it should be carefully checked before use.
  2. Pins 1 and 3 are REPL UART Tx and Rx, so if there is a serial communication connection, do not use these 2 pins to connect to an external circuit.
  3. Pin 6, 7, 8, 11, 16 and 17 are used to connect to the flash memory circuit of the microcontroller, so these pins should not be used with external circuits if the selected chip has an external flash ROM, especially the chip WROOM32, uses the pin to connect to the flash as follows.
    1. GPIO6 –> SCK/CLK
    2. GPIO7 –> SDO/SD0
    3. GPIO8 –> SDI/SD1
    4. GPIO9 –> SHD/SD2
    5. GPIO10 –> SWP/SD3
    6. GPIO11 –> CSC/CMD
  4. Pins for Capacitive Touch Sensors for use as wake up pins are:
    1. T0 –> GPIO4
    2. T1 –> GPIO0
    3. T2 –> GPIO2
    4. T3 –> GPIO15
    5. T4 –> GPIO13
    6. T5 –> GPIO12
    7. T6 –> GPIO14
    8. T7 –> GPIO17
    9. T8 –> GPIO33
    10. T9 –> GPIO32
  5. Pin 34-39 is a pin for acting as an input pin only, so it can’t work like machine.Pin.OUT.
  6. The pins for functioning DAC0 and DAC1 are GPIO25 and GPIO26.
  7. For some pins that machine.Pin.PULLUP and want to use it during power-saving mode or deepsleep set it to machine.PULLUP_HOLD
  8. These pins are specifically used for booting: GPIO 0, GPIO 2, and GPIO 4. The following 3 pins must have a mandatory state to boot.
    1. GPIO 1 must be HIGH
    2. GPIO 3 must be HIGH
    3. GPIO 5 must be HIGH during boot.
    4. GPIO 6 to GPIO11 connected with flash memory, should not be used
    5. GPIO 12 must be LOW during boot.
    6. GPIO 14 must be HIGH
    7. GPIO 15 must be HIGH during boot
  9. A pin that can perform ADC functions is a 12-bit ADC circuit.
    1. ADC1_CH0 –> GPIO 36
    2. ADC1_CH1 –> GPIO 37
    3. ADC1_CH2 –> GPIO 38
    4. ADC1_CH3 –> GPIO 39
    5. ADC1_CH4 –> GPIO 32
    6. ADC1_CH5 –> GPIO 33
    7. ADC1_CH6 –> GPIO 34
    8. ADC1_CH7 –> GPIO 35
    9. ADC2_CH0 –> GPIO 4
    10. ADC2_CH1 –> GPIO 0
    11. ADC2_CH2 –> GPIO 2
    12. ADC2_CH3 –> GPIO 15
    13. ADC2_CH4 –> GPIO 13
    14. ADC2_CH5 –> GPIO 12
    15. ADC2_CH6 –> GPIO 14
    16. ADC2_CH7 –> GPIO 27
    17. ADC2_CH8 –> GPIO 25
    18. ADC2_CH9 –> GPIO 26

For the pins connected to UART0, UART1 and UART2 are shown in the following table.

  1. UART0 used in the process of chip programming and during system reset/boot
  2. UART1 , on some boards it is used with SPI Flash.
  3. UART2 is a port that developers can continue to use.
Tx/Send data11017
Rx/Receive data3916

The pins for connecting to SPI communication in the hardware mode of the ESP32 microcontroller are as follows.

PinHSPI (id=1)VSPI (id=2)
SCK/generate clock signal1418
MOSI/export data1323
MISO/import data1219

The pins for connecting to I2C communication in hardware mode are that the GPIO25 and GPIO26, they can be DAC, so if you choose to use a DAC, be careful when using the 2nd I2C  (id=1).

PinI2C (id=0)I2C (id=1)
SCL/Generate clock signal1825
SDA/Send or receive data1926


From this article, you will find that the pins of esp8266 and esp32 are enough for use. But if you look closely, you will find that many pins have limited functions. The remaining pins for connecting to the designed circuit are so few that sometimes we have chosen to use the I2C bus connection for connecting to the Arduino Uno/Mega2560 or STM32 board to expand the port. Finally, have fun with programming.

If you want to talk with us, feel free to leave comments below!!


  1. Micropython, “Quick reference for ESP8266”
  2. Micropython, “Quick reference for ESP32”
  3. Micropython, “class Pin – control I/O pins”

(C) 2020-2021, By Jarut Busarathid and Danai Jedsadathitikul
Updated 2021-11-02