3

Is there any way to handle the equivalent of Python's GPIO.cleanup() via the terminal ?

1
  • Make a small python script and call it from terminal. That's the easiest way I suppose. Nov 5, 2015 at 5:55

2 Answers 2

1
A Small Bash script/python script will do the job. Run it on raspberry Pi.

explanation

RPi.GPIO provides a built-in function GPIO.cleanup() to clean up all the ports you’ve used. But be very clear what this does. It only affects any ports you have set in the current program. It resets any ports you have used in this program back to input mode. This prevents damage from, say, a situation where you have a port set HIGH as an output and you accidentally connect it to GND (LOW), which would short-circuit the port and possibly fry it. Inputs can handle either 0V (LOW) or 3.3V (HIGH), so it’s safer to leave ports as inputs.

CODE :

import RPi.GPIO as GPIO  

# the rest of your code would go here 

# when your code ends, the last line before the program exits would be...  
GPIO.cleanup() 

# remember, a program doesn't necessarily exit at the last line! 

For more in-depth check this link.

1
  • "It resets any ports you have used in this program back to input mode" - in other words it DOES NOTHING.
    – Milliways
    Nov 5, 2015 at 22:38
2

The following code will set all the exported GPIO to be unexported inputs.

sudo bash -c "for ((i=0; i<32; i++)); do echo \$i; echo in >/sys/class/gpio/gpio\$i/direction; echo \$i >/sys/class/gpio/unexport; done"

Note that the \ before the $i are important to prevent premature shell expansion.

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.