Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using `gpiozero` on `raspberry pi` to control pins, but output pins are reset upon script exit, even though state is remembered between runs

I am using gpiozero to control devices on the Raspberry Pi. When I create a reference to (for example) an LED device, there is a parameter for creating the object without effecting it's current state: initial_state=None. (The default is initial_state=False, which automatically turns the value off upon reference object creation) The problem is it always seems to reset the hardware pin on script exit (though oddly enough not the internal "state"). What's worse, when I run the script again, it knows the state I left it in, and puts the physical pin back to that state!

Here's my jumpers on/off program, it now has a pausing input, during which the state stays unchanged, but when the program exits, the pins reset. (Though as I mention above, the state is "remembered")

#!/usr/bin/env python
from __future__ import print_function
import sys
import time
from gpiozero import LED
jump1=LED(17,initial_value=None)
jump2=LED(27,initial_value=None)


if len(sys.argv)>1:
    print ("Jumper were: (%s,%s)"%(str(jump1.is_active),str(jump2.is_active)))
    if sys.argv[1].lower() == 'on':
        jump1.on()
        jump2.on()
        print ('turned both on')
    elif sys.argv[1].lower() == 'off':
        jump1.off()
        jump2.off()
        print ('turned both off')

print ("Jumper Currently: (%s,%s)"%(str(jump1.is_active),str(jump2.is_active)))

raw_input("Press enter to exit.")

Does anyone know a way to tell gpiozero to leave the hardware alone after exit? This question details a similar problem, though a different module.

(Edit: it turns out the gpiozero module changes the pin direction to input but doesn't change the output latch, which is how it gets the old state back when the pin direction is changed back to output.)

like image 379
RufusVS Avatar asked Dec 04 '18 17:12

RufusVS


People also ask

Which function is used in Raspberry Pi to reset the used pins back to its normal state?

Correct use of 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.

What is the difference between GPIO and GPIOZero?

GPIO are used to turn on the LED, but in terms of simplicity, the GPIOZero is relatively easy and requires minimum lines code to turn on the LED. It is because the GPIOZero has a module which is already imported at the start of a code and it will always use the Broadcom GPIO numbering system to identify the GPIO pins.

What does GPIOZero do?

¶ gpiozero provides a range of different approaches to reading input devices. Sometimes you want to ask if a button's pressed, sometimes you want to do something until it's pressed, and sometimes you want something to happen when it's been pressed, regardless of what else is going on.

What precautions should be taken with Raspberry Pi GPIO pins?

Shut down and power off before adding hardware: Shut down and power off your Raspberry Pi before adding hardware like different SD cards, HATs, camera, or add-on boards. It is generally safe for adults to connect things like LEDs to the GPIO pins with the power on. A mistake won't hurt you, but could damage your Pi.


1 Answers

I rewrote using RPi.GPIO module instead of gpiozero. It feels different, but it was easier than researching a way to do exit without cleanup using gpiozero.

Here is the "equivalent" program without pin cleanup.

#!/usr/bin/env python
from __future__ import print_function
import sys
import time
import RPi.GPIO as GPIO

# these pin numbers map have to change
# try the 'pinout' command from the bash prompt

pina = 17
pinb = 27


# set pins up:

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(pina, GPIO.OUT)
GPIO.setup(pinb, GPIO.OUT)


if len(sys.argv)>1:
    print ("Jumpers were: (%s,%s)"%  (str(GPIO.input(pina)),str(GPIO.input(pinb))))
    if sys.argv[1].lower() == 'on':
        GPIO.output(pina, GPIO.HIGH)
        GPIO.output(pinb, GPIO.HIGH)
        print ('turned both on')
    elif sys.argv[1].lower() == 'off':
        GPIO.output(pina, GPIO.LOW)
        GPIO.output(pinb, GPIO.LOW)
        print ('turned both off')

print ("Jumpers now: (%s,%s)"%    (str(GPIO.input(pina)),str(GPIO.input(pinb))))

#raw_input("Press enter to exit.")  # optional pause for testing

# Note:  I/O pins will remain at their last state.
like image 71
RufusVS Avatar answered Oct 29 '22 00:10

RufusVS