User avatar
VeryNoob
Posts: 49
Joined: Thu Feb 14, 2013 1:07 pm
Location: Australia

Phantom Button Presses

Sat Jun 08, 2013 1:11 pm

I've hooked up and LCD and 8 buttons to my GPIO. I did it via a breadboard, but was having some issues, so I've now soldered it to a humble pi to get rid of any connection errors. A schematic is in a previous thread here with some of the GPIO swapped around to make it fit on the humble pi easier. I'm using a modified meistervision script.

The LCD works fine (GPIO 4, 10, 9, 11, 8 7) and 3 of the buttons works (GPIO 24, 25, 27). The problem is the other 5 buttons don't work.

When I swap the GPIO numbers in the scrip it acts like I am holding the button down. All the wiring looks fine and the 5 not working are the same arrangement as the 3 that are. Any ideas what I am missing?

User avatar
VeryNoob
Posts: 49
Joined: Thu Feb 14, 2013 1:07 pm
Location: Australia

Re: Phantom Button Presses

Mon Jun 10, 2013 1:26 pm

These are the GPIO and what happens when I assign them in software
GPIO2(P1-3) - Always pressed
GPIO3(P1-5) - Always pressed

GPIO17(P1-11) - Does Nothing
GPIO22(P1-15)- Does Nothing
GPIO23(P1-16)- Does Nothing

GPIO24(P1-18) - Works
GPIO25(P1-22) - Works
GPIO27(P1-13) - Works

GPIO18(P1-12) - reserved for backlight PWM
GPIO4(P1-7) - LCD_RS
GPIO10(P1-19) - LCD_E
GPIO9(P1-21) - LCD_D4
GPIO11(P1-23) - LCD_D5
GPIO8(P1-24) - LCD_D6
GPIO7(P1-26) - LCD_D7

[Edited to make it clearer]
Last edited by VeryNoob on Tue Jun 11, 2013 3:19 am, edited 1 time in total.

User avatar
gordon@drogon.net
Posts: 2022
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: Phantom Button Presses

Mon Jun 10, 2013 1:29 pm

VeryNoob wrote:These are the GPIO and what happens when I assign them in software
2 - Constantly Pressed
3 - Constantly Pressed
17 - Does nothing
22 - Does nothing
22 - Does nothing
23 - Does nothing

27 - Works
24 - Works
25 - Works

4 - LCD_RS
10 - LCD_E
6 - LCD_D4
11 - LCD_D5
8 - LCD_D6
7 - LCD_D7

18 - I've saved it for backlight control as it's got PWM.
How have you wired the buttons up?

However lets start with 2 and 3 - these have on-board 1800 owm resistors pulling them to 3.3v. So your buttons should connect these to 0v. Push the button and the input will read 0.

The others - did you use any pull-up or pull-down resistors? If not, then get wiringPi and try this:

gpio -g 17 mode up
gpio -g 22 mode up

and so on. (Not that 22 is a Rev 1 pin, Rev 2's use 27 instead of 22)

connect the buttons from the pin to 0v. they will read 1 when not pushed and read 0 when pushed.

The problem is probably that the inputs are floating and will be susceptible to stray voltages making them almost random.

-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
piglet
Posts: 915
Joined: Sat Aug 27, 2011 1:16 pm

Re: Phantom Button Presses

Mon Jun 10, 2013 1:38 pm

What he said.

Does wiringPi sort out debounce for you, or is that something you need to code for?

I decided to try my hand at electronics by jumping in the deep end by trying to build an LED "Word clock" using the MSP chip from a TI Launchpad, a large pile of LEDs, a radio clock receiver and a bunch of shift registers. It was the *jolly* debouncing of buttons which made me give up in the end and shove it into my wardrobe! It worked perfectly apart from the buttons.

User avatar
gordon@drogon.net
Posts: 2022
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: Phantom Button Presses

Mon Jun 10, 2013 2:06 pm

piglet wrote:What he said.

Does wiringPi sort out debounce for you, or is that something you need to code for?

I decided to try my hand at electronics by jumping in the deep end by trying to build an LED "Word clock" using the MSP chip from a TI Launchpad, a large pile of LEDs, a radio clock receiver and a bunch of shift registers. It was the *jolly* debouncing of buttons which made me give up in the end and shove it into my wardrobe! It worked perfectly apart from the buttons.
wiringPi doesn't do debounce - that's a user problem ;-)

Debounce is usually easy - so you get a key press - do the action, then

cycle
wait 20mS
repeat while key pressed


Unless you have really bouncy buttons that's worked very well for me in simple situations. You can get very complex though - e.g. if polling lots of buttons, then you might not want to waste time on just one button, so you need to keep more state

-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13223
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Phantom Button Presses

Mon Jun 10, 2013 2:08 pm

The PI has 1K8 pullups on-board for the two I2C GPIO's (pin 3 and pin 5), so (as said) you should use active low buttons (buttons connected between GND and the GPIO pin, which is also the standard way to do it), but these GPIO's also should not have the 1K current limiting resistors (between the button and GPIO), as together with the 1K8 pull-up they would form a resistor divider, and the GPIO pin won't be pulled low enough for a reliable "logical 0". Instead with an 1K series resistor the GPIO will be pulled down to: 1K / (1K8 +1K) x 3.3V = 1.1V which is neither logical high, nor logical low!

To effectively suppress keybounce simply place a 100nF capacitor over the key.

User avatar
VeryNoob
Posts: 49
Joined: Thu Feb 14, 2013 1:07 pm
Location: Australia

Re: Phantom Button Presses

Mon Jun 10, 2013 9:45 pm

See attachment for how I wired the switches. So you suggest that I should remove the 1k ohm resistor for GPIO2(P1-3) and GPIO3(P1-5)?

Gordon, you said "Not that 22 is a Rev 1 pin, Rev 2's use 27 instead of 22". I thought there was a GPIO22(P1-15) & GPIO27(P1-13) on the rev2's. I've probably confused you with the way I wrote out the numbers. I've tired to make it clearer below.

I see why GPIO2(P1-3) & GPIO3(P1-5) are different, but why does GPIO24(P1-18) work when GPIO22(P1-15) doesn't?

The python I am using is based on meistervisions youtube. The relavent bits are below.

Code: Select all

import RPi.GPIO as GPIO
NEXT = 24 # Define GPIO for Radio Controls
PREV = 25

  GPIO.setmode(GPIO.BCM)    # Use BCM GPIO numbers
  GPIO.setup(NEXT, GPIO.IN) # Next Channel button
  GPIO.setup(PREV, GPIO.IN) # Previous Channel button

  while 1:
	  if ( GPIO.input(NEXT) == True):	 
		os.system("mpc next")
		os.system("mpc play")
            
	  if ( GPIO.input(PREV) == True):
		os.system("mpc prev")
		os.system("mpc play")
GPIO2(P1-3) - Always pressed
GPIO3(P1-5) - Always pressed

GPIO17(P1-11) - Does Nothing
GPIO22(P1-15)- Does Nothing
GPIO23(P1-16)- Does Nothing

GPIO24(P1-18) - Works
GPIO25(P1-22) - Works
GPIO27(P1-13) - Works

GPIO18(P1-12) - reserved for backlight PWM
GPIO4(P1-7) - LCD_RS
GPIO10(P1-19) - LCD_E
GPIO9(P1-21) - LCD_D4
GPIO11(P1-23) - LCD_D5
GPIO8(P1-24) - LCD_D6
GPIO7(P1-26) - LCD_D7
Attachments
Switches.tif
Switches.tif (6.58 KiB) Viewed 3772 times

User avatar
VeryNoob
Posts: 49
Joined: Thu Feb 14, 2013 1:07 pm
Location: Australia

Re: Phantom Button Presses

Mon Jun 10, 2013 10:00 pm

Or should GPIO2(P1-3) & GPIO3(P1-5) have nothing but a switch between them and ground?

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13223
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Phantom Button Presses

Tue Jun 11, 2013 7:53 pm

VeryNoob wrote:Or should GPIO2(P1-3) & GPIO3(P1-5) have nothing but a switch between them and ground?
yes, because the "switches.tif" diagram doesn't work when there are strong (1.8K) pullups on board, a 10K pulldown resistor cannot overcome the pull of the much stronger 1.8K pull-up, and neither will a switch connected to GND work if you still use the 1K series resistor. So Yes, simply wire the switches of the first two GPIO's between the GPIO and GND and don't use the 1K resistor in this case. In fact its better to wire all the switches to GND instead of 3.3V.

Still you seem to be persevering in that on all the other buttons the closed button should pull up the GPIO by connecting it to 3V3, so that you get a "1" when you press the button. However that is NOT the universal standard way of doing it, the standard way is that the button shorts the GPIO (through 1K) to GND, and pressing the button thus generates a "0". So one end of each button should be connected to GND, not to 3.3V.
This might seem odd, or counter-intuitive, but for the software it doesn't matter at all, and its thought to be much safer to do it this way.

Also if you do it this way, all the buttons work the same as the first two, and the only difference is that in the first two cases the 1K resistor should be removed.

User avatar
VeryNoob
Posts: 49
Joined: Thu Feb 14, 2013 1:07 pm
Location: Australia

Re: Phantom Button Presses

Wed Jun 12, 2013 12:09 am

Thanks for your help. I have no issue using pull up or pull down. In an earlier thread I was going in a certain direction, but then to make the humble pi layout work, I swapped some input switches to GPIO2&3 to make the layout work better.

I've attached my new layout, but given the removal of the 1k resistor from GPIO2&3 do I loose protection on the pins should I accidentally set them as outputs? Can that be re-instated? Or should I used GPIO2&3 to connected to the LCD data lines so I don't need to protect them? Can they be used for the LCD with there built in resistors?
Attachments
PullUpSwitch.tif
PullUpSwitch.tif (21.73 KiB) Viewed 3689 times

User avatar
VeryNoob
Posts: 49
Joined: Thu Feb 14, 2013 1:07 pm
Location: Australia

Re: Phantom Button Presses

Thu Jun 13, 2013 12:55 pm

YAY! IT WORKED!

Just thought I would follow up for the fellow newbies who found this thread. It worked. And I also wanted to thank you guys for your help.

This is my little test code.

Code: Select all

#!/usr/bin/env python

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

GPIO.setmode(GPIO.BCM)
GPIO.setup(2, GPIO.IN)
GPIO.setup(3, GPIO.IN)
GPIO.setup(17, GPIO.IN)
GPIO.setup(22, GPIO.IN)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
GPIO.setup(25, GPIO.IN)
GPIO.setup(27, GPIO.IN)

print 'Program Works - Now press a button'

while True:
	if ( GPIO.input(2) == False ):
		print 'GPIO 02 Pressed'
	if ( GPIO.input(3) == False ):
		print 'GPIO 03 Pressed'
	if ( GPIO.input(17) == False ):
		print 'GPIO 17 Pressed'
	if ( GPIO.input(22) == False ):
		print 'GPIO 22 Pressed'
	if ( GPIO.input(23) == False ):
		print 'GPIO 23 Pressed'
	if ( GPIO.input(24) == False ):
		print 'GPIO 24 Pressed'
	if ( GPIO.input(25) == False ):
		print 'GPIO 25 Pressed'
	if ( GPIO.input(27) == False ):
		print 'GPIO 27 Pressed'
	sleep(0.1);

Return to “Interfacing (DSI, CSI, I2C, etc.)”