Using array contents to set up GPIO Pins

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
BallardHill
Posts: 3
Joined: Wed Jan 05, 2022 3:04 am

Using array contents to set up GPIO Pins

Post by BallardHill » Mon Jul 25, 2022 6:48 pm

Would it just be better to make a list of statements than use code such as this:

Code: Select all

#Setup GPIO Pins from array

from machine import Pin
#from Array import Array

def arr(n=0):
    Setup = [
    [0, Pin_IN, PULL_UP], #p1
    [2, Pin_IN, PULL_UP] #p4
    ]
	
#Now use array entries to set up GPIO Pins
#for contents of array
	#set up Pin(p#, ptype, PULL_UP/DOWN)

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: Using array contents to set up GPIO Pins

Post by karfas » Mon Jul 25, 2022 8:18 pm

BallardHill wrote:
Mon Jul 25, 2022 6:48 pm
Would it just be better to make a list of statements than use code such as this:
You can setup your pins as you like/as it fits your application or use case. With your approach, you will get most likely an array or tuple of some Pin objects.

Many of us like to have the code readable, so we prefer names like in
red_led=Pin(...)
buzzer=Pin(...)
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Using array contents to set up GPIO Pins

Post by scruss » Mon Jul 25, 2022 8:55 pm

I'm mostly with karfas — it's better to use readable names.

The one time I found it useful to put pin definitions in an array was for accessing the row of 6 LEDs on the really old ESP8266-12 dev boards. They weren't arranged in any logical order, so I used this definition:

Code: Select all

leds = [
    Pin( 2, Pin.OUT, value=1),
    Pin( 0, Pin.OUT, value=1),
    Pin( 4, Pin.OUT, value=1),
    Pin( 5, Pin.OUT, value=1),
    Pin(14, Pin.OUT, value=1),
    Pin(16, Pin.OUT, value=1)]
to access the LEDs: leds[5] was the leftmost one, leds[0] the rightmost.

BallardHill
Posts: 3
Joined: Wed Jan 05, 2022 3:04 am

Re: Using array contents to set up GPIO Pins

Post by BallardHill » Mon Jul 25, 2022 11:39 pm

Thanks for your responses!

And *I* agree that it's good to know the names of Pins by their usage. I was mostly looking for the technique to iterate through an array in MicroPython, since that will be useful in upcoming code routines. I've bounced all through the MP docs and have not yet unearthed that.

Names matter, and I find it 'curious' that while the Pi Pico has 40 pins on its package, the GPIOs are named 'Pin' instead of 'GPIOn's. To me, unnecessary cognitive dissonance, and certainly confusing to newbies.

I've worked about 50-50 with hardware and software over the years, and spent a couple of wasted hours just getting a DS18B20 to work on my first Pico attempt. I'll always think of pins as hardware contact points on an electronic device, so I'll forever be fighting the unfortunate naming of Pico GPIOs ...

BallardHill
Posts: 3
Joined: Wed Jan 05, 2022 3:04 am

Re: Using array contents to set up GPIO Pins

Post by BallardHill » Mon Jul 25, 2022 11:53 pm

Scruss, in my obsessive manner I'd have defined LEDLeftMost = 2 and continued naming the indices so my code would be readable to forgetful me, or even added a different level of indirection like LED0 = 2, LED1 = 0, etc.

I've worked with other developers who found such habits of mine quite annoying ...

Post Reply