1

I am not so fluent in programming for the Rpi, which I only do in my very few spare time, so I would like some confirmation.

If I put GPIO.cleanup() this cleansups and puts to input all used GPIOs , right?

I ask this very simple question because in the docs of a sensor (ultrasonic) I have there is this code for us to use:

def reading(sensor):
    import time
    import RPI.GPIO as GPIO
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BOARD)
    TRIG=11
    ECHO= 13
    if sensor == 0:
        GPIO.setup(TRIG,GPIO.OUT)
        GPIO.setup(ECHO,GPIO.IN)
        time.sleep(0.3)
        GPIO.output(TRIG,True)
        time.sleep(0.00001)
        GPIO.output(TRIG,False)
        while GPIO.input(ECHO) ==0:
            signaloff=time.time()
        while GPIO.input(ECHO) ==1:
            signalon=time.time()
        timepassed=signalon-signaloff
        distance=timepassed*170000
        return distance
        GPIO.cleanup()
    else:
        print"Incorrect usonic() function variable"

print reading(0)

First, I find the implementation of this function not that good. With the imports and the setmode inside the function. But what it bothers me most is that the function includes cleanup

I am planning to use other GPIOs for other sensors as well, But does using the above function once means that it is going to interfere with my own other sensors? I guess it will and that the implementation of this function is wrong.

Any thoughts on this?

2 Answers 2

4

Yes, the cleanup() function sets to be inputs all the GPIO used by a RPi.GPIO script. It doesn't effect other RPi.GPIO scripts running at the same time.

That particular script seems to be designed as a one-off which invokes reading() once and then exits. In that context it may not be as poor as it first looks.

Opinion.

Even so I think it poor practice. I would move the initialisation (import time, import RPI.GPIO, setwarnings, setmode, setup) and termintation (cleanup) functions outside the reading() function as generally a much more sensible, usable, and changeable design.

End of opinion.

As long as RPi.GPIO scripts do not use the same resource as another running script multiple scripts may be running at a time.

As an example the following script uses GPIO4 (I prefer BCM numbering). Copy the script and change the USE=4 line to different values for each script. If you then run them in parallel (use & or separate terminal windows per script) you can confirm each works quite happily with the other scripts.

#!/usr/bin/env python

import time
import RPi.GPIO as GPIO

USE=4

GPIO.setmode(GPIO.BCM)
GPIO.setup(USE, GPIO.OUT)

end_time = time.time() + 60

while end_time > time.time():
   print("using {}".format(USE))
   GPIO.output(USE, False)
   time.sleep(0.5)
   GPIO.output(USE, True)
   time.sleep(0.5)

GPIO.cleanup()
1
  • Thank you for the reply. Excuse me to deviate a bit but you said something very interesting: "other scripts running at the same time" Can this be possible? How? This seem very interesting for what I am trying to do. Sep 17, 2017 at 8:18
0

GPIO.cleanup() is cleaning up all the ports you’ve used.

It resets the ports you have used in your program back to input mode.

5
  • So it will obvioulsy interfere not only with the ports in that function but also with any other ports I would use, right? The function is bad design I guess Sep 17, 2017 at 6:50
  • You use some ports and then clean up the one you have used, that's a bad design?
    – MatsK
    Sep 17, 2017 at 6:59
  • If I clean ALL ports, not only the ones involved in that function, it obviously is. You see I am going to use reading multiple times, as well as many other functions that uses GPIO. If I clean ALL ports everytime I read, I will have to set all other ports again every time I use them in totally unrelated functions. That is bad design I suppose. Sep 17, 2017 at 7:54
  • or is it Rapsberry pi style of programming to set ports input or output everytime I want to use them perhaps??? ( In Pic programming you set this only when necessary) Sep 17, 2017 at 7:56
  • See answer from @joan that describe the different use cases quite well.
    – MatsK
    Sep 17, 2017 at 8:13

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.