Mapping NodeMCU Dev Board Pins to MicroPython Pins

The NodeMCU development board is a popular choice among hobbyists and developers for Internet of Things (IoT) projects due to its low cost, Wi - Fi connectivity, and ease of use. MicroPython, on the other hand, is a lean and efficient implementation of the Python 3 programming language that runs on microcontrollers, allowing developers to use Python for embedded systems. When working with the NodeMCU board using MicroPython, one of the crucial aspects is understanding how to map the physical pins on the NodeMCU board to the pin identifiers used in MicroPython. This blog post will provide a comprehensive guide on this mapping, including fundamental concepts, usage methods, common practices, and best practices.

Table of Contents#

  1. Fundamental Concepts
  2. Mapping NodeMCU Pins to MicroPython Pins
  3. Usage Methods
  4. Common Practices
  5. Best Practices
  6. Conclusion
  7. References

Fundamental Concepts#

NodeMCU Dev Board Pins#

The NodeMCU board is based on the ESP8266 microcontroller. It has a set of GPIO (General - Purpose Input/Output) pins that can be used for various purposes such as connecting sensors, actuators, and other external devices. These pins are numbered on the board, and their functions can be configured according to the requirements of the project.

MicroPython Pins#

MicroPython uses a different naming convention for the pins compared to the physical pin numbers on the NodeMCU board. MicroPython abstracts the hardware details and provides a more convenient way to access the pins. Each pin is identified by a specific name, which is used in the MicroPython code to perform operations such as reading input or writing output.

Mapping NodeMCU Pins to MicroPython Pins#

The following table shows the mapping between the NodeMCU board pin numbers and the corresponding MicroPython pin identifiers:

NodeMCU PinMicroPython Pin
D0GPIO16
D1GPIO5
D2GPIO4
D3GPIO0
D4GPIO2
D5GPIO14
D6GPIO12
D7GPIO13
D8GPIO15
D9GPIO3
D10GPIO1

Usage Methods#

Reading Input from a Pin#

The following code example demonstrates how to read the input from a pin connected to a button. Assume the button is connected to the D3 pin on the NodeMCU board.

from machine import Pin
import time
 
# Map the NodeMCU D3 pin to MicroPython GPIO0
button_pin = Pin(0, Pin.IN, Pin.PULL_UP)
 
while True:
    button_state = button_pin.value()
    if button_state == 0:
        print("Button is pressed")
    else:
        print("Button is released")
    time.sleep(0.1)

Writing Output to a Pin#

The following code example shows how to turn an LED on and off. Assume the LED is connected to the D4 pin on the NodeMCU board.

from machine import Pin
import time
 
# Map the NodeMCU D4 pin to MicroPython GPIO2
led_pin = Pin(2, Pin.OUT)
 
while True:
    led_pin.on()
    time.sleep(1)
    led_pin.off()
    time.sleep(1)

Common Practices#

Pull - up and Pull - down Resistors#

When using input pins, it is common to use pull - up or pull - down resistors to ensure a stable input signal. In MicroPython, you can configure the pull - up or pull - down resistors directly in the Pin constructor, as shown in the button example above (Pin.PULL_UP).

Debouncing#

When reading input from buttons, it is important to implement debouncing to avoid false readings caused by mechanical vibrations. You can use a simple time - based debouncing technique, as shown in the following code:

from machine import Pin
import time
 
button_pin = Pin(0, Pin.IN, Pin.PULL_UP)
last_state = button_pin.value()
last_time = time.ticks_ms()
debounce_time = 200
 
while True:
    current_state = button_pin.value()
    current_time = time.ticks_ms()
    if current_state != last_state:
        if current_time - last_time > debounce_time:
            if current_state == 0:
                print("Button is pressed")
            else:
                print("Button is released")
            last_state = current_state
            last_time = current_time
 
 

Best Practices#

Use Descriptive Variable Names#

When mapping the pins, use descriptive variable names to make the code more readable. For example, instead of using Pin(2), use led_pin = Pin(2, Pin.OUT) if the pin is connected to an LED.

Error Handling#

When working with pins, it is important to handle potential errors. For example, if there is a hardware issue with a pin, the code should be able to gracefully handle the error and provide meaningful error messages.

from machine import Pin
import time
 
try:
    led_pin = Pin(2, Pin.OUT)
    while True:
        led_pin.on()
        time.sleep(1)
        led_pin.off()
        time.sleep(1)
except Exception as e:
    print(f"An error occurred: {e}")

Conclusion#

Understanding the mapping between NodeMCU dev board pins and MicroPython pins is essential for successfully developing projects using the NodeMCU board with MicroPython. By following the concepts, usage methods, common practices, and best practices outlined in this blog post, developers can efficiently use the pins to connect external devices, read input, and write output.

References#