Is there any way to handle the equivalent of Python's GPIO.cleanup() via the terminal ?
2 Answers
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.
-
"It resets any ports you have used in this program back to input mode" - in other words it DOES NOTHING. Nov 5, 2015 at 22:38
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.