spechter
spechter

Reputation: 13

How to use the Adafruit_BBIO module with python3?

I'm trying to use the GPIO pins on a Beaglebone Black. I've used the Adafruit_BBIO.GPIO module when trying some code examples from a book. Since then I coded my whole logic in python3, which can't access the module. I would like to ask if someone knows how to access the module with python3. I already tried to copy the Adafruit_BBIO directory into my python3 folder but it didn't work.

'#!/usr/bin/python3
import Adafruit_BBIO'

When I call the mainscript like

root@arm: python3 main.py

I get this error:

_Traceback (most recent call last): File "main.py", line 8, in <module> import lcd_control File "/home/debian/python/lcd_control.py", line 3, in <module> import Adafuit_BBIO.GPIO as GPIO ImportError: No module named 'Adafuit_BBIO'_ 

Upvotes: 1

Views: 3936

Answers (3)

Ogre Psalm33
Ogre Psalm33

Reputation: 21946

Just to bring this question up-to-date, the following now works to install Adafruit_GPIO for python3 in Debian "buster":

pip3 install Adafruit-GPIO

Looking at the project history, they made updates to be compatible with Python 3 in April 2019.

Upvotes: 0

Chris Porter
Chris Porter

Reputation: 11

Awesome! thanks Joran, this has been a huge help. Your code didn't work for me at first, I had to make a few tweaks: this is what I use now and import into other simple programs to use GPIO:

  import os 
  class SimpleGPIO:
      def __init__(self,gpio_pin):
          self.gpio_pin = gpio_pin
          os.system("echo %d > /sys/class/gpio/export" % self.gpio_pin)
          self.gpio_path = "/sys/class/gpio/gpio%d/"%gpio_pin
          with open(self.gpio_path+"direction") as f:
              self.direction = f.read()
      def write(self,value):
          if self.direction != "out":
              os.system("echo out > %sdirection"%self.gpio_path)
              self.direction = "out"
          os.system("echo %s > %svalue"%(value,self.gpio_path))
      def read(self):
          if self.direction != "in":
              os.system("echo in > %sdirection"%self.gpio_path)
              self.direction = "in"
          with open(self.gpio_path+value) as f:
              return f.read()

now, I just reuse the code by adding from gpio import SimpleGPIO at the beginning.

I found when i pasted it into this post, it seemed to remove the same line that was missing from yours, import os and os.system("echo %d > /sys/class/gpio/export..... I'm guessing it did the same when you posted which is why it didn't work straight away for me.

Thanks a bunch! I really didn't like having to use the Adafruit module since it didn't work for python3 directly plus i learned a ton about how to use the GPIO just by working through this and getting the code to work.

Upvotes: 1

Joran Beasley
Joran Beasley

Reputation: 113988

you should be able to figure out your gpio by going to

$ ls /sys/class/gpio/gpio100 ;or whatever your gpiopin is

then in python

class SimpleGPIO:
     def __init__(self,gpio_pin):
        self.gpio_pin = gpio_pin
        self.gpio_path = "/sys/class/gpio/gpio%d/"%gpio_pin
        with open(self.gpio_path+"direction") as f:
        self.direction = f.read()
     def write(self,value):
        if self.direction != "out":
           os.system("echo out > %sdirection"%self.gpio_path)
           self.direction = "out"
        os.system("echo %s > %svalue"%(value,self.gpio_path)
     def read(self):
        if self.direction != "in":
           os.system("echo in > %sdirection"%self.gpio_path)
           self.direction = "in"
        with open(self.gpio_path+value) as f:
           return f.read()


 gpio_100 = SimpleGPIO(100) #open on 100
 gpio_100.write(1)

Upvotes: 1

Related Questions