May I R/W any I2C slave device by CircuitPython library?

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
PKuo
 
Posts: 2
Joined: Wed Nov 30, 2022 5:41 am

May I R/W any I2C slave device by CircuitPython library?

Post by PKuo »

Hi All,
I got a adafruit FT232H board.
I setuped environment and scaned I2C bus that can find my I2C device.
As screenshot I can find my slave device (addr : 0x0b), but I will got error while send data to it.
Image

Code: Select all

import time
import board    # need to set BLINKA_FT232H=1 from system environment variable...PK 20221125+

# To use default I2C bus (most boards)
i2c = board.I2C()

while not i2c.try_lock():
    pass

try:
    while True:
        print(
            "I2C addresses found:",
            [hex(device_address) for device_address in i2c.scan()],
        )
        # i2c.writeto(0x0B, bytes([0x44, 0x01,0x00]))
        i2c.writeto(0x0b, bytes([0x08]), stop=True)
        result = bytearray(2)
        i2c.readfrom_into(0x0b, result)
        # result        

        time.sleep(2)

finally:  # unlock the i2c bus when ctrl-c'ing out of the loop
    i2c.unlock()
May I R/W any I2C slave device by CircuitPython library?

Thanks for your suppot.

============================
Environment informaion :
- Windows : Windows 10 Enterprise
- Board : Adafruit FT232H
- Python : v3.10.8
============================

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: May I R/W any I2C slave device by CircuitPython library?

Post by adafruit_support_carter »

Approach seems OK. What is the specific device being talked to?

User avatar
PKuo
 
Posts: 2
Joined: Wed Nov 30, 2022 5:41 am

Re: May I R/W any I2C slave device by CircuitPython library?

Post by PKuo »

Hi~~
slave device is bq40z50 https://www.ti.com/lit/pdf/sluubc1
I follow the specification to get "DeviceChemistry" (reg : 0x22)
Every time I can scan the device address is 0x0B, but next I'll get "NACK from slave" issue while call "i2c.writeto()".

I use other I2C tooling board to send "addr:0x0B, reg:0x22" and get data that is work.
Could you give me some advice?

Attachment my source code.

Code: Select all

import time
import board    # need to set BLINKA_FT232H=1 from system environment variable...PK 20221125+

# To use default I2C bus (most boards)
i2c = board.I2C()

while not i2c.try_lock():
    pass

try:
    while True:
        result = bytearray(5)
        # write bytes of data from the board to the MCP9808 and
        # receive two bytes of temperature sensor register data
        i2c.writeto_then_readfrom(0x0B, bytes([0x22]), result)

        time.sleep(1.0)

finally:  # unlock the i2c bus when ctrl-c'ing out of the loop
    i2c.unlock()

My installed librayies.

Code: Select all

Package                          Version
-------------------------------- ---------
Adafruit-Blinka                  8.7.0    
adafruit-circuitpython-busdevice 5.2.3
adafruit-circuitpython-requests  1.12.11
adafruit-circuitpython-typing    1.8.2
Adafruit-PlatformDetect          3.35.0
Adafruit-PureIO                  1.1.9
altgraph                         0.17.2
async-generator                  1.10
attrs                            22.1.0
certifi                          2022.6.15
cffi                             1.15.1
charset-normalizer               2.1.1
colour                           0.1.5
crcmod                           1.7
cycler                           0.11.0
et-xmlfile                       1.1.0
fonttools                        4.37.1
future                           0.18.2
h11                              0.13.0
idna                             3.3
iso8601                          1.0.2
Jinja2                           3.1.2
jsonschema                       4.15.0
kiwisolver                       1.4.4
lxml                             4.9.1
MarkupSafe                       2.1.1
matplotlib                       3.5.3
numpy                            1.23.2
openpyxl                         3.0.10
outcome                          1.2.0
packaging                        21.3
pandas                           1.4.4
pefile                           2022.5.30
Pillow                           9.2.0
pip                              22.3.1
pycparser                        2.21
pyftdi                           0.54.0
pyinstaller                      5.3
pyinstaller-hooks-contrib        2022.10
pyparsing                        3.0.9
PyQt5                            5.15.7
PyQt5-Qt5                        5.15.2
PyQt5-sip                        12.11.0
pyrsistent                       0.18.1
pyserial                         3.5
PySocks                          1.7.1
python-dateutil                  2.8.2
pytz                             2022.2.1
pyuic5-tool                      0.0.1
pyusb                            1.0.2
pywin32-ctypes                   0.2.0
PyYAML                           6.0
requests                         2.28.1
seaborn                          0.12.0
selenium                         4.4.3
serial                           0.0.97
six                              1.16.0
sniffio                          1.3.0
sortedcontainers                 2.4.0
styleframe                       4.1
trio                             0.21.0
trio-websocket                   0.9.2
typing_extensions                4.4.0
urllib3                          1.26.12
wsproto                          1.2.0
XlsxWriter                       3.0.3


Finally, thanks for your support.

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: May I R/W any I2C slave device by CircuitPython library?

Post by adafruit_support_carter »

I use other I2C tooling board to send "addr:0x0B, reg:0x22" and get data that is work.
Is that the new code you've attached? This works?

Code: Select all

        i2c.writeto_then_readfrom(0x0B, bytes([0x22]), result)
If so, then it likely requires repeated starts.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”