Page 1 of 1

Using array contents to set up GPIO Pins

Posted: Mon Jul 25, 2022 6:48 pm
by BallardHill
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)

Re: Using array contents to set up GPIO Pins

Posted: Mon Jul 25, 2022 8:18 pm
by karfas
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(...)

Re: Using array contents to set up GPIO Pins

Posted: Mon Jul 25, 2022 8:55 pm
by scruss
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.

Re: Using array contents to set up GPIO Pins

Posted: Mon Jul 25, 2022 11:39 pm
by BallardHill
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 ...

Re: Using array contents to set up GPIO Pins

Posted: Mon Jul 25, 2022 11:53 pm
by BallardHill
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 ...