1 1
ALYD

GPIO button press not working properly

Recommended Posts

Hello, hope you can help me with my issue. I have an Orange Pi Zero+ with an H5 processor. My requirement is to detect when a button is pressed, my input port is PA1 or pin 11, my pin for 3.3V is 17, I'm using the pyA20 library, have also setup this project: https://github.com/herzig/orangepi_PC_gpio_pyH5. My code is based on the read_button example (https://github.com/herzig/orangepi_PC_gpio_pyH5/blob/master/examples/read_button.py#L46). Bellow I will show the code, but the main problem is that the gpio.input() method is usually returning 0 but every few seconds it returns 1, which is only supposed to happen when button is pressed. I'm a complete newbie in this world so excuse me if this is an easy thing to solve. For the record I have also tried with other libraries an none have worked for me. Thanks in advance.

Example code:

#!/usr/bin/env python
"""Read button.

Make gpio input and enable pull-up resistor.
"""

import os
import sys
import time

import logging


if not os.getegid() == 0:
    sys.exit('Script must be run as root')


from pyA20.gpio import gpio
from pyA20.gpio import connector
from pyA20.gpio import port

__author__ = "Stefan Mavrodiev"
__copyright__ = "Copyright 2014, Olimex LTD"
__credits__ = ["Stefan Mavrodiev"]
__license__ = "GPL"
__version__ = "2.0"
__maintainer__ = __author__
__email__ = "support@olimex.com"

button = port.PA1 #connector.gpio3p40

"""Init gpio module"""
gpio.init()

"""Set directions"""
gpio.setcfg(button, gpio.INPUT)

"""Enable pullup resistor"""
# gpio.pullup(button, gpio.PULLUP)
gpio.pullup(button, gpio.PULLDOWN)     # Optionally you can use pull-down resistor


try:
    while True:
        state = gpio.input(button)      # Read button state
        print (state)
        if state == 1:
            print("button pressed")
            logging.info("button pressed")
        time.sleep(0.2)

except KeyboardInterrupt:
    print ("Goodbye.")

 

Share this post


Link to post
Share on other sites
58 minutes ago, ALYD said:

my input port is PA1 or pin 11, my pin for 3.3V is 17

Are you sure you are using the right pins ? Because PA1 is the UART RX ... Simply use another pin such PD11 ...

Also, you should normally use it with PULLUP option and bringing the button to GND and printing "pressed" when reading "0" detecting the pull down to GND.

 

Share this post


Link to post
Share on other sites

Hello @martinayo

Thanks for your reply, please according to the image that I'm attaching which is the pin you suggest I should use, because I can't identify the one you say in it.

Orange-Pi-Zero-pinout.jpg

Share this post


Link to post
Share on other sites
23 minutes ago, ALYD said:

because I can't identify the one you say in it.

Pretty normal since you are showing an OPiZero (H2), not OPiZeroPlus2 (H5) like you mentioned earlier ...

If your board is the OPiZero, I suggest to use GPIO10 which is PA10.

Share this post


Link to post
Share on other sites

Ok, I am using an OPiZeroPlus, not a OpiZeroPlus2, right now I'm trying with PA10 as suggested by @martinayotte and Im also using the PULLUP option and bringing the button to GND, and printing when the value is 0, and the problem persists, it ocassionaly, without the button being put to GND it launches the 0 value. Any suggestions?

Share this post


Link to post
Share on other sites
30 minutes ago, ALYD said:

Any suggestions?

Do you have long wires or short ones between the board and the button ? Make them shortest as possible ...

I've tested your script with the modification for PULLUP and state == 0 on my OPiZero (PA10) and on my OPiZeroPlus2 (PD14), and I don't have any issue ...

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
1 1