Code Monkey home page Code Monkey logo

opi.gpio's Introduction

OPi.GPIO

https://travis-ci.org/rm-hull/OPi.GPIO.svg?branch=master https://coveralls.io/repos/github/rm-hull/OPi.GPIO/badge.svg?branch=master https://readthedocs.org/projects/opi-gpio/badge/?version=latest

https://img.shields.io/maintenance/yes/2021.svg?maxAge=2592000

A drop-in replacement library for RPi.GPIO for the Orange Pi Zero and other SBCs. Only the basic GPIO functions are replicated, using sysfs: this allows the GPIO pins to be accessed from user space.

See the documentation for install instructions and detailed API usage.

References

License

The MIT License

Copyright (c) 2018 Richard Hull

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

opi.gpio's People

Contributors

5kft avatar botter-nidnul avatar drizzle1 avatar farnsworth9qc avatar fionnoch avatar ipitsyn avatar mikalstill avatar nopnop2002 avatar orknist avatar pascallj avatar rm-hull avatar sapir avatar sgjava avatar sillo01 avatar stephensorriaux avatar winterscar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

opi.gpio's Issues

Need for speed, faster switching GPIO possible?

I'm trying to convert a project which is using pyA20 to OPi.GPIO to avoid running software as root.
I succeeded converting it but OPi.GPIO now is too slow at switching GPIO state.
I need fast switching GPIO to turn ON/OFF an infrared LED and communicate with an Air Conditioned unit.

Is there any way to speed up the switching of OPi.GPIO?

GPIO.PWM function takes exactly 2 arguments (4 given)

Hello there,

thanks a lot for your library, it helps me quite a lot.
Today, I decided to give a go at PWM, so I cloned your repo, built it, and then tried the PWM demo.
Here is what I got:

Traceback (most recent call last):
  File "pwm.py", line 10, in <module>
    p = GPIO.PWM(PWM_chip, PWM_pin, frequency_Hz, Duty_Cycle_Percent)    # new PWM on channel=LED_gpio frequency=38KHz
TypeError: function takes exactly 2 arguments (4 given)

The code begins like that:

import OPi.GPIO as GPIO

if __name__ == "__main__":

    PWM_chip = 0
    PWM_pin = 0
    frequency_Hz = 3800
    Duty_Cycle_Percent = 100

    p = GPIO.PWM(PWM_chip, PWM_pin, frequency_Hz, Duty_Cycle_Percent)    # new PWM on channel=LED_gpio frequency=38KHz
[...]

What did I do wrong?
Can you help?

Thanks.

Tests fail on python >=3.6

I've tried to update your Travis config for newer versions of python3 (my default is 3.7) to the Github/Travis defaults up to 3.10, and I seem to be getting some test failures relating to RuntimeError raising (or not raising) OSError. Tests are a little out of my python experience so far, so I wonder if you could shed some light on what's going wrong (you can peruse my fork, and the travis tests on travis-ci.com/mjeveritt hopefully).

I also unwittingly tried to port the Travis configuration over to github actions (you'll see this in other branches of my repo), and can't seem to fathom how best to set up tox and coveralls as you had done. I'm sure it's reasonably trivial if you know what you're doing!!

Thanks in advance!

Update in Pypi

Please could you update version from 0.5.2 -> 0.5.3 version in pypi.org
Thanks.

Support for Pull up / Pull down resistors

Pull request #8 added faux-compatibility for pull up/down resistors by adding an optional parameter, so that existing code doesn't break. This issue is a placeholder to add real support for pull-up/down resistors.

Related (originating) issue: #6

Support for bouncetime

Pull request #8 added faux-compatibility for bouncetime by adding an optional parameter, so that existing code doesn't break. This issue is a placeholder to add real support for bouncetime.

Related (originating) issue: #6

orangepi zero2 trouble issue help

orangepi zero2 python3

import time
import OPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
not working

GPIO.setmode(GPIO.orangepi.zero2)
not working

import orangepi.zero2
from OPi import GPIO
GPIO.setmode(orangepi.zero2.BOARD)
not working

GPIO.setboard(GPIO.ZERO2)
GPIO.setmode(GPIO.BOARD)
not working

how to working in GPIO.setboard

I want to know how to turn off or turn on the machine.

how to serboard, setmode

I wonder how to set it up.

Error when using add_event_detect

I have encountered a problem when using
GPIO.add_event_detect(channel)
on an orangepi zero

As soon as i use this function, the GPIO.input(channel) is random and static
The interrupt is working, but i cannot read the input any longer with the actual value, instead i get a constant 0 or a constant 1, but this seems to endependent of the input-state.
The PullUp is still active and even the Pin is no output - i just get no actual value.
If i skip the add_event_detect i can read the actual value of the pin.

I have installed OPi.GPIO with "sudo pip install OrangePi.GPIO" - at the moment i don't know which version i have.

test.zip

Has anyone got GPIO input working on OrangePi Prime?

I for the life of me can't get it to work. Here's the code I am using:

import OPi.GPIO as GPIO
import orangepi.pc
import time

def pin_number(letter, digit):
    return (ord(letter) - ord('A')) * 32 + digit

pin = pin_number('A', 13)

GPIO.setmode(orangepi.pc.BOARD)
GPIO.setwarnings(False)
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def event_callback(channel):
    print('Event detected: %s' % GPIO.input(pin))
    
GPIO.add_event_detect(pin, GPIO.BOTH, callback=event_callback, bouncetime=50)

while True:
    time.sleep(1)

I simply never get an edge detected. Output works just fine.

cannot detect inputs from within event detection

I have in issue trying to detect any gpio input changes from within an event detection callback.

My goal is to read an ir remote with the GPIO's, when an input is detected (a falling edge) I want to enter a function where i measure the number and duration of pulses of the remote for a fixed time. so far I can detect the initial pulse and enter the callback function however once inside the input seems to be fixed to high (1) and does not change. I know this is not correct as if i just print the inputs in the main loop then I can detect both 0's and 1's.

Any idea or guidance on this would be hugely appreciated!

import OPi.GPIO as GPIO
from time import sleep
from time import perf_counter

LED_gpio = 7 # pin name = PA6 wPi = 2 GPIO = 6 
ir_gpio = 5 # pin name = SCL.0 wPi = 1 GPIO = 11

GPIO.setboard(GPIO.PCPCPLUS)
GPIO.setmode(GPIO.BOARD)  # set up BOARD BCM SUNXI numbering
GPIO.setup(LED_gpio, GPIO.OUT)    # set pin as an output (LED)
GPIO.setup(ir_gpio, GPIO.IN)

def inputChange(channel):
    print("input interupt detected") 
    timeout = perf_counter()+3 #stay in this loop for 3 seconds
    counter_1 = 0
    counter_0 = 0
        
    while perf_counter()<timeout:
        if GPIO.input(ir_gpio)==1: # 1 if high 0 if low tells you input value
            counter_1 = counter_1 +1   
        else: 
            counter_0 = counter_0 +1 
        
    print("callback ended")
    print ("number of 1's detected = ", counter_1)
    print ("number of 0's detected = ", counter_0)
                          
GPIO.add_event_detect(ir_gpio, GPIO.FALLING, callback = inputChange, bouncetime = 3200) #bouncetime stops the callback from being run again for a set time in miliseconds. needs to be length of ir command

try:
    GPIO.output(LED_gpio, 0) 
    print("running program press ctrl+c to stop")
    while 1:
        sleep(4) #comment out when measuring all values
        #print("mainloop pin value = ", GPIO.input(ir_gpio)) #display all values
        
except KeyboardInterrupt:
    print("")
    print("program stopping") 
    GPIO.cleanup()

How exactly to port this library?

I'm fairly new to this domain, so please bear with me with some "so obvious to you" questions. :-)
This is a drop-in replacement lib for RPi.GPIO. But how exactly should I port this new lib for OPi? Should I install RPi library first as normally would do, then install this new lib as described in doc $ sudo pip install --upgrade OPi.GPIO? Thanks!

Pin Numbering

Could you please elaborate more on the sunxi numbering system. Thank you

OPi Zero internal pullup/down error

Hi!
After a recent OS (dietpi) and firmware update on my OPi Zero, my python code stopped working. I found that the problem is around "GPIO.setup(..., pull_up_down=GPIO.PUD_UP)".
When I run the code it spits out "AttributeError: 'module' object has no attribute 'PUD_UP' ".
Probably I screwed up something but I could not figure out what.

my code:

#!/usr/bin/python
import OPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(11,GPIO.IN,pull_up_down=GPIO.PUD_UP)

when run with ./mycode.py it replies:

Traceback (most recent call last):
  File "./test.py", line 6, in <module>
    GPIO.setup(11,GPIO.IN,pull_up_down=GPIO.PUD_UP)
AttributeError: 'module' object has no attribute 'PUD_UP'

Thanks for reading ;)

race condition when using the library as non root user

I have an udev rule in place that allows me to use the GPIOs as a normal user

SUBSYSTEM=="gpio", ACTION=="add", RUN="/bin/sh -c '\
  chown -R root:gpio /sys/class/gpio && chmod -R 770 /sys/class/gpio;\
  chown -R root:gpio /sys$DEVPATH && chmod -R 770 /sys$DEVPATH"

The problem is that, after writing to the /sys/class/gpio/export files, udev takes a little time to adjust the permissions, so when the library accesses the gpio file it doesn't have the permission to do so.
As a workaround I added a delay in GPIO.py (see attached patch opi.zip) but I think there should be a better way .
See also https://forum.armbian.com/index.php?/topic/5225-gpio-from-python-user-space-opigpio-app/

add_event_detect() prevent the script to exit

Not sure this is related to the library but just in case...
When I run the test script below, it works fine but then when I press ctrl+c, it does not exit.

import OPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
pin = 9
GPIO.setup(pin, GPIO.IN,initial=GPIO.HIGH)
def event_callback(pin):
        print pin
        print GPIO.input(pin)

GPIO.add_event_detect(pin, GPIO.FALLING, callback=event_callback)
while True:
        time.sleep(10)

Looks like the interrupt is caught hence sleep() is interrupted but then the only way to close the script is to kill it from a different console.

Traceback (most recent call last):
  File "gpio.py", line 12, in <module>
    time.sleep(10)
KeyboardInterrupt

If I comment out GPIO.add_event_detect(pin, GPIO.FALLING, callback=event_callback), it exists normally.
Thanks

OrangePi One Plus problem with Interrupts and Edge detection

I have an issue with events detection in my OrangePi One Plus . I'm running this script as a root user:

import orangepi.oneplus
import OPi.GPIO as GPIO
from time import sleep

channel = 21

GPIO.setmode(orangepi.oneplus.BOARD)
GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def my_callback(channel):
    print('This is a edge event callback function!')
    print('Edge detected on channel %s'%channel)
    print('This is run in a different thread to your main program')

GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback)

while True:    
    sleep(.5)

GPIO.cleanup()
print("Bye")

The output is:

inputsInterruptions.py:8: UserWarning: Pull up/down setting are not (yet) fully supported, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_UP)
inputsInterruptions.py:8: UserWarning: Channel 21 is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_UP)
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/local/lib/python2.7/dist-packages/OPi/event.py", line 74, in run
    sysfs.edge(self._pin, NONE)
  File "/usr/local/lib/python2.7/dist-packages/OPi/sysfs.py", line 84, in edge
    with open(path, "w") as fp:
IOError: [Errno 13] Permission denied: '/sys/class/gpio/gpio67/edge'

Traceback (most recent call last):
  File "inputsInterruptions.py", line 20, in <module>
    GPIO.cleanup()
  File "/usr/local/lib/python2.7/dist-packages/OPi/GPIO.py", line 572, in cleanup
    cleanup(list(_exports.keys()))
  File "/usr/local/lib/python2.7/dist-packages/OPi/GPIO.py", line 578, in cleanup
    cleanup(ch)
  File "/usr/local/lib/python2.7/dist-packages/OPi/GPIO.py", line 582, in cleanup
    event.cleanup(pin)
  File "/usr/local/lib/python2.7/dist-packages/OPi/event.py", line 162, in cleanup
    remove_edge_detect(pin)
  File "/usr/local/lib/python2.7/dist-packages/OPi/event.py", line 144, in remove_edge_detect
    _threads[pin].cancel()
  File "/usr/local/lib/python2.7/dist-packages/OPi/event.py", line 45, in cancel
    self.join()
  File "/usr/local/lib/python2.7/dist-packages/OPi/event.py", line 81, in join
    raise e
IOError: [Errno 13] Permission denied: '/sys/class/gpio/gpio67/edge'

gpio readall output:

$ gpio readall
 +------+-----+----------+------+---+  OPi H6  +---+------+----------+-----+------+
 | GPIO | wPi |   Name   | Mode | V | Physical | V | Mode | Name     | wPi | GPIO |
 +------+-----+----------+------+---+----++----+---+------+----------+-----+------+
 |      |     |     3.3V |      |   |  1 || 2  |   |      | 5V       |     |      |
 |  230 |   0 |    SDA.1 | ALT4 | 0 |  3 || 4  |   |      | 5V       |     |      |
 |  229 |   1 |    SCL.1 | ALT4 | 0 |  5 || 6  |   |      | GND      |     |      |
 |  228 |   2 |     PWM1 |  OFF | 0 |  7 || 8  | 0 | OFF  | PD21     | 3   | 117  |
 |      |     |      GND |      |   |  9 || 10 | 0 | OFF  | PD22     | 4   | 118  |
 |  120 |   5 |    RXD.3 | ALT2 | 0 | 11 || 12 | 0 | OFF  | PC09     | 6   | 73   |
 |  119 |   7 |    TXD.3 | ALT2 | 0 | 13 || 14 |   |      | GND      |     |      |
 |  122 |   8 |    CTS.3 |  OUT | 0 | 15 || 16 | 0 | IN   | PC08     | 9   | 72   |
 |      |     |     3.3V |      |   | 17 || 18 | 0 | OFF  | PC07     | 10  | 71   |
 |   66 |  11 |   MOSI.0 |  OFF | 0 | 19 || 20 |   |      | GND      |     |      |
 |   67 |  12 |   MISO.0 |   IN | 1 | 21 || 22 | 0 | ALT2 | RTS.3    | 13  | 121  |
 |   64 |  14 |   SCLK.0 |  OUT | 0 | 23 || 24 | 0 | OFF  | CE.0     | 15  | 69   |
 |      |     |      GND |      |   | 25 || 26 | 0 | OFF  | PH03     | 16  | 227  |
 +------+-----+----------+------+---+----++----+---+------+----------+-----+------+
 | GPIO | wPi |   Name   | Mode | V | Physical | V | Mode | Name     | wPi | GPIO |
 +------+-----+----------+------+---+  OPi H6  +---+------+----------+-----+------+

what i'm doing wrong?

Actually I have success with inputs polling method and controlling outputs.

Thank you in advance!!!

Erratic add_event_detect readings

Hi all,

So i've got a OrangePi Zero Plus2 H5 board and am trying to get 4 buttons to work. The buttons have four pins each, pushing the button shorts the bottom two pins, where i've connected the cable that goes to the board. (physical pin 15). Also i've connected a 10k resistor between the 3.3v power pin and the top two pins of the button.

In the following code i'm just trying to get one button to work


import orangepi.zeroplus2
from OPi import GPIO
from time import sleep

GPIO.setmode(orangepi.zeroplus2.BOARD)

orangePin = 15

GPIO.setwarnings(False)
GPIO.setup(orangePin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def event_callback(channel):
    print(f'Event detected: {GPIO.input(orangePin)}')

try:
    GPIO.add_event_detect(orangePin, GPIO.BOTH, callback=event_callback, bouncetime=50)

    while True:
        sleep(1)

except KeyboardInterrupt:  
    print('Done')
    GPIO.cleanup()

The readings are not consistent. But when i press the button the event_callback function is not triggered. (when released i get this behavior)

Event detected: 0
Event detected: 1
Event detected: 0
Event detected: 1
Event detected: 0
Event detected: 1
Event detected: 0
Event detected: 1
Event detected: 0

Also tried to increase the bounce time (just to test out). a couple of other types of resistors and a few other pins on the board but the problem unfortunately remains

Hoping that someone can give me any pointers on how to resolve this.

Thanks !

Does not work with fissio.py

Hello,
I am currently trying to port a python script from Raspberry to Orangepi 3 lts. Not a python coder or a coder at all. Some scripting I can do but this is too difficult for me.
This is the script I have been attempting to port to OPi.GPIO https://fissio.fi/fissio.py

Any help would be appreciated.
Timo

Rising Edge Detection innacurate

Hello,

I wanted to use a RPi script to use a rotary dialer, https://github.com/Szpeja/RotaryPi/blob/master/modules/RotaryDial.py

But it seem that rising edge detection is somewhat inaccurate, sometime detecting one less change in input than it should.

While I don't know much about Python at all, I tried a simple script to actively monitor the "button"

import OPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(2, GPIO.IN)

while True:
    input_state = GPIO.input(2)
    if input_state == True:
        print('Button Pressed')
        time.sleep(0.01)
    else:
        print('nope')
        time.sleep(0.01)

I've ran both at the same time, and while the output of that small script was what I expected (10 "Button Pressed" for 10 impulsion)

...
nope
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
nope
nope
nope
nope
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
nope
nope
nope
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
nope
nope
nope
nope
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
nope
nope
nope
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
nope
nope
nope
nope
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
nope
nope
nope
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
nope
nope
nope
nope
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
nope
nope
nope
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
nope
nope
nope
nope
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
Button Pressed
nope
nope
nope
nope

The output of the one depending on rising edge detection only detected 9 changes

[INPUT] 1 (2)
[INPUT] 0 (2)
[INPUT] 1 (2)
[INPUT] 0 (2)
[INPUT] 1 (2)
[INPUT] 1 (2)
[INPUT] 0 (2)
[INPUT] 1 (2)
[INPUT] 0 (2)
[INPUT] 1 (2)
[INPUT] 0 (2)
[INPUT] 1 (2)
[INPUT] 0 (2)
[INPUT] 1 (2)
[INPUT] 0 (2)
[INPUT] 1 (2)
[INPUT] 0 (2)
[INPUT] 1 (2)
[INPUT] 0 (2)
[DIGIT] Got digit: 9
[NUMBER] We have: 9

You can especially see at the line 5 and 6 that it didn't detect one of the change.

It get it right sometime, but not always, and it doesn't seem to be an hardware issue as monitoring the pin manually give me the correct number of value change, I don't know if it's fixable but the problem definitively seem to be around rising edge detection.

Device or resource busy when cleanup() is not used

Hi, really an excellent job @rm-hull first of all!

Not sure if it is expected but when I configure a pin as output and forget to use GPIO.cleanup() at the end of my script, it is no more possible to change the value of the output. E.g. if I run:

import OPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
pin = 11
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH)

And then:

import OPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
pin = 11
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.LOW)

I get the following error:

Traceback (most recent call last):
  File "gpio.py", line 4, in <module>
    GPIO.setup(pin, GPIO.OUT)
  File "/usr/local/lib/python2.7/dist-packages/OPi/GPIO.py", line 241, in setup
    sysfs.export(pin)
  File "/usr/local/lib/python2.7/dist-packages/OPi/sysfs.py", line 20, in export
    fp.write(str(pin))
IOError: [Errno 16] Device or resource busy

I guess because GPIO.setup tries to export the pin again causing the error and preventing the program to continue running. Not using setup() or catching the error results in a Channel is not configured message.
Thanks

usermod: group 'gpio' does not exist

Hi,

I just installed OPi.GPIO on my OrangePi Zero running Armbian buster with Linux 5.4.31-sunxi.
I followed the documentation but got the following error:

sudo pip3 install --upgrade OPi.GPIO
Collecting OPi.GPIO
  Downloading https://files.pythonhosted.org/packages/32/88/be49fa348875993d033cd4234ff4f27fab97265fe3e2687de3009816190d/OPi.GPIO-0.4.0-py2.py3-none-any.whl
Installing collected packages: OPi.GPIO
Successfully installed OPi.GPIO-0.4.0
sudo usermod -aG gpio poddingue
usermod: group 'gpio' does not exist

Do I have to create the group gpio by myself, or should the pip3 install do it for me?
Thanks.

Pin mapping feature request

I like the idea of adding SBC based mappings, but I would also like the BCM mappings. What I started with was adding dicts BOARD_DUO and BCM_DUO dicts. What do you think? Also, does it make sense to call the project OPi when it's really sysfs.GPIO? Let me know if you like the dicts idea and I'll do a PR for Duo. There's also the Duo Mini Shield, so I'm going to see if I need another set of dicts or if I can just use the Duo mappings.

Add support for Orange Pi Zero 3

The Orange Pi Zero 3 uses an Allwinner H618 processor which appears to be very similar to the H616 used in the Zero 2. In fact, the pin mappings may even be identical. Here's a link to the Zero 3 schematic: OrangePi-Schematic of ZERO3.pdf

As soon as I get my Zero 3 in the mail, I'll see if I can figure out the physical to GPIO pin mappings and create a pull request. In the meantime, I'd love to hear if anyone has experimented with it yet!

usermod: group 'gpio' does not exist

Hardware: OrangePi PC (H3)
OS: Armbian 22.05.3 Jammy with Linux 5.15.48-sunxi
Issue: running sudo usermod -aG gpio produces error: usermod: group "gpio" "does not exist"

I created the group manually (per #47), and continued to follow the instructions but am still not able to access GPIO with non-root user (Permission denied '/sys/class/gpio/gpio7/direction').

Can it be there is a comma error in the instructions?
The instruction use "Right Double Quotation Mark", "Left Double Quotation Mark" and "Left Single Quotation Mark" instead of the more common "Quotation Mark" and "Apostrophe"
I replaced the above with the common chars, but am still not able to obtain non-root access:
SUBSYSTEM=="gpio", KERNEL=="gpiochip*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:gpio /sys/class/gpio/export /sys/class/gpio/unexport ; chmod 220 /sys/class/gpio/export /sys/class/gpio/unexport'" SUBSYSTEM=="gpio", KERNEL=="gpio*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:gpio /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value ; chmod 660 /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value'"

`ModuleNotFoundError: No module named 'OPi'` even though installed [ OrangePi Zero 2W]

Device

  • OrangePi Zero 2W
  • Debian image 6.1 kernel

Problem/Notes

  • OPi.GPIO is installed, but import doesn't work.
  • Everything is correct physically because wiringpi code works.
  • What am I doing wrong here?
  • Do I need Python 3.4.0?
  • Thanks!

Steps I took

mkdir blink-opi && cd blink-opi
python -m venv .venv
pyenv local 3.10.0 # this version is installed via pyenv on system
pip install OPi.GPIO
# create blink-opi.py
sudo python blink-opi.py 
Traceback (most recent call last):
  File "/home/opi/code/blink-opi/blink-opi.py", line 3, in <module>
    import OPi.GPIO as GPIO
ModuleNotFoundError: No module named 'OPi'

OPi.GPIO blink-opi.py

❌ Code at issue

#!/usr/bin/env python
# import RPi.GPIO as GPIO
import OPi.GPIO as GPIO
from time import sleep

ledPin = 8
GPIO.setmode(GPIO.BCM)  # choose BCM or BOARD
GPIO.setup(ledPin, GPIO.OUT)

try:
    while True:
        GPIO.output(ledPin, 1)
        sleep(0.5)
        GPIO.output(ledPin, 0)
        sleep(0.5)

except KeyboardInterrupt:
    GPIO.cleanup()  # resets all GPIO ports used by this program

wiringpi blink-wiringpi.py

✅ This works

import wiringpi
from time import sleep
import sys
from wiringpi import GPIO

wiringpi.wiringPiSetup()
NUM = wiringpi.getGpioNum()
ledPin = 3 # Physical 8
wiringpi.pinMode(ledPin, GPIO.OUTPUT)

try:
    while True:
        wiringpi.digitalWrite(ledPin, GPIO.HIGH)
        sleep(0.5)
        wiringpi.digitalWrite(ledPin, GPIO.LOW)
        sleep(0.5)

except KeyboardInterrupt:
    print("\nexit")
    sys.exit(0)

PermissionError /sys/class/gpio/export NanoPi Neo Armbian 23 6.1.63

Hey wonderful people,

Having issues getting gpio working from my pygame script..
as per title keep getting PermissionError: Permission Denied /sys/class/gpio/export

running as sudo doesnt fix the issue as im running a virtual environment due to "Environment maintened externally" error when trying to install pip packages as sudo.. (some packages are installable with the --break-system-packages line but that doesnt fix the issue here..)

any help would be greatly appreciated!

BCM pinout calculation

Hello,
please can explain how you calculate BCM pinout mapping? I'm searching for it on the web all the day but found nothing about the link of physical with BCM pins.
(i'm trying to add pin mapping for NanoPi Neo board)

Thanks

wait_for_edge not working on OPi lite

import orangepi.lite
import OPi.GPIO as GPIO
GPIO.setmode(orangepi.lite.BOARD)
GPIO.setup(16, GPIO.IN)
GPIO.wait_for_edge(16, GPIO.RISING)

gives

[Errno 2] No such file or directory: '/sys/class/gpio/gpio68/edge'

NanoPi M4v2 + OPi.GPIO + Luma.OLED + SSD1351 Error

Hello,

I'm sure this library works just fine but I'm missing some piece here. I'm trying to drive an SSD1351 LCD with OPi.GPIO + Luma.OLED however I'm not having much luck. Here is what I did:

  1. Install Luma.OLED:
apt-get update
apt-get install python3 python3-pip python3-pil libjpeg-dev zlib1g-dev libfreetype6-dev liblcms2-dev libopenjp2-7 libtiff5 -y
pip3 install luma.oled
  1. Install OPi.GPIO:
pip3 install --upgrade OPi.GPIO
  1. Wire my LCD as:
MOSI > GPIO1_A7 > SPI1_RXD > M4 Pin 21   > LCD DIN Pin (blue)
SCK > GPIO1_B1 > SPI1_CLK > M4 Pin 23     > LCD CLK Pin (yellow)
CS > GPIO1_B2  > SPI1_CSn0 > M4 Pin 24    > LCD CS Pin (orange)

RST > GPIO1_A1(3V) > M4 Pin 11    >   LCD RST Pin (white) 
DC > GPIO1_A3(3V)  > M4 Pin 13        >   LCD DC Pin (green)
  1. Find the spidev and run a Luma.OLED example as:
ls /dev/spi*
/dev/spidev1.0

python3 luma.examples/examples/3d_box.py 
--display=ssd1351 
--interface=spi 
--width=128 --height=128 
--spi-bus-speed=16000000 
--bgr 
--spi-device=1 --spi-port=0 
--gpio-reset=35
--gpio-data-command=33 
--gpio=OPi.GPIO
--gpio-mode=nanopi.m4.BOARD

From what I understand of https://github.com/rm-hull/OPi.GPIO/blob/master/nanopi/m4.py my physical pin 11 maps to GPIO 33 and physical pin 13 to GPIO 35. Is this even correct?

Now it fails with the following output:

Traceback (most recent call last):
  File "luma.examples/examples/3d_box.py", line 129, in <module>
    device = get_device()
  File "/root/luma.examples/examples/demo_opts.py", line 61, in get_device
    device = cmdline.create_device(args)
  File "/usr/local/lib/python3.7/dist-packages/luma/core/cmdline.py", line 246, in create_device
    device = Device(serial_interface=interface(), **params)
  File "/usr/local/lib/python3.7/dist-packages/luma/core/cmdline.py", line 161, in spi
    gpio=self.gpio or GPIO)
  File "/usr/local/lib/python3.7/dist-packages/luma/core/interface/serial.py", line 299, in __init__
    bitbang.__init__(self, gpio, transfer_size, reset_hold_time, reset_release_time, DC=gpio_DC, RST=gpio_RST)
  File "/usr/local/lib/python3.7/dist-packages/luma/core/interface/serial.py", line 187, in __init__
    self._DC = self._configure(kwargs.get("DC"))
  File "/usr/local/lib/python3.7/dist-packages/luma/core/interface/serial.py", line 200, in _configure
    self._gpio.setup(pin, self._gpio.OUT)
  File "/usr/local/lib/python3.7/dist-packages/OPi/GPIO.py", line 470, in setup
    pin = get_gpio_pin(_mode, channel)
  File "/usr/local/lib/python3.7/dist-packages/OPi/pin_mappings.py", line 80, in get_gpio_pin
    return _pin_map[mode][channel]
KeyError: 33

SBC: NanoPi M4V2
System: Armbian 5.10.35-rockchip64 #21.05.1 SMP PREEMPT Fri May 7 13:53:11 UTC 2021 aarch64 GNU/Linux
Display: https://www.ebay.com/itm/203101646517
Datasheet: https://www.waveshare.com/w/upload/5/5b/1.5inch_RGB_OLED_Module_User_Manual_EN.pdf

Thank you.

Question: Rock 4?

Just wondering if this library will work out of the box for a Rock 4. I see the directory for the Rock pi S and Zero..but not Rock 4.

I'd be happy to help in any way I can to implement. Have been looking at pin mappings!

Support for Pull up / Pull down resistors

It would be great (if not too complex) to add support for the RPi.GPIO pull_up_down argument of setup() since it is very common to have pull up/down set when reading an input.
I've tried the trick to output e.g. HIGH before reading but I correctly got an error saying the pin is supposed to be an input not an output.
Alternatively, I'd recommend adding the pull_up_down option anyway and just print out a warning saying it is not implemented so to ensure compatibility with existing code. A similar approach can be used for bouncetime and other optional parameters in this way the code will be fully compatible even if the functionality is not implemented.

Thanks

about setmode for orangepi.pc.BCM

I tried both BOARD and BCM using OrangePi-PC.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import orangepi.pc
import OPi.GPIO as GPIO
from time import sleep            # this lets us have a time delay

pin=26
GPIO.setmode(orangepi.pc.BOARD)   # set up BOARD numbering
GPIO.setup(pin, GPIO.OUT)         # set pin 26 as an output (LED)

try:
    print ("Press CTRL+C to exit")
    while True:
        GPIO.output(pin, 1)       # set port/pin value to 1/HIGH/True
        sleep(0.1)
        GPIO.output(pin, 0)       # set port/pin value to 0/LOW/False
        sleep(0.1)

        GPIO.output(pin, 1)       # set port/pin value to 1/HIGH/True
        sleep(0.1)
        GPIO.output(pin, 0)       # set port/pin value to 0/LOW/False
        sleep(0.1)

        sleep(0.5)

except KeyboardInterrupt:
    GPIO.output(pin, 0)           # set port/pin value to 0/LOW/False
    GPIO.cleanup()               # Clean GPIO
    print ("Bye.")

It work fine.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import orangepi.pc
import OPi.GPIO as GPIO
from time import sleep          # this lets us have a time delay

pin=7
GPIO.setmode(orangepi.pc.BCM)   # set up BCM numbering
GPIO.setup(pin, GPIO.OUT)         # set BCM7 (pin 26) as an output (LED)

try:
    print ("Press CTRL+C to exit")
    while True:
        GPIO.output(pin, 1)       # set port/pin value to 1/HIGH/True
        sleep(0.1)
        GPIO.output(pin, 0)       # set port/pin value to 0/LOW/False
        sleep(0.1)

        GPIO.output(pin, 1)       # set port/pin value to 1/HIGH/True
        sleep(0.1)
        GPIO.output(pin, 0)       # set port/pin value to 0/LOW/False
        sleep(0.1)

        sleep(0.5)

except KeyboardInterrupt:
    GPIO.output(pin, 0)           # set port/pin value to 0/LOW/False
    GPIO.cleanup()              # Clean GPIO
    print ("Bye.")

It don't work.

I changed orangepi/pc.py as follows

# NanoPi Duo BCM pin to actual GPIO pin
BCM = {
    2: 12,    # I2C0_SDA/PA12 (TWI0_SDA/DI_RX/PA_EINT12)
    3: 11,    # I2C0_SCL/PA11 (TWI0_SCK/DI_TX/PA_EINT11)
    4: 6,     # PA6 (SIM_PWREN/PWM1/PA_EINT6)
    14: 13,   # PA13 (SPI1_CS/UART3_TX/PA_EINT13)
    15: 14,   # PA14 (SPI1_CLK/UART3_RX/PA_EINT14)
    17: 1,    # PA1 (UART2_RX/JTAG_CK/PA_EINT1)
    18: 110,  # PD14
    27: 0,    # PA0 (UART2_TX/JTAG_MS/PA_EINT0)
    22: 3,    # PA3 (UART2_CTS/JTAG_DI/PA_EINT3)
    23: 68,   # PC4
    24: 71,   # PC7
    10: 64,   # PC0 (SPI0_MOSI)
    9: 65,    # PC1 (SPI0_MISO)
    25: 2,    # PA2 (UART2_RTS/JTAG_DO/PA_EINT2)
    11: 66,   # PC2 (SPI0_CLK)
    8: 67,    # PC3 (SPI0_CS)
    7: 21,    # PA21 (PCM0_DIN/SIM_VPPPP/PA_EINT21)
    0: 19,    # PA19 (PCM0_CLK/TWI1_SDA/PA_EINT19)
    1: 18,    # PA18 (PCM0_SYNC/TWI1_SCK/PA_EINT18)
    5: 7,     # PA7 (SIM_CLK/PA_EINT7)
    6: 8,     # PA8 (SIM_DATA/PA_EINT8)
    12: 200,  # PG8 (UART1_RTS/PG_EINT8)
    13: 9,    # PA9 (SIM_RST/PA_EINT9)
    19: 10,   # PA10 (SIM_DET/PA_EINT10)
    16: 201,  # PG9 (UART1_CTS/PG_EINT9)
    26: 20,   # PA20 (PCM0_DOUT/SIM_VPPEN/PA_EINT20)
    20: 198,  # PG6 (UART1_TX/PG_EINT6)
    21: 199,  # PG7 (UART1_RX/PG_EINT7)
}

Orange pi 5, OPi.GPIO.output() does change the value inside ".../gpio2/value" file but it doesn't change the physical pin condition

Hi everyone,

I am using "Ubuntu image" to lunch my board. when I use "gpio readall" command, it shows me all pins info well. I want to use physical pin 7 (GPIO 2) to control an LED on/off as a test. Controlling the pin with bash is ok, and all these command like "gpio mode 2 out", "gpio write 2 0" works fine and could control LED as well.
--- So, the pin is OK and can control LED with bash command ---

The problem rises when I am using "OPi.GPIO" to control LED condition. I change the "pin_mapping" file to comply "Orange pi 5" board's pin_mapping. Now, everything also goes well and when I run my python code it change the value inside "/sys/class/gpio2/value" file from 0 to 1 and vice versa as it was demanded.
--- So, the code is also OK ---

But, It has no effect on the LED condition!
I mean it can change the value inside "value" file as programmed in "GPIO.py and sysfy.py" files, but has no effect on pin.

Thank anybody in advance if has any idea!? Do I miss something here?

python threading library is actually not running thread in parallel

Hello,

the original RPi.GPIO is written in C because in python is not possible to have real multithreading
behaviour. When you decided to port the original library and switch to python only, has you
considered this issue?

https://realpython.com/python-gil

Please note that I'm just curious, I didn't find any mention to that into provided documentation. I've started to adjust the original RPi.GPIO to work with other boards when I discovered your project.

Thanks,

OPi.GPIO and libgpiod instead /sysfs

OPi.GPIO is a very good support for writing python applications to interact with GPIO on SBC based on H3/H5 etc. Currently, the latest OS distributions based on Debian 12 and higher using kernel 6.x do not have access to GPIO via /sysfs, only via libgpiod. Does OPi.GPIO support interaction with GPIO via libgpiod?

I found info https://linux-sunxi.org/GPIO
Accessing the GPIO pins through character device with mainline kernel
The sysfs GPIO interface is now deprecated in favor of character devices /dev/gpiochipX

I found a project called RPI.GPIO2

https://github.com/underground-software/RPi.GPIO2/tree/master?tab=readme-ov-file

which allows you to maintain the communication syntax with GPIO but by using libgpiod.
Maybe it would be possible to make OPi.GPIO2 versions in a similar way, so that we could still use our python scripts on 6.x series krenels and use the settings and communication with GPIO via libgpiod?

Fixed Orange Pi PC pin mapping is not in PyPi

Hello,

The newest Orange Pi PC pin mapping is not in PyPi. Since I'm using the Orange Pi One, this means that my pin mapping does not match with the correct pin mapping.

Would it be possible to tag and release the newest pin mapping to PyPi so somebody else doesn't have the same issue, haha...

orangepi4 error ?

writing byte-compilation script '/var/tmp/portage/dev-python/OPi-GPIO-0.5.0/temp/tmpdqhcd8x1.py'
/usr/bin/python3.8 /var/tmp/portage/dev-python/OPi-GPIO-0.5.0/temp/tmpdqhcd8x1.py
  File "/usr/lib/python3.8/site-packages/orangepi/pi4B.py", line 21
    import orangepi.4
                             ^
SyntaxError: invalid syntax

now i wonder why it faults there ?

KeyError: 25 when using radxa zero

Hi!

I try to use the OPi.GPIO with radxa zero pinout. But i get a "KeyError: 25" in the pin_mappings.py, line 80, in get_gpio_pi return _pin_map[mode][channel]

I'm not that experienced, so i might be doing something very wrong or missing out.
Thank you for your help!

Intermittent test failures

  • test_add_edge_callback(): E assert 71 in {}
  • test_edge_detected():
E   assert False
E    +    where False = <function edge_detected at 0x7f4cfb0b5aa0>(23)
E    +    where <function edge_detected at 0x7f4cfb0b5aa0> = event.edge_detected
  • test_callback_raises_error(): E Failed: DID NOT RAISE <type 'exceptions.RuntimeError'>

Is OPi Lite 2 being support yet?

Hi, I'm working on OPi Lite 2 board and came across this page for porting RPi.GPIO lib into OPi board. Is anyone working on getting Lite 2 board supported by this drop-in lib yet? If yes, I might want to give it a try.
thanks,!

IOError: [Errno 13] Permission denied: '/sys/class/gpio/gpio298/direction'

Ubuntu 16.04
Jetson TX2 board (aarch64, ARMv8)

Hello all.
Thanks to LUMA libs, my robot has eyes.

I need to run my python without root privileges, and it produces an error !

python /home/nvidia/catkin_ws/src/alfred_eyes/src/RandomOledEyes.py
Traceback (most recent call last):
  File "/home/nvidia/catkin_ws/src/alfred_eyes/src/RandomOledEyes.py", line 21, in <module>
    serial = spi(port=3, device=0, bus_speed_hz=8000000, transfer_size=2048, gpio=GPIO, gpio_DC=1, gpio_RST=2)
  File "/usr/local/lib/python2.7/dist-packages/luma/core/interface/serial.py", line 253, in __init__
    bitbang.__init__(self, gpio, transfer_size, DC=gpio_DC, RST=gpio_RST)
  File "/usr/local/lib/python2.7/dist-packages/luma/core/interface/serial.py", line 153, in __init__
    self._DC = self._configure(kwargs.get("DC"))
  File "/usr/local/lib/python2.7/dist-packages/luma/core/interface/serial.py", line 164, in _configure
    self._gpio.setup(pin, self._gpio.OUT)
  File "/usr/local/lib/python2.7/dist-packages/OPi/GPIO.py", line 366, in setup
    sysfs.direction(pin, direction)
  File "/usr/local/lib/python2.7/dist-packages/OPi/sysfs.py", line 32, in direction
    with open(path, "w") as fp:

IOError: [Errno 13] Permission denied: '/sys/class/gpio/gpio298/direction'

Does anyone has idea to correct it ? Anything like udev ?
Thanks, Vincent.

IOError when trying to setup PA10 pin

Hi,

I am trying to setup the following PA10 pin as input in the "BOARD" mode.

Other references to this pin:
Orange Pi = SIM_DET/PA_EINT10/PA10
Physical Pin = 26

My code:

import OPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(26, GPIO.IN)

Error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/OPi/GPIO.py", line 356, in setup
    sysfs.unexport(pin)
  File "/usr/local/lib/python2.7/dist-packages/OPi/sysfs.py", line 26, in unexport
    fp.write(str(pin))
IOError: [Errno 22] Invalid argument

I don't get this error in any other pins when I try to set them as input. Am I missing something?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.