User avatar
infernowheels
Posts: 8
Joined: Sat Mar 11, 2017 12:03 pm
Location: Philippines

Open or Closed Circuit Detection

Tue Mar 14, 2017 3:20 pm

Code: Select all

import RPi.GPIO as GPIO
import time
from time import sleep
GPIO.setmode(GPIO.BCM)

GPIO.setup(2, GPIO.IN)

while True:
    GPIO.setup(2,GPIO.IN)
    if GPIO.input(2) == False:
        print('Circuit Closed.')
        sleep(1) 
    elif GPIO.input(2) == True:
        print('Circuit Open.')
        sleep(1)
    GPIO.cleanup(2)
The code runs, just that it doesn't really detect our input properly. (so I'm assuming something's wrong) It just keeps on printing "Circuit Open" even when we have our input on HIGH.

Refer to this thread for more details:
viewtopic.php?f=37&t=177029

Any help would be appreciated, Thanks!

scotty101
Posts: 3672
Joined: Fri Jun 08, 2012 6:03 pm

Re: Open or Closed Circuit Detection

Tue Mar 14, 2017 3:30 pm

Have you put a multi-meter/scope on the input to the pi to test whether the input is actually changing from 3.3v to 0V?

Have you tried your code with a simple button connected?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
infernowheels
Posts: 8
Joined: Sat Mar 11, 2017 12:03 pm
Location: Philippines

Re: Open or Closed Circuit Detection

Tue Mar 14, 2017 3:52 pm

scotty101 wrote:Have you put a multi-meter/scope on the input to the pi to test whether the input is actually changing from 3.3v to 0V?

Have you tried your code with a simple button connected?
No, but I actually got "Circuit Closed." if I only used "else" instead of "elif".
(And we don't get "Circuit Open." if I unplugged the input from its power source)

Furthermore, we can confirm that we are receiving an input on GPIO2 if we used this code:

Code: Select all

from gpiozero import Button
button = Button(2)

button.wait_for_press()
print('You pushed me')
It's just a matter of getting the conditions to recognize that the input is either receiving power or not.

scotty101
Posts: 3672
Joined: Fri Jun 08, 2012 6:03 pm

Re: Open or Closed Circuit Detection

Wed Mar 15, 2017 9:04 am

Try moving the 'read' of the GPIO pin out of the if-elif-else loop. Also given that a GPIO pin can only be true or false, you don't need an elif. If it isn't true, then it must be false.
I've also moved the time.sleep statement outside of the if-else since it happens regardless of the input state.

Oh and GPIO.cleanup should only ever be run once and at the exit point of code. You currently have it inside the while loop.

Code: Select all

import RPi.GPIO as GPIO
import time
from time import sleep
GPIO.setmode(GPIO.BCM)

GPIO.setup(2, GPIO.IN)

while True:
    GPIO.setup(2,GPIO.IN)
    state = GPIO.input(2)
    if state == False:
        print('Circuit Closed.')
    else:
        print('Circuit Open.')
    sleep(1)

GPIO.cleanup(2)
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
infernowheels
Posts: 8
Joined: Sat Mar 11, 2017 12:03 pm
Location: Philippines

Re: Open or Closed Circuit Detection

Sat Mar 25, 2017 8:32 am

scotty101 wrote:Try moving the 'read' of the GPIO pin out of the if-elif-else loop. Also given that a GPIO pin can only be true or false, you don't need an elif. If it isn't true, then it must be false.
I've also moved the time.sleep statement outside of the if-else since it happens regardless of the input state.

Oh and GPIO.cleanup should only ever be run once and at the exit point of code. You currently have it inside the while loop.

Code: Select all

import RPi.GPIO as GPIO
import time
from time import sleep
GPIO.setmode(GPIO.BCM)

GPIO.setup(2, GPIO.IN)

while True:
    GPIO.setup(2,GPIO.IN)
    state = GPIO.input(2)
    if state == False:
        print('Circuit Closed.')
    else:
        print('Circuit Open.')
    sleep(1)

GPIO.cleanup(2)
I realized that I had to use GPIO.HIGH and GPIO.LOW to make the code work.

However, is there a way to do this same function with interrupts?

Code: Select all

import RPi.GPIO as GPIO
import time
from time import sleep

LINE1 = 8
GPIO.setmode(GPIO.BCM)
GPIO.setup(LINE1, GPIO.IN)

def inputChng(channel):
    print('LINE1 pin status changed to ', GPIO.input(LINE1))

GPIO.add_event_detect(LINE1, GPIO.RISING, callback=inputChng, bouncetime=3)
time.sleep(1)

while True:
    if GPIO.input(LINE1) > 0.5:
        print('Input=',GPIO.input(LINE1), 'LINE1 OFF')
    else:
        print('Input=',GPIO.input(LINE1), 'LINE1 ON')
    sleep(0.5)

GPIO.remove_event_detect(LINE1)
GPIO.cleanup()

It detects OFF correctly but when the circuit is closed, the inputChng erratically prints that the input is changing from 1 to 0 and vice versa without any other user intervention.

yaheath
Posts: 15
Joined: Tue Mar 28, 2017 9:58 pm

Re: Open or Closed Circuit Detection

Thu May 11, 2017 10:24 pm

Your button connects between the GPIO pin and ground, right? If so you need to set a pull-up on the GPIO so that it will consistently read high when the button isn't pressed (because otherwise it is open and floating, and at the mercy of any surrounding EM noise).

Code: Select all

GPIO.setup(LINE1, GPIO.IN, pull_up_down=GPIO.PUD_UP)

Idahowalker
Posts: 445
Joined: Wed Jan 03, 2018 5:43 pm

Re: Open or Closed Circuit Detection

Wed Jan 31, 2018 1:42 am

infernowheels wrote:
Sat Mar 25, 2017 8:32 am
scotty101 wrote: However, is there a way to do this same function with interrupts?
Yes, I am posting a post I made in another thread:
There is another way of doing the thing:

Instead of running a loop, why not use an event trigger?

Code: Select all

def fDoTheThing():

    try:
        fSetup()

        GPIO.add_event_detect(iPinIn2, GPIO.RISING, callback=fCallBack, bouncetime = 20)

        #wait for stop signal
        GPIO.wait_for_edge(iPinIn1, GPIO.FALLING)
                
        GPIO.cleanup() #clean up on normal exit
        serialPort.close()
    except KeyboardInterrupt: 
        GPIO.cleanup() #clean up on kbd exit
        serialPort.close()
    return;

fDoTheThing()
Instead of polling allow the events of each input trigger in your code to do work.

Here the GPIO pin is assigned a call back function to do the work: GPIO.add_event_detect(iPinIn2, GPIO.RISING, callback=fCallBack, bouncetime = 20)

The call back method is passed the GPIO pin so an if statement can be used, in the call back, to distinguish which GPIO is the event trigger.

At this point the code is stopped: GPIO.wait_for_edge(iPinIn1, GPIO.FALLING) until a button, wired to a GPIO pin is pressed. Until the 'stop' button is pressed the program does nothing but wait to respond to events.

This method does not do polling and sleep which could mean, for a 5 minute sleep a long time till a response to a sensor handled.
[\quote]

Hope that helps point you into the direction of you interest.
Without knowing why you are deleting my postings, I will not know how...

Return to “General discussion”