difficulty using bitbangio with Pico and TCS34725, read/writ

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
gmcc111
 
Posts: 2
Joined: Sat Sep 11, 2021 10:56 am

difficulty using bitbangio with Pico and TCS34725, read/writ

Post by gmcc111 »

Hi All,

I've been working on a little fluorescence/light sensor project in my university's lab, testing the TCS34725 color sensor with the Arduino Uno and the Raspberry Pi Pico. I've got a decent background in programming (mainly Matlab and Python) but I'm fairly new to embedded coding and Micropython. While its generally been a lot of fun and the Uno hasn't had any difficulty, I've been running into a lot of issues trying to get the TCS34725 running on the Pico. I've been trying to troubleshoot this for a bit, so I've laid out what's turned up below in the hopes that someone can help me sort out how this is going wrong. Searching through the forums here indicated that the Pico/TCS34725 interface has had some issues but I got the impression they'd been resolved.

To start, I'm running MicroPython v1.17 on 2021-09-02; Raspberry Pi Pico with RP2040, and interfacing to the Pico using Thonny. I've saved the adafruit_blinka, adafuit_busdevice, adafruit_platform_detect and adafruit_tcs34725 libraries to the board. I tried using the basic example from the sensor's webpage: https://learn.adafruit.com/adafruit-col ... cuitpython

Code: Select all

import board
import adafruit_tcs34725
i2c = board.I2C()
sensor = adafruit_tcs34725.TCS34725(i2c)
This gave me the following error:

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
AttributeError: 'module' object has no attribute 'I2C'
Looking into it, it appears that the board module doesn't create an I2C ttribute when initializing because the adafruit_blinka.board.raspberrypi.pico library it imports doesn't set anything for SDA or SLC, so when board tries to check for them with locals(), it sees nothing. So I added two lines to set my SDA and SCL pins to GP0 and GP1 respectively in the board.raspberrypi.pico library. This let me initialize the sensor, but any attempt to use its basic functions gives me an error. For example, the following code:

Code: Select all

i2c = board.I2C()
sensor = adafruit_tcs34725.TCS34725(i2c)
print('Color: ({0}, {1}, {2})'.format(*sensor.color_rgb_bytes))
gives me the error "OSError: [Errno 5] EIO" with the following traceback:

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 30, in <module>
  File "/lib/adafruit_tcs34725.py", line 136, in color_rgb_bytes
  File "/lib/adafruit_tcs34725.py", line 249, in color_raw
  File "/lib/adafruit_tcs34725.py", line 181, in active
  File "/lib/adafruit_tcs34725.py", line 383, in _read_u8
  File "/lib/adafruit_bus_device/i2c_device.py", line 125, in write_then_readinto
  File "/lib/busio.py", line 176, in writeto_then_readfrom
  File "/lib/adafruit_blinka/microcontroller/rp2040/i2c.py", line 55, in writeto_then_readfrom
  File "/lib/adafruit_blinka/microcontroller/rp2040/i2c.py", line 33, in writeto
OSError: [Errno 5] EIO
The responsible writeto function code is:

Code: Select all

    def writeto(self, address, buffer, *, stop=True):
        "Write data to the address from the buffer"
        return self._i2c.writeto(address, buffer, stop)
I've tried a couple little workarounds based on what I've seen from other forum responses. Specifically those recommending using bitbangio for i2c creation: viewtopic.php?f=60&t=176259&p=858537&hi ... 25#p858537

Running this code:

Code: Select all

import bitbangio, board, adafruit_tcs34725
i2c = bitbangio.I2C(board.GP1, board.GP0)
sensor = adafruit_tcs34725.TCS34725(i2c)
print('Color: ({0}, {1}, {2})'.format(*sensor.color_rgb_bytes))
I run into errors in bitbangio:

Code: Select all

  File "<stdin>", line 2, in <module>
  File "/lib/bitbangio.py", line 28, in __init__
  File "/lib/bitbangio.py", line 40, in init
TypeError: can't convert Pin to int
More googling and I rewrite the initialization on line 40 of bitbangio to include scl and sda keywords. But then I get the error

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 14, in <module>
  File "/lib/bitbangio.py", line 28, in __init__
  File "/lib/bitbangio.py", line 40, in init
ValueError: I2C(-1) doesn't exist

At this point I've reached the edge of what I can figure out and don't know what to do next. It looks like the TCS34725's I2C errors were considered resolved back in April based on the thread here: https://github.com/adafruit/circuitpython/issues/4082 but as this post shows I'm still trying to get it working and finding different issues along the way. I should note that I also tried the SoftI2C method suggested at https://forum.micropython.org/viewtopic.php?t=9673 and didnt have success there either (incompatibility issues with the adafruit_bus_device.i2c_device library). Trying a dirty workaround for that (commenting out the try_lock checks in that library) did let me initialize the sensor, but I still found myself facing an "OSError: [Errno 5] EIO" when trying to use any of the sensor functions.

Anyways, thanks in advance for whatever advice you can give as to what I should try next or potential solutions that I'm missed in my search. I know this was a bit of a long post but hopefully the details will help someone else if they go searching with these same errors. Again I've had no problem running this with the Arduino Uno, so I'm hopeful that I can get this running eventually on the Pico.

User avatar
westfw
 
Posts: 2008
Joined: Fri Apr 27, 2007 1:01 pm

Re: difficulty using bitbangio with Pico and TCS34725, read/

Post by westfw »

I'm running MicroPython v1.17 ...
... example from the sensor's webpage: https://learn.adafruit.com/adafruit-col ... cuitpython
I think that part of your problem is that you're running MicroPython, and looking at CircuitPython examples.

The Pico should have hardware I2C; maybe check out https://www.digikey.com/en/maker/projec ... df4d626ba5 ?

User avatar
gmcc111
 
Posts: 2
Joined: Sat Sep 11, 2021 10:56 am

Re: difficulty using bitbangio with Pico and TCS34725, read/

Post by gmcc111 »

Thanks! Reloading it with CircuitPython 6.3.0 and redoing the prior troubleshooting I've been able to make it work.

For anyone who follows up on this with similar issues, I still had to use the bitbangio method to define the initial i2c object. Attempting it with i2c = board.I2C() has issues with recognizing the RP2040 chip and the Pico. For whatever reason the adafruit_platformdetect library worked fine on MicroPython but doesn't recognize the Pico in CircuitPython.

Link to where I saw the bitbang recommendation: viewtopic.php?f=60&t=176259&p=858537&hi ... 25#p858537

Thanks again!

User avatar
westfw
 
Posts: 2008
Joined: Fri Apr 27, 2007 1:01 pm

Re: difficulty using bitbangio with Pico and TCS34725, read/

Post by westfw »

I still had to use the bitbangio method
I'm glad that my hint/guess allowed you to get things working.
I still think it's odd that native I2C didn't work, but in fact "python on hardware" is not something that I have any experience with.
Perhaps now that's you're on circuitPython, someone else can step in with solutions to that problem as well. (My general impression is that Adafruit is much more "Circuit-python-y" than Micro-python-y.)

User avatar
danhalbert
 
Posts: 4649
Joined: Tue Aug 08, 2017 12:37 pm

Re: difficulty using bitbangio with Pico and TCS34725, read/

Post by danhalbert »

Using blinka on MicroPython on a Pi Pico could have some bugs to work out. You could file an issue about this in the blinka repo. If you are using the Pi Pico, you may as well go ahead and use CircuitPython, since you're using CircuitPython library anyway. Blinka is mainly used as a wrapper library on Linux boards (e.g. RPi), and the MicroPython helper has received less testing.

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

Return to “Adafruit CircuitPython”