syl.poi
Posts: 8
Joined: Sat Jan 19, 2019 3:07 am

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

Fri Mar 06, 2020 5:41 am

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.")
    
Last edited by syl.poi on Sat Mar 07, 2020 6:16 pm, edited 2 times in total.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

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

Fri Mar 06, 2020 8:11 am

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.

syl.poi
Posts: 8
Joined: Sat Jan 19, 2019 3:07 am

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

Sat Mar 07, 2020 6:08 pm

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.

User avatar
rpiMike
Posts: 1386
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

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

Sat Mar 07, 2020 6:35 pm

What is the problem that you are trying to solve?

syl.poi
Posts: 8
Joined: Sat Jan 19, 2019 3:07 am

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

Sat Mar 07, 2020 10:53 pm

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.

DirkS
Posts: 10363
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

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

Sun Mar 08, 2020 12:37 am

Use GPIO.setup(pin, GPIO.OUT, initial=GPIO.LOW)?
https://sourceforge.net/p/raspberry-gpi ... asicUsage/

pcmanbob
Posts: 9465
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

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

Sun Mar 08, 2020 10:44 am

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 ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

syl.poi
Posts: 8
Joined: Sat Jan 19, 2019 3:07 am

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

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.

pcmanbob
Posts: 9465
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

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

Tue Mar 10, 2020 8:53 pm

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.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Troubleshooting”