6

I intend to write a python script to change the status of GPIO pins. The code will receive arguments through sys.argv, change the state of some GPIO pins, and close afterward.

My question is: Do the state of GPIO pins (input/output) and their values (low/high) reset to the default state after the python script ends, or will their state persist until the next time the script runs and changes them again?

2
  • Upvoting and choosing an answer is the way to show your appreciation on this site. Mar 25, 2016 at 5:02
  • I have the same question, but I am using the gpiozero module. But same problem/issue.
    – RufusVS
    Dec 4, 2018 at 17:48

3 Answers 3

13

If you're using RPi.GPIO and run .cleanup() at the end of your script, it will return the channel(s) back to inputs with no pull up/down. Otherwise, they will remain in the state you left them.

See https://sourceforge.net/p/raspberry-gpio-python/wiki/BasicUsage/ for details.

2
  • This is exactly what I was looking for. Thank you!
    – Ali F
    Mar 24, 2016 at 20:10
  • This may be the same issue with gpiozero, but it perhaps does the cleanup by default.
    – RufusVS
    Dec 4, 2018 at 17:49
5

It depends on the module you are using and if you have changed the GPIO state using the module (rather than indirectly by calling a shell program to change the state).

My pigpio leaves the GPIO in the last set state.

I'm not sure about wiringPi (Python).

RPi.GPIO and RPIO.GPIO both have a cleanup function. If that is called then any GPIO you have changed with RPi or RPIO functions will be set as inputs with no (resistor) pulls.

0
2

Status (input/output direction and value) will stay as set until they are changed again - unless your script explicitly messes with them (the other answers got that covered).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.