I have some experience with the python GPIO library and am working on porting some of my code to C for better performance. Normally, at the end of programs, or when I break out of them with Ctrl-C, I have my code set up with a try except so that GPIO.cleanup() is called and my motors don't just keep on doing what they were doing when I broke out.

However, I see no equivalent of GPIO.cleanup() in wiringPi. Is there one, or do I not need to bother with this in wiringPi?

2 Answers 2


As far as I am aware the only Python modules which have a cleanup function are RPi.GPIO and RPIO.GPIO. The others expect you to tidy up when you terminate.

None of the C libraries that I am aware off have an equivalent. If you want to change the mode or level of a GPIO when your program terminates you have to explicitly do so yourself.

  • I tried this, but some of the pins I am using are the SPI pins. They are at the mode ALT0 by nature, and when I try a setMode(pin, ALT0) I get an error. Is there a way to set them back to the ALT0 state?
    – dpdt
    Mar 28, 2016 at 19:00
  • I don't know for wiringPi. I'm afraid you'll need to check the documentation or look at the source code. My pigpio is quite happy setting a GPIO to an ALT mode. I.e. abyz.co.uk/rpi/pigpio/cif.html#gpioSetMode (click on the mode parameter). I think the bcm2835 C library also has a mode set command.
    – joan
    Mar 28, 2016 at 19:12

You should write your own "cleanup" code. I don't know what kind of circuit / motors you are using, so I can only assume here.

If your motors are not running when the pi boots up, before your program runs, I would explicitly change the mode of all GPIO pins you touched back to inputs. This will turn off your motors.

Your Answer

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

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