Strange python bug on raspberries pi GPIO: s

I am running some testing using Python on a Raspberry Pi. The hardware is a series of SPDT relays connected to a Darlington array (ULN2803) that is connected to a GPIO raspberry Pi B.

If I set the GPIO pin high, the corresponding relay will pull. If I set it low, it will be released. I can use a loop to set all the GPIO: s values, and all the relays are pulled except the last (?). But if I run time.sleep() after the loop has set all the pins to height , all of them are automatically set automatically.

Check the code below; When it starts, it iterates through eight GPIO: s, setting one maximum at a time, and sleeps for 200 ms between them. This works, but the latter is not activated. After that, he sleeps for 1 s, which leads to the fact that all the pins are lowered. And then he starts repeating through them again, backward, turning them off. As soon as it starts back, at the first iteration, everything rises again. Why is this? This does not happen inside the loop, only between them. This is similar to the fact that sleep () is run outside the loop, everything returns to what it was before the program started.

 import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) iopins = [4,7,8,9,10,11,17,18] try: for x in iopins: GPIO.setup(x, GPIO.OUT) GPIO.output(x,0) while True: for x in iopins: #turn on. time.sleep(0.2) GPIO.output(x, GPIO.HIGH) time.sleep(1) #<-- causes all GPIOs to pull low... for y in reversed(iopins): #Turning off. GPIO.output(y, GPIO.LOW) #<--here all GPIOs pulls high upon first iteration time.sleep(0.2) finally: GPIO.cleanup() 

Another strange thing: if the IOPINS array contains only one object, it does not work.

+4
source share
1 answer

If you delete time.sleep(1) , the last relay will not work, because it will be activated and deactivated immediately after. You can change the order in the last for loop:

 import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) iopins = [4,7,8,9,10,11,17,18] try: for x in iopins: GPIO.setup(x, GPIO.OUT) GPIO.output(x,0) while True: for x in iopins: #turn on. time.sleep(0.2) GPIO.output(x, GPIO.HIGH) for y in reversed(iopins): #Turning off. time.sleep(0.2) GPIO.output(y, GPIO.LOW) finally: GPIO.cleanup() 
0
source

Source: https://habr.com/ru/post/1502588/


All Articles