cscuilla
Posts: 58
Joined: Tue Apr 26, 2016 6:35 pm

Having trouble reading GPIO.IN state

Thu Jun 09, 2016 3:40 pm

I'm using a MFRC522 rfid card reader. I'd like different actions to happen, depending on if a button is pressed or not when the card is scanned. I cannot seem to read the state of the input button correctly, it always stays at 0.

Code: Select all

import MFRC522
import signal
import datetime
import time
import os
import sys
import RPi.GPIO as GPIO
import csv

continue_reading = True
MIFAREReader = MFRC522.MFRC522()

#VARIABLES
BTN1 = 18

# COMMANDS
GPIO.setmode(GPIO.BOARD)
GPIO.setup(BTN1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
os.system("sudo mount -a")

def end_read(signal, frame):
  global continue_reading
  continue_reading = False
  print "Ctrl+C captured, ending read."
  MIFAREReader.GPIO_CLEEN()
signal.signal(signal.SIGINT, end_read)
I believe the issue is within this section of code..

Code: Select all

while continue_reading:
    BTN_STATE = GPIO.input(BTN1) # I think this should be moved maybe?
    (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
    if status == MIFAREReader.MI_OK:
        print "Card detected"
    (status,backData) = MIFAREReader.MFRC522_Anticoll()
    if status == MIFAREReader.MI_OK:
        print "Card read UID: "+str(backData[0])+","+str(backData[1])+","+str(backData[2])+","+str(backData[3])+","+str($
        print BTN_STATE # This always prints 0, even when the button is pressed

User avatar
Burngate
Posts: 6302
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Having trouble reading GPIO.IN state

Fri Jun 10, 2016 4:30 pm

Would it be worth while concentrating on just the button hardware/software and getting that to work before adding in the rfid hardware/software?
Hardware-wise, I would like to see a circuit diagram, showing how you've connected the button.

Regarding software, the only bits in that code to do with the button that I could see appear to be

Code: Select all

import RPi.GPIO as GPIO
BTN1 = 18
GPIO.setmode(GPIO.BOARD)
GPIO.setup(BTN1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
while continue_reading:
    BTN_STATE = GPIO.input(BTN1) # I think this should be moved maybe?
...
...
...
        print BTN_STATE # This always prints 0, even when the button is pressed

cscuilla
Posts: 58
Joined: Tue Apr 26, 2016 6:35 pm

Re: Having trouble reading GPIO.IN state

Tue Jun 14, 2016 7:49 pm

Ok, so it works if I set the input as a PULL_UP resistor, and not PULL_DOWN.

User avatar
Burngate
Posts: 6302
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Having trouble reading GPIO.IN state

Wed Jun 15, 2016 7:53 am

... and your button is connected between the GPIO and ground?

Return to “Python”