adafruit / Adafruit_CircuitPython_ServoKit

CircuitPython helper library for the PWM/Servo FeatherWing, Shield and Pi HAT kits.
MIT License
69 stars 29 forks source link

AttributeError: module 'board' has no attribute 'I2C' #7

Closed sumghai closed 5 years ago

sumghai commented 5 years ago

Summary of Issue

Attempting to initialize the class object from ServoKit results in the following error:

Traceback (most recent call last):
  File "servo_test.py", line 3, in <module>
    kit = ServoKit(channels=16)
  File "/usr/local/lib/python3.5/dist-packages/adafruit_servokit.py", line 85, in __init__
    i2c = board.I2C()
AttributeError: module 'board' has no attribute 'I2C'

Hardware

Software and Environment

servo_test.py

import time
from adafruit_servokit import ServoKit
kit = ServoKit(channels=16)

# Tuned for GWS S03T STD Servos
kit.servo[0].set_pulse_width_range(775, 2450)
kit.servo[1].set_pulse_width_range(775, 2450)

# Return servos to starting positions
kit.servo[0].angle = 0
kit.servo[1].angle = 180
time.sleep(1)

while True:
    for tgtAngle in range(0, 180, 1):
        kit.servo[0].angle = tgtAngle
        kit.servo[1].angle = 180 - tgtAngle
        time.sleep(0.1)
    for tgtAngle in range(180, 0, -1):
        kit.servo[0].angle = tgtAngle
        kit.servo[1].angle = 180 - tgtAngle
        time.sleep(0.1)

Notes

abhiTronix commented 5 years ago

@sumghai Bug confirmed. Tested on my Raspberry Pi 3 Model B.

There is no documentation/comment anywhere related to the new i2c parameter introduced in the latest commit #6, like what should be its value? @kattni can you please help?

kattni commented 5 years ago

@sumghai @abhiTronix We were fixing a different I2C bug and introduced this one. This is fixed by https://github.com/adafruit/Adafruit_Blinka/pull/95. Please update Adafruit_Blinka to resolve the issue. I've done a new release - give it a few minutes to deploy to PyPI before trying to update.

To answer your question, @abhiTronix, to pass in different pins, you would instantiate the library as follows:

import busio
from adafruit_servokit import ServoKit
kit = ServoKit(channels=16, i2c=(busio.I2C(board.SCL, board.SDA)))

I have only tested that on a microcontroller, not on RasPi, but that is the idea of how to pass in different I2C pins.

sumghai commented 5 years ago

I can confirm that updating Adafruit Blinka to 1.2.7 resolves this issue on the RasPi.

Thanks @kattni!

hamwong commented 4 years ago

HI,

I am trying to use below code on python3 and have same error

    import busio
    import adafruit_tsl2561
    i2c = busio.I2C(board.SCL, board.SDA)
    sensor = adafruit_tsl2561.TSL2561(i2c)

I am using Rpi 3B+ with Adafruit-Blinka 3.3.4

Name: Adafruit-Blinka
Version: 3.3.4
Summary: CircuitPython APIs for non-CircuitPython versions of Python such as CPython on Linux and MicroPython.
Home-page: https://github.com/adafruit/Adafruit_Blinka
Author: Adafruit Industries
Author-email: circuitpython@adafruit.com
License: MIT
Location: /usr/local/lib/python3.5/dist-packages
Requires: Adafruit-PlatformDetect, rpi-ws281x, spidev, RPi.GPIO, sysv-ipc, pyftdi, Adafruit-PureIO

I got below error

  File "./TSL2561c.py", line 12, in <module>
    i2c = busio.I2C(board.SCL, board.SDA)
AttributeError: module 'board' has no attribute 'SCL'
caternuson commented 4 years ago

You aren't importing board? But somehow it must be or the error would be different. What does this show?

import board
dir(board)
hamwong commented 4 years ago

You aren't importing board? But somehow it must be or the error would be different. What does this show?

import board
dir(board)

Sorry, I didn't double check the code I post, I have , it's at first line and I miss to copy it and paste here. but I am 100% sure import board is there, I add your line and it didn't show alarm but I found further when print out, it looks my board have some problem

import time
import board
dir(board)
import busio
import adafruit_tsl2561
print("board contents: ", dir(board))
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_tsl2561.TSL2561(i2c)

python3 TSL2561c.py
board contents:  ['BaseDimension', 'Board', 'Dimension', 'Empty', 'Image', 'InfiniteDimension', 'Infinity', '_Empty', '_InfiniteDimension', '_Infinity', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_centred_coord', 'cornerposts', 'functools', 'imagefile_sprite', 'io', 'itertools', 'long', 'os', 'sys', 'text_sprite']
Traceback (most recent call last):
  File "TSL2561c.py", line 15, in <module>
    i2c = busio.I2C(board.SCL, board.SDA)
AttributeError: module 'board' has no attribute 'SCL'
I found other people should come out like this, what's wrong with my board
board contents: ['CE0', 'CE0_1', 'CE1', 'CE1_1', 'D10', 'D11', 'D12', 'D13', 'D16', 'D17', 'D18', 'D19', 'D20', 'D21', 'D22', 'D23', 'D24', 'D25', 'D26', 'D27', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'I2C', 'MISO', 'MISO_1', 'MOSI', 'MOSI_1', 'SCK', 'SCK_1', 'SCL', 'SCLK', 'SCLK_1', 'SCL_1', 'SDA', 'SDA_1', 'SPI', 'builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'spec', 'ap_board', 'board_id', 'detector', 'pin', 'sys']

pip3 list
Adafruit-Blinka (3.3.4)
adafruit-circuitpython-busdevice (4.1.0)
adafruit-circuitpython-lis3dh (5.0.1)
adafruit-circuitpython-tsl2561 (3.2.2)
Adafruit-PlatformDetect (1.3.8)
Adafruit-PureIO (1.0.4)
board (1.0)
ladyada commented 4 years ago

looks like you have some other library that has created a board.py in your python collection, & you'll have to remove it

caternuson commented 4 years ago

Yep. You somehow have this installed: https://pypi.org/project/board/ instead of the board that comes with Blinka.

You can not install the board module individually. You need to do a complete Blinka install.

hamwong commented 4 years ago

Thanks @caternuson and @ladyada after remove board and reinstall Blinka, work as expect.

Nathanaelhend commented 1 year ago

@hamwong solved?