Page 1 of 1

GPIO.setup(pin,GPIO.OUT) turn pin on

Posted: Fri Mar 06, 2020 5:41 am
by syl.poi
1. The line GPIO.setup(38, GPIO.OUT) was lighting the led.
2. The GPIO.cleanup() was not solving the problem. The keyboard interrup was not solving the problem by itself.
3. At the first iteration of the script everything was working correctly, but after the led is lighted one time at the second iteration, the GPIO.setup(38, GPIO.OUT) was lighting the led.
4. The problem was not specific to a GPIO pin but it was the same with all.
5. The problem appeared suddenly with no identified reason.

The only solutions that was working was:
1. Rebooting the pi. But again it works only for the first iteration of the script.
2. Adding the following two lines in the keaboard interrupt solved the problem definitely. Those two lines reset each pin to 0 or GPIO.LOW.

GPIO.setup(38, GPIO.OUT, initial=0) # Solved the problem
GPIO.setup(22, GPIO.OUT, initial=0) # Solved the problem

Hardware and software are:
- Raspberry pi 4.
- Raspbian version: Raspbian GNU/Linux 10 (buster).
- All update and upgrade completed.


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Code: Select all

# This code activate a pin at a specific time of the day and deactivate the pin at a different time of the day
from datetime import datetime
import time as t
import RPi.GPIO as GPIO

t2 = t.time() 

def setup():
    # GPIO.setwarnings(False)  Keyboard interrup is a better way to manage these errors
    GPIO.setmode(GPIO.BOARD) 
    GPIO.setup(38, GPIO.OUT)  
    GPIO.setup(22, GPIO.OUT) 

setup()

try:
    while True:
        elapsed_time = t.time() - t2
        if elapsed_time > 10:
            t2 = t.time()
            if str(datetime.now())[11:16] == str("23:00"): # Pins activation 23:00
                GPIO.output(38,GPIO.HIGH)  
                GPIO.output(22,GPIO.HIGH) 
                print("ledpin on")
            if str(datetime.now())[11:16] == "06:00": # Pins deactivation at 06:00
                GPIO.output(38,GPIO.LOW)  
                GPIO.output(22,GPIO.LOW) 
                print("ledpin off")
except KeyboardInterrupt:
    GPIO.setup(38, GPIO.OUT, initial=0)  #  Solved the problem
    GPIO.setup(22, GPIO.OUT, initial=0)  #  Solved the problem
    GPIO.cleanup()
    print("KeyboardInterrupt has been caught.")
    

Re: GPIO.setup(pin,GPIO.OUT) turn pin on

Posted: Fri Mar 06, 2020 8:11 am
by B.Goode
syl.poi wrote:
Fri Mar 06, 2020 5:41 am
I got the same problem.

In which case you must still be running the Jessie version of the Raspbian Operating System. This has now been twice superceded. Can you recreate the same problem if you run it on the current supported Raspbian Buster Operating System?

If so, asking the Moderator to move your question to its own new topic might be a better way to proceed. A lot has changed since the original question was asked, including the availablity of updated hardware. Mentioning what model of RPi board you have might be sensible.

Re: GPIO.setup(pin,GPIO.OUT) turn pin on

Posted: Sat Mar 07, 2020 6:08 pm
by syl.poi
I did an update for my post, my pi is recent, model 4 with Raspbian Buster.
All update and upgrade done.
It appear to be a come back of an old problem.

Re: GPIO.setup(pin,GPIO.OUT) turn pin on

Posted: Sat Mar 07, 2020 6:35 pm
by rpiMike
What is the problem that you are trying to solve?

Re: GPIO.setup(pin,GPIO.OUT) turn pin on

Posted: Sat Mar 07, 2020 10:53 pm
by syl.poi
Hi,

The problem is the "GPIO.setup(pin,GPIO.OUT)" that turn the pin on. It should be done only with "GPIO.HIGH".
How i can solve that?
Resetting the pins to 0 solve the problem partially, but it's only a workaround.

Thanks.

Re: GPIO.setup(pin,GPIO.OUT) turn pin on

Posted: Sun Mar 08, 2020 12:37 am
by DirkS
Use GPIO.setup(pin, GPIO.OUT, initial=GPIO.LOW)?
https://sourceforge.net/p/raspberry-gpi ... asicUsage/

Re: GPIO.setup(pin,GPIO.OUT) turn pin on

Posted: Sun Mar 08, 2020 10:44 am
by pcmanbob
Well it does not do it on my pi4B running a fully updated version of buster lite.

running this code

Code: Select all

import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD) 
GPIO.setup(38, GPIO.OUT)  
GPIO.setup(22, GPIO.OUT) 
time.sleep(30)
run over and over the LED's never come on, which is what I would expect , if your LEDs are wired between the gpio output pin and a ground pin.

So are you running a fresh copy of raspbian buster or raspbian stretch updated to raspbian buster ?

Re: GPIO.setup(pin,GPIO.OUT) turn pin on

Posted: Tue Mar 10, 2020 8:41 pm
by syl.poi
I run a fresh copy of Raspbian Buster that was preinstalled on the pi.
The problem started after 2 months; at the beginning everything was working perfectly.
There is the possibility that a corruption occurred at a certain moment.

Re: GPIO.setup(pin,GPIO.OUT) turn pin on

Posted: Tue Mar 10, 2020 8:53 pm
by pcmanbob
syl.poi wrote:
Tue Mar 10, 2020 8:41 pm
I run a fresh copy of Raspbian Buster that was preinstalled on the pi.
The problem started after 2 months; at the beginning everything was working perfectly.
There is the possibility that a corruption occurred at a certain moment.
As it was working correctly for 2 months it would suggest you have some corruption on your SD card which has affected the RPi.GPIO files.

I would suggest trying a fresh install of raspbian buster on another SD card , you will probably find it works correctly then.

If you have nothing in you existing SD card that you want to keep you could just do the fresh install on that.

You could also try doing a reinstall of RPi.GPIO on you existing SD card, but there is no guarantee it would fix the problem.