17

I considered myself out of this sort of beginner level stuff a long time ago, but I guess I'm not as smart as I thought I was because I seem to be having an exceptional level of trouble with it!

So I've finally finished exams and can play with my new toy, so the first thing I did was get an LCD character display rigged up over UART. I got that working no problem, so I am having a really hard time figuring out why I am having such a hard time with this button!

So I've set up a button similar to the way one would with an arduino; with a 1k resistor connecting to ground. I used my multimeter to ensure that the button was working and putting out the right amount of power, which it is. I SSH into my shell, plug the button into GPIO pin 7 and went into python. Here's what I tried:

GPIO.pinout(GPIO.BOARD)
GPIO.setup(7,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
while 1==1:
    inputval = GPIO.input(7)
    print inputval

This gives me an endless loop of False regardless of how I press the button. I tried it with GPIO.PUD_UP as well and got an endless loop of True instead. I also tried it with GPIO.PUD_OFF and got more False.

Am I missing something here?

Michael
  • 171
  • 1
  • 3
  • So you have the switch in series with the 1k resistor to GND? That should work with GPIO.PUD_UP. What voltage do you see with GPIO.PUD_UP on and the button pressed? – John La Rooy Aug 21 '12 at 01:07
  • Slightly confused; what is the switch connected to? – Alex Chamberlain Aug 21 '12 at 07:08
  • Hello Michael and welcome to [raspberrypi.se]! I have taken the liberty of merging your two accounts. Thanks! –  Aug 21 '12 at 23:36

2 Answers2

6

My GPIO doesn't have pinout, I had to change it to setmode

import time
from RPi import GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
while True:
    inputval = GPIO.input(7)
    print inputval
    time.sleep(1)

Since you are using GPIO.BOARD pin 7 is the one labelled GPIO 4(GPCLK0). If you meant to use GPIO 7(CE1), you should use GPIO.setmode(GPIO.BCM) instead

enter image description here

I tested the code by touching a 1k resistor from 3V3 Power to GPIO 4(GPCLK0)

John La Rooy
  • 11,947
  • 9
  • 47
  • 75
  • I am almost certain your correct on why it was not working. I was under the impression GPIO.BOARD was for the pinout of the board itself as shown in the pictures, not the physical pin numbering scheme. When I get home from work I will try it out and I am sure it will work. – Michael Aug 21 '12 at 14:55
1

I never use "pull_up_down=GPIO.PUD_DOWN" in the setup of the pin. Try this:

GPIO.setmode(GPIO.BCM)
GPIO.setup(PinNum,GPIO.IN)
while 1==1:
    if GPIO.input(PinNum)==1:
        print "True"
    else:
        print "False"

As gnibbler said, make sure PinNum is for the BCM GPIO.

Matthew
  • 979
  • 3
  • 10
  • 20