Get pin number of Pin instance

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
User avatar
curt
Posts: 25
Joined: Thu Jul 29, 2021 3:52 am
Location: Big Lake, Alaska

Get pin number of Pin instance

Post by curt » Sat Jan 29, 2022 8:45 pm

Given the following code:

Code: Select all

import machine
LED1 = machine.Pin (15)
Is it possible to get the pin number from LED1?

Thanks,
Curt

Shards
Posts: 39
Joined: Fri Jun 25, 2021 5:14 pm
Location: Milton Keynes, UK

Re: Get pin number of Pin instance

Post by Shards » Wed Feb 02, 2022 3:15 pm

You don't specify which port you are using but simply typing the pin name gives some information on all I've tried, showing it's a Pin object and which pin number or physical pin it's linked to. This varies somewhat by port:

Raspberry Pi Pico

Code: Select all

>>> led = Pin(1)
>>> led
Pin(1, mode=OUT)
ESP32 & ESP8266

Code: Select all

>>> led = Pin(10)
>>> led
Pin(10)
MIMXRT1010-EVX

Code: Select all

>>> led = Pin(10)
>>> led
Pin(GPIO_AD_05)
If you actually want the Pin number you will need a bit more work, this is for the simplest case with an ESP32:

Code: Select all

>>> LED = Pin(10)
>>> LED
Pin(10)
>>> int(str(LED)[4:-1])
10
There may be a more elegant way of getting this. If there is I'm sure someone will let you know.

Post Reply