circuitpython import microcontroller pins

The problem of circuitpython import microcontroller pins

In this blog, we will learn how we can import circuitpython import microcontroller pins module in our python program. CircuitPython is a microcontroller-optimized programming language that enables you to connect to a wide range of sensors, inputs, and other hardware peripherals. The process of setting up a circuit and using CircuitPython to, for instance, receive data from a sensor or detect a button press are covered in a tonne of guides. The majority of CircuitPython code involves hardware setup, which calls for a number of modules, including board and digitalio.

Solution

To import the above module the code for the following is:

import board

dir(board)

The output of the following code is:

 

 

On microcontroller boards that are compatible with CircuitPython, many pins have numerous names; yet, often, the real board only has one name labeled. So how can you learn what the other pin names that are accessible are? Simple, just use the script below! The list of names for each pin is printed on a separate line to the serial console. To use the microcontroller pins in python one needs to write the below code:

 

import microcontroller
import board

board_pins = []
for pin in dir(microcontroller.pin):
    if isinstance(getattr(microcontroller.pin, pin), microcontroller.Pin):
        pins = []
        for alias in dir(board):
            if getattr(board, alias) is getattr(microcontroller.pin, pin):
                pins.append("board.{}".format(alias))
        if len(pins) > 0:
            board_pins.append(" ".join(pins))
for pins in sorted(board_pins):
    print(pins)

 

The output of the above code is:

 

 

 

Also Read: What is IndexError and how to solve it?

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *