piefairy
Posts: 12
Joined: Wed Dec 03, 2014 9:52 pm

Adding buttons as keys

Tue Sep 01, 2020 6:34 pm

I am trying to add 2 physical buttons to the GPIO pins to emulate 2 different keyboard keys. I know this has probably been asked a thousand times, but I have not had success with following some of the others. I have tried Pikeyd and it does not want to work. Ideally I would like to use the onboard resistors rather than using secondary resistors, but that is optional. Anyone have a simple step by step guide for this? What keys are used doesn't matter, I can change their function in the program that this is going into. I do need the keys to not spam the system if pressed though, IE holding down the button. I am familiar with the pin header and using pin 6 for ground and looking to have pin 8 and 10 for the inputs. I am planning to have this run on startup by adding it to rc.local.

Thanks in advance for the help.

msl
Posts: 190
Joined: Tue Jul 07, 2020 9:12 pm
Location: Munich
Contact: Website Twitter

Re: Adding buttons as keys

Tue Sep 01, 2020 7:42 pm


scotty101
Posts: 4256
Joined: Fri Jun 08, 2012 6:03 pm

Re: Adding buttons as keys

Wed Sep 02, 2020 7:42 am

+1 for gpio-keys. By far the simplest way to do this.

Copied from the README to give you a head start

Code: Select all

Name:   gpio-key
Info:   This is a generic overlay for activating GPIO keypresses using
        the gpio-keys library and this dtoverlay. Multiple keys can be
        set up using multiple calls to the overlay for configuring
        additional buttons or joysticks. You can see available keycodes
        at https://github.com/torvalds/linux/blob/v4.12/include/uapi/
        linux/input-event-codes.h#L64
Load:   dtoverlay=gpio-key,<param>=<val>
Params: gpio                    GPIO pin to trigger on (default 3)
        active_low              When this is 1 (active low), a falling
                                edge generates a key down event and a
                                rising edge generates a key up event.
                                When this is 0 (active high), this is
                                reversed. The default is 1 (active low)
        gpio_pull               Desired pull-up/down state (off, down, up)
                                Default is "up". Note that the default pin
                                (GPIO3) has an external pullup
        label                   Set a label for the key
        keycode                 Set the key code for the button
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

piefairy
Posts: 12
Joined: Wed Dec 03, 2014 9:52 pm

Re: Adding buttons as keys

Wed Sep 02, 2020 5:34 pm

I appreciate the help. I am having a hard time figuring out how to enable this. I understand modifying the config.txt to load the overlay, but not what file I need to configure to create the parameters. I thought it was gpio-key.dtbo, but nano cannot read it.

msl
Posts: 190
Joined: Tue Jul 07, 2020 9:12 pm
Location: Munich
Contact: Website Twitter

Re: Adding buttons as keys

Wed Sep 02, 2020 6:30 pm

piefairy wrote:
Wed Sep 02, 2020 5:34 pm
I appreciate the help. I am having a hard time figuring out how to enable this. I understand modifying the config.txt to load the overlay, but not what file I need to configure to create the parameters. I thought it was gpio-key.dtbo, but nano cannot read it.
No need to do anything with dtbo
As a test add “dtoverlay=gpio-key,gpio=3,active_low=1,gpio_pull=up,keycode=30” to config.txt
With this GPIO3 to ground should give you ‘A’ button press

piefairy
Posts: 12
Joined: Wed Dec 03, 2014 9:52 pm

Re: Adding buttons as keys

Wed Sep 02, 2020 7:04 pm

I put in that command to config.txt at the end of the page. No luck when i jump pin 6 to pin 3. this is what the end of the config looks like

#dtoverlay=gpio-ir,gpio_pin=17
#dtoverlay=gpio-ir-tx,gpio_pin=18

# Additional overlays and parameters are documented /boot/overlays/README

# Enable audio (loads snd_bcm2835)
dtparam=audio=on

[pi4]
# Enable DRM VC4 V3D driver on top of the dispmanx display stack
dtoverlay=vc4-fkms-v3d
max_framebuffers=2

[all]
#dtoverlay=vc4-fkms-v3d
enable_uart=1
dtoverlay=gpio-key,gpio=3,active_low=1,gpio_pull=up,keycode=30

msl
Posts: 190
Joined: Tue Jul 07, 2020 9:12 pm
Location: Munich
Contact: Website Twitter

Re: Adding buttons as keys

Wed Sep 02, 2020 7:21 pm

piefairy wrote:
Wed Sep 02, 2020 7:04 pm
No luck when i jump pin 6 to pin 3.
Try your luck with pin 5 to pin 6 :)
GPIO3 is pin 5

piefairy
Posts: 12
Joined: Wed Dec 03, 2014 9:52 pm

Re: Adding buttons as keys

Wed Sep 02, 2020 7:32 pm

That did it, thanks.

Ok, so if i want to use F5 and F6 with GPIO 3 and 4 it should look like this correct?

dtoverlay=gpio-key,gpio=3,active_low=1,gpio_pull=up,keycode=63
dtoverlay=gpio-key,gpio=4,active_low=1,gpio_pull=up,keycode=64

piefairy
Posts: 12
Joined: Wed Dec 03, 2014 9:52 pm

Re: Adding buttons as keys

Wed Sep 02, 2020 7:36 pm

Also, I would like to assign a reboot function to the F5 key. What config should I alter to do this? Or if there is a direct pin option to trigger a reboot. I don't want a shutdown, I see that that is an overlay option.

scotty101
Posts: 4256
Joined: Fri Jun 08, 2012 6:03 pm

Re: Adding buttons as keys

Thu Sep 03, 2020 8:06 am

piefairy wrote:
Wed Sep 02, 2020 7:36 pm
Also, I would like to assign a reboot function to the F5 key. What config should I alter to do this? Or if there is a direct pin option to trigger a reboot. I don't want a shutdown, I see that that is an overlay option.
You'll need something different to do that. There are quite a few "addons" for the pi that add reset buttons so it is well documented and easy to find if you search for it. For example https://scruss.com/blog/2017/10/21/comb ... pberry-pi/

There is even a simple example in the gpiozero documentation showing you how to perform a shutdown with a gpio pin. To make this a reset, you just need to change "poweroff" to "reboot"
https://gpiozero.readthedocs.io/en/stab ... own-button
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Beginners”