Open or Closed Circuit Detection


6 posts
by infernowheels » 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:
https://www.raspberrypi.org/forums/view ... 7&t=177029

Any help would be appreciated, Thanks!
User avatar
Posts: 8
Joined: Sat Mar 11, 2017 12:03 pm
Location: Philippines
by scotty101 » 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
Posts: 2082
Joined: Fri Jun 08, 2012 6:03 pm
by infernowheels » 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.
User avatar
Posts: 8
Joined: Sat Mar 11, 2017 12:03 pm
Location: Philippines
by scotty101 » 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
Posts: 2082
Joined: Fri Jun 08, 2012 6:03 pm
by infernowheels » 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.
User avatar
Posts: 8
Joined: Sat Mar 11, 2017 12:03 pm
Location: Philippines
by yaheath » 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)
Posts: 11
Joined: Tue Mar 28, 2017 9:58 pm