User avatar
liudr
Posts: 687
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

How does GPIOs work in raspbian?

Fri Sep 13, 2019 3:31 pm

So from a resource stand point, if I were to use a python module to manipulate the states of GPIOs, after my script ends, will the GPIO resource be in anyway reset to default, or will they stay the way I set it in my script? Same for manipulating GPIOs with say some sort of bash command etc. The reason I asked these questions is that I wonder if one script or bash command can set up some GPIOs say GPIO25 set to LOW and make the settings persist after it ends. Thanks.

Although I've used pi for years, I've always given the task of GPIO to arduinos attached to pi via usb so I should know the answer to the above but don't. :lol:
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

User avatar
DougieLawson
Posts: 38506
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How does GPIOs work in raspbian?

Fri Sep 13, 2019 3:40 pm

It depends whether you code an exception handler to reset things. Else they'll stick in the last active state.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
liudr
Posts: 687
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Re: How does GPIOs work in raspbian?

Fri Sep 13, 2019 3:57 pm

DougieLawson wrote:
Fri Sep 13, 2019 3:40 pm
It depends whether you code an exception handler to reset things. Else they'll stick in the last active state.
Exactly what I was seeking (and secretly hoping for) in a concise way! Thank you!

Some background: I was hoping to have rpi0w powered by a usb hub, not via its own microUSB ports but via header connection to an arduino and arduino's USB port. I need to make sure there is no surge current or high current consumption before arduino's 500mA USB configuration is activated by the host (atmega32u4 chip with onboard USB). This means arduino keeps pi powered off (external pullup on P-MOSFET during power up) until it gets the (set_configuration) from USB host.

All sounds good but pi has responsibilities to flash firmware to arduino via SPI. So while that happens, 32u4 will be in a reset state and having no strapping pins, it won't be able to hold the P-MOSFET low to keep pi powered. Plus, 32u4 also resets if it is told to emulate a different device by pi, via hardware UART.

This means once pi is powered up, the P-MOSEFT should be pulled low by something other than 32u4's pin. I thought about adding another MCU or I2C port extender to do this but thought that was overkill. I can use an N-MOSFET bi-directional level shifter setup for pi to hold the P-MOSFET low once rpi boots and runs a script and the pin is held low from that point on to guard against 32u4 reset and lost of LOW state on its pin.

Being a complete noob in this pi GPIO business, what is the easiest way to set GPIO state, some shell commands? I don't mind the speed as long as it's easy to do in command line. I'm using GPIO25 for 32u4 reset and all SPI pins. Which GPIO pins would you use? Something without alternative functions would be best.

Thanks.
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

User avatar
joan
Posts: 14845
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: How does GPIOs work in raspbian?

Fri Sep 13, 2019 4:03 pm

All the GPIO have multiple modes.

Have a look at wiringPi's gpio utility.

man gpio

User avatar
liudr
Posts: 687
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Re: How does GPIOs work in raspbian?

Fri Sep 13, 2019 4:14 pm

Thanks. Will investigate the gpio command. I'll just pick one that is not I2C, not second SPI, not alt. hardware-UART, etc. There should be a few left that are less feature-ful I guess :lol:
Also the inner row of pins are preferred to save time on board routing. Will get back to report how it works out.
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

fruitoftheloom
Posts: 22640
Joined: Tue Mar 25, 2014 12:40 pm
Location: Delightful Dorset

Re: How does GPIOs work in raspbian?

Fri Sep 13, 2019 4:38 pm

liudr wrote:
Fri Sep 13, 2019 4:14 pm
Thanks. Will investigate the gpio command. I'll just pick one that is not I2C, not second SPI, not alt. hardware-UART, etc. There should be a few left that are less feature-ful I guess :lol:
Also the inner row of pins are preferred to save time on board routing. Will get back to report how it works out.

Regards GPIO pinout see

https://pinout.xyz/
Rather than negativity think outside the box !

Asus ChromeBox 3 Celeron is my other computer.

User avatar
DougieLawson
Posts: 38506
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How does GPIOs work in raspbian?

Fri Sep 13, 2019 6:30 pm

So in RPi.GPIO

Code: Select all

        
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
button = 19
led = 17
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(led, GPIO.OUT)

try:
  while True:
    input_state = GPIO.input(button)

    if input_state == False:
      print('Button Pressed')
      time.sleep(0.2)
      GPIO.output(led, 1)
    else: 
      GPIO.output(led, 0)

except KeyboardInterrupt:
  print ("About to exit")

finally:
  GPIO.cleanup()
In GPIOZero

Code: Select all

from gpiozero import LED, Button
from signal import pause

led = LED(17)
button = Button(19)

button.when_pressed = led.on
button.when_released = led.off
try:
  pause()
  
except KeyboardInterrupt:
  print ("About to exit")
  
finally:
  led.close()
Both programs trap [CTRL]+[C] and exit gracefully.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
liudr
Posts: 687
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Re: How does GPIOs work in raspbian?

Fri Sep 13, 2019 8:21 pm

Awesome help! If I understand correctly, RPi.GPIO looks similar to arduino. gpiozero is more OOP and tries to hide more details. I'll try RPi.GPIO first. Just have to figure out what "GPIO25" means, BCM, or other mode.
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

User avatar
DougieLawson
Posts: 38506
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How does GPIOs work in raspbian?

Fri Sep 13, 2019 8:35 pm

Use BCM everywhere (Broadcom numbering is most sane). Use https://pinout.xyz to find the pins you're seeking.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
rpdom
Posts: 16744
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How does GPIOs work in raspbian?

Fri Sep 13, 2019 8:49 pm

DougieLawson wrote:
Fri Sep 13, 2019 8:35 pm
Use BCM everywhere (Broadcom numbering is most sane). Use https://pinout.xyz to find the pins you're seeking.
I agree with Dougie. The Pi uses BCM numbers internally, so it is a good idea to get to know them. Then if you want to do any low-level stuff with direct access you will understand it better.
Unreadable squiggle

Return to “Beginners”