lunarkingdom
Posts: 127
Joined: Sun Jul 20, 2014 3:38 am

Pi B+ 40 pin GPIO how to use buttons and how many can I use?

Sun Jul 20, 2014 8:12 pm

Pi B+ 40 pin GPIO how to use buttons and how many can I use?

I am using all digital buttons for a 2 player arcade build, I have two pieces of this joystick:

http://www.digikey.com/product-detail/e ... ND/2138044

I also have 20 of these buttons:

http://www.ebay.com/itm/321440527437?_t ... EBIDX%3AIT

My B+ Pi pinout is:

Image

Is there an easy to understand tutorial to do this? I want to make 2 players each have a joystick (4 gpio pins and a ground X 2), 4 action keys (4 gpio pins and a ground X 2), a select and a start key (2 gpio pins and a ground X 2). If I connect them to the pins I mentioned can I just start an emulator and configure the buttons by using them or will there be code to write? Am I missing anything? Your help is much appreciated, thank you.

lunarkingdom
Posts: 127
Joined: Sun Jul 20, 2014 3:38 am

Re: Pi B+ 40 pin GPIO how to use buttons and how many can I

Sun Jul 20, 2014 8:14 pm

so that is a grand total of 20 GPIO pins and lets say 5 ground pins with 4 grounds each.

User avatar
rpdom
Posts: 15396
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Pi B+ 40 pin GPIO how to use buttons and how many can I

Sun Jul 20, 2014 9:07 pm

A ground pin is a ground pin. They are all connected together. It doesn't matter how many things are connected to each.

lunarkingdom
Posts: 127
Joined: Sun Jul 20, 2014 3:38 am

Re: Pi B+ 40 pin GPIO how to use buttons and how many can I

Sun Jul 20, 2014 9:27 pm

Thanks RPDom, good to know, I assumed as much but had seen some others talking about they were worried using too many grounds per pin. Appreciate your input!

User avatar
DigitalLumberjack
Posts: 336
Joined: Thu May 22, 2014 8:55 am
Location: France
Contact: Website

Re: Pi B+ 40 pin GPIO how to use buttons and how many can I

Sun Jul 20, 2014 9:31 pm

Hi,

You will have to connect your buttons to GPIOs and then use a software or a driver that will read gpio status and convert them into joystick events.

See https://learn.adafruit.com/retro-gaming ... pi/buttons and http://blog.thestateofme.com/2012/08/10 ... -joystick/ for more informations.

I wrote a kernel space module that do the thing, but i stille have not modified it for B+ revision. This week maybe.

lunarkingdom
Posts: 127
Joined: Sun Jul 20, 2014 3:38 am

Re: Pi B+ 40 pin GPIO how to use buttons and how many can I

Sun Jul 20, 2014 9:43 pm

Awesome! Thank you digitalumberjack! Reading these now!

lunarkingdom
Posts: 127
Joined: Sun Jul 20, 2014 3:38 am

Re: Pi B+ 40 pin GPIO how to use buttons and how many can I

Sun Jul 20, 2014 9:55 pm

this is probably a dumb question but just to be sure, when I compile my ports to allowed keys with retrogame, I will have 2 joysticks, one for each of 2 players. The example given was this:

struct {
int pin;
int key;
} io[] = {
// Input Output (from /usr/include/linux/input.h)
{ 25, KEY_LEFT },
{ 9, KEY_RIGHT },
{ 10, KEY_UP },
{ 17, KEY_DOWN },
{ 23, KEY_LEFTCTRL },
{ 7, KEY_LEFTALT }
};

My question is if I use the arrow keys for one joystick cant I just use q,w,e and r for the other so they act separately from each other? I assume I can use any of the valid key code names for whatever button I want then configure the keys in the emulators after I get them set up, does this sound correct?

lunarkingdom
Posts: 127
Joined: Sun Jul 20, 2014 3:38 am

Re: Pi B+ 40 pin GPIO how to use buttons and how many can I

Sun Jul 20, 2014 10:06 pm

Is there a default standard on the keys I select for gameplay with the emulators that is universal so I could just setup the correct keys the first time or is that wishful thinking?

Ian57
Posts: 3
Joined: Tue Aug 12, 2014 4:09 pm

Re: Pi B+ 40 pin GPIO how to use buttons and how many can I

Tue Aug 12, 2014 4:12 pm

Perhaps you should try that ;) https://github.com/ian57/Raspicade-Retr ... ayer-BPlus

the most common key mapping is the Mame key mapping :

P1 Up (KEY_UP or JOY_1_UP)
P1 Down (KEY_DOWN or JOY_1_DOWN)
P1 Left (KEY_LEFT or JOY_1_LEFT)
P1 Right (KEY_RIGHT or JOY_1_RIGHT)
P1 Button 1 (KEY_LCONTROL or JOY_1_BUTTON1 or MOUSE_1_BUTTON1)
P1 Button 2 (KEY_LALT or JOY_1_BUTTON2 or MOUSE_1_BUTTON3)
P1 Button 3 (KEY_SPACE or JOY_1_BUTTON3 or MOUSE_1_BUTTON2)
P1 Button 4 (KEY_LSHIFT or JOY_1_BUTTON4)
P1 Button 5 (KEY_Z or JOY_1_BUTTON5)
P1 Button 6 (KEY_X or JOY_1_BUTTON6)
P1 Button 7 (KEY_C or JOY_1_BUTTON7)
P1 Button 8 (KEY_V or JOY_1_BUTTON8)
P1 Button 9 (KEY_BACKSPACE or JOY_1_BUTTON9)
P1 Button 10 (KEY_ESC or JOY_1_BUTTON10)
P1 Start (KEY_1 or JOY_1_START)
P1 Select (KEY_5 or JOY_1_SELECT)
P2 Up (KEY_R or JOY_2_UP)
P2 Down (KEY_F or JOY_2_DOWN)
P2 Left (KEY_D or JOY_2_LEFT)
P2 Right (KEY_G or JOY_2_RIGHT)
P2 Button 1 (KEY_A or JOY_2_BUTTON1)
P2 Button 2 (KEY_S or JOY_2_BUTTON2)
P2 Button 3 (KEY_Q or JOY_2_BUTTON3)
P2 Button 4 (KEY_W or JOY_2_BUTTON4)
P2 Button 5 (KEY_E or JOY_2_BUTTON5)
P2 Button 6 (KEY_T or JOY_2_BUTTON6)
P2 Button 7 (KEY_O or JOY_2_BUTTON7)
P2 Button 8 (KEY_L or JOY_2_BUTTON8)
P2 Button 9 (KEY_TAB or JOY_2_BUTTON9)
P2 Button 10 (KEY_ESC or JOY_2_BUTTON10)
P2 Start (KEY_2 or JOY_2_START)
P2 Select (KEY_6 or JOY_2_SELECT)
Escape (KEY_ESC)

User avatar
DigitalLumberjack
Posts: 336
Joined: Thu May 22, 2014 8:55 am
Location: France
Contact: Website

Re: Pi B+ 40 pin GPIO how to use buttons and how many can I

Wed Aug 13, 2014 8:48 am

Hi,
i updated the gpio joystick driver, take a look of you want to use your gpios a joysticks :
https://github.com/digitalLumberjack/mk ... ystick_rpi

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

Re: Pi B+ 40 pin GPIO how to use buttons and how many can I

Wed Aug 13, 2014 9:03 am

The best way to connect a button to a PI is like this:
[attachment=0]pull_up.png[/attachment]

The 10K pull-up pulls the input to 3V3, and does it more reliable than the weak (>50K) internal pull-up.
The 1K series resistor protects the GPIO from a possible short to GND when the button is pressed, and the GPIO is programmed to output a high (accidentally).

Note the GND is simply any GND pin of the PI.

If you want the circuit to register a high when the button is pressed instead of a low, then simply switch the positions of the button and the 10K resistor, so the 10K resistor becomes a pulldown resistor.
Attachments
pull_up.png
pull_up.png (23.64 KiB) Viewed 64834 times

dan3008
Posts: 1172
Joined: Wed Aug 15, 2012 1:05 pm

Re: Pi B+ 40 pin GPIO how to use buttons and how many can I

Wed Aug 13, 2014 9:11 am

DigitalLumberjack wrote: Hi mahjongg,
Could you explain why it is the best way ? Is it really better than using gpios internal pullups ?
I just wonder what is the gain with this configuration, cause if i would have to add two resistor for each button, it should be for something :)

<edit> and now its vanished</edit>

I was just typing the same question as you posted..
I suppose Mahjongg's design is more "traditionally correct" using an external pullup instead of an internal one...
Last edited by dan3008 on Wed Aug 13, 2014 9:12 am, edited 1 time in total.
dan3008 wrote:Pays your money, takes your choice

User avatar
DigitalLumberjack
Posts: 336
Joined: Thu May 22, 2014 8:55 am
Location: France
Contact: Website

Re: Pi B+ 40 pin GPIO how to use buttons and how many can I

Wed Aug 13, 2014 9:12 am

Lol question censored :)

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

Re: Pi B+ 40 pin GPIO how to use buttons and how many can I

Wed Aug 13, 2014 9:15 am

sorry, you both reacted too soon, and I simply wasn't finished yet, also I used a wrong picture first, that had the pull-up on the wrong side of the series resistor. To to avoid confusion I tried to edit out the wrong picture, but bodged it up. :roll:

j0z0r
Posts: 52
Joined: Fri Oct 28, 2011 5:46 pm

Re: Pi B+ 40 pin GPIO how to use buttons and how many can I

Tue Aug 26, 2014 11:48 pm

lunarkingdom wrote:Pi B+ 40 pin GPIO how to use buttons and how many can I use?

I am using all digital buttons for a 2 player arcade build, I have two pieces of this joystick:

http://www.digikey.com/product-detail/e ... ND/2138044

I also have 20 of these buttons:

http://www.ebay.com/itm/321440527437?_t ... EBIDX%3AIT

My B+ Pi pinout is:

Image

Is there an easy to understand tutorial to do this? I want to make 2 players each have a joystick (4 gpio pins and a ground X 2), 4 action keys (4 gpio pins and a ground X 2), a select and a start key (2 gpio pins and a ground X 2). If I connect them to the pins I mentioned can I just start an emulator and configure the buttons by using them or will there be code to write? Am I missing anything? Your help is much appreciated, thank you.
I would like to point out that this method of constantly poling the GPIO is taxing to the processor. That may not be an issue for your build, especially if you are emulating older systems/games, or if your objective is to keep the part count/cost low. A solution if this is a problem would be to "offload" the polling of the GPIO to a smaller, less capable processor, and have that one report to the Pi when it has an interrupt. An Arduino is more than capable of this, and there are examples online that can be found through a quick Google search

Nitrogen_Widget
Posts: 38
Joined: Tue Aug 28, 2012 5:52 pm

Re: Pi B+ 40 pin GPIO how to use buttons and how many can I

Sun Aug 31, 2014 9:02 am

Or he can pick up an inexpensive atmega device and program it using the kade software.

Vill
Posts: 1
Joined: Wed Sep 10, 2014 8:21 am

Re: Pi B+ 40 pin GPIO how to use buttons and how many can I

Wed Sep 10, 2014 8:38 am

You could use the SPI signals with the Microchip SPC23S17 16 bit I/O port expander chip.
It has 2 interrupt outputs and can be programmed to generate interrupt on individual inputs.
you allso have the possibility of configuring outputs maybe for leds, vibrator etc.,
and you have address selection if you want to add more than one port chip.

Moayidd
Posts: 21
Joined: Sun May 03, 2015 4:33 am

Re: Pi B+ 40 pin GPIO how to use buttons and how many can I

Mon Sep 07, 2015 4:23 pm

I've asked this question on a different thread without a response so far. I'm trying to use GPIO pins below pin 28 (because a 3.2 waveshare tft takes up all the rest) without success.
The setup is very simple, a switch connected to ground on one end and to a pin on the other. The switches are normally closed and open when clicked. This works for GPIO5 and GPIO6 only not GPIO's 12, 13, 16,19, 20, 21, 26.
Here's the code trying GPIO13

Code: Select all

#!/usr/bin/env python2.7
import RPi.GPIO as GPIO
import signal
import sys
import time
def gpio5clicked(channel):
     print 'GPIO5 Clicked!'

def gpio6clicked(channel):
    print 'GPIO6 Clicked!'

def gpio13clicked(channel):
    print 'You pressed GPIO13 !'

def signal_handler(signal, frame):
     print 'Quiting ...!'
     GPIO.cleanup()       # clean up GPIO on CTRL+C exit
     quit()

GPP1=5
GPP2=6
GPP3=13

GPIO.setmode(GPIO.BCM)
GPIO.setup(GPP1, GPIO.IN, pull_up_down =  GPIO.PUD_UP)
GPIO.setup(GPP2, GPIO.IN, pull_up_down =  GPIO.PUD_UP)
GPIO.setup(GPP3, GPIO.IN, pull_up_down =  GPIO.PUD_UP)

signal.signal(signal.SIGINT, signal_handler)
GPIO.add_event_detect(GPP1, GPIO.FALLING, callback=gpio5clicked, bouncetime=300)
GPIO.add_event_detect(GPP2, GPIO.FALLING, callback=gpio6clicked, bouncetime=300)
GPIO.add_event_detect(GPP3, GPIO.FALLING, callback=gpio13clicked, bouncetime=300)

while True:
     time.sleep(1)
It appears that only GPIO's upto 7 can be used and nothing else.
I'm using a Pi2 4.1.6-v7 and python 5.11.
Any suggestions ?

Return to “Gaming”