Page 1 of 2

using tsl2561 light sensor with a pyboard

Posted: Fri Apr 07, 2017 6:29 am
by sashaks
Hello,
I am trying to use a TSL2561 light sensor with a pyboard. I have previously used this sensor successfully with an ESP8266 using the following code:

import tsl2561
from machine import I2C, Pin
i2c = I2C(Pin(5), Pin(4))
sensor = tsl256.TSL2561(i2c)
sensor.read()

where 5 = scl and 4 = sda on the ESP8266

On the pyboard I am using scl = 'X9' and sda = 'X10" as those are the built ins, and when I substitute them into the above code I get an error that it can't convert Pin to an integer. I have tried to modify the i2c object with i2c = machine.I2C(id = -1, scl = machine.Pin('X9'), sda = machine.Pin('X10'), freq=40000) but still get an error, this time "TypeError: function missing 1 required positional arguments". I'm not sure what I am missing. Does anyone have any ideas on how to get this to work with a pyboard? Thanks!

Re: using tsl2561 light sensor with a pyboard

Posted: Fri Apr 07, 2017 6:00 pm
by dhylands
Try using I2C(1)

Re: using tsl2561 light sensor with a pyboard

Posted: Mon Apr 10, 2017 5:47 pm
by sashaks
Thank you - I am able to do that if I use pyb instead of machine and I get a little bit farther, but now I can't manage to get the tsl2561.py file to work.

Currently I have:
>>> import pyb
>>> import tsl2561
>>> i2c = pyb.I2C(1, pyb.I2C.MASTER)
>>> sensor = tsl2561.TSL2561(i2c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "tsl2561.py", line 48, in __init__
File "tsl2561.py", line 105, in sensor_id
File "tsl2561.py", line 67, in _register8
AttributeError: 'I2C' object has no attribute 'readfrom_mem'

In looking into this it seems like readfrom_mem() is from machine and in pyb it is mem_read() so I went into the file and changed all those over (as well as the writeto_mem() to mem_write()) ... still no luck and instead I get: OSError: [Errno 5] EIO

Thanks for the help!

Re: using tsl2561 light sensor with a pyboard

Posted: Tue Apr 11, 2017 1:39 am
by sashaks
Actually found the solution, you can manually designate the pyboard pins with -1 in the I2C function:
>>> import tsl2561
>>> from machine import Pin, I2C
>>> i2c = machine.I2C(-1, scl = machine.Pin('X9'), sda = machine.Pin('X10'), freq = 10000)
>>> sensor = tsl2561.TSL2561(i2c)
>>> sensor.read()

Re: using tsl2561 light sensor with a pyboard

Posted: Tue Apr 11, 2017 5:54 am
by dhylands
Note that when you use -1 and specify the pins, then you're using a SW implementation of I2C. When you pass in a positive number, you're using the HW I2C.

Re: using tsl2561 light sensor with a pyboard

Posted: Tue Apr 11, 2017 6:47 am
by pythoncoder
The pyb and machine implementations of I2C have different interfaces so it's important to use the appropriate documentation. In general machine is designed for portability across platforms. pyb is Pyboard-specific and (in general) tends to be more complete in terms of encompassing Pyboard functionality.

Re: using tsl2561 light sensor with a pyboard

Posted: Wed Jan 17, 2018 7:42 pm
by keithostertag
I'm also attempting to use the adafruit tsl2561.py to play with a tsl2561 board connected to a pyboard and am glad to find this discussion (albeit old now). I'm using Linux (screen) to connect.

I keep getting a

Code: Select all

OSError: [Errno 19] ENODEV
error. Reading some other posts this would normally suggest a compiler error? (/* No such device */) But I can't see what's causing that... does it look as though I am having RAM space issues?

I've tried it various ways, but here is a recent REPL sequence where I use gc and micropython.mem_info() to follow the memory status... is my problem in any way related to memory or storage? Or do I get that error based on some code not yet being correct?

Code: Select all

MicroPython v1.9.3-240-ga275cb0f on 2018-01-11; PYBv1.1 with STM32F405RG
Type "help()" for more information.
>>> import micropython
>>> micropython.mem_info()
stack: 500 out of 15360
GC: total: 102400, used: 1104, free: 101296
 No. of 1-blocks: 12, 2-blocks: 7, max blk sz: 40, max free sz: 6321
 
>>> import tsl2561
>>> micropython.mem_info()
stack: 500 out of 15360
GC: total: 102400, used: 8096, free: 94304
 No. of 1-blocks: 80, 2-blocks: 45, max blk sz: 74, max free sz: 5200
 
>>> import gc
>>> micropython.mem_info()
stack: 500 out of 15360
GC: total: 102400, used: 8272, free: 94128
 No. of 1-blocks: 85, 2-blocks: 48, max blk sz: 74, max free sz: 5200
 
>>> gc.collect()
>>> micropython.mem_info()
stack: 500 out of 15360
GC: total: 102400, used: 6752, free: 95648
 No. of 1-blocks: 42, 2-blocks: 44, max blk sz: 74, max free sz: 5200
 
>>> import machine, pyb
>>> micropython.mem_info()
stack: 500 out of 15360
GC: total: 102400, used: 6960, free: 95440
 No. of 1-blocks: 46, 2-blocks: 47, max blk sz: 74, max free sz: 5200
 
>>> gc.collect()
>>> micropython.mem_info()
stack: 500 out of 15360
GC: total: 102400, used: 6800, free: 95600
 No. of 1-blocks: 42, 2-blocks: 44, max blk sz: 74, max free sz: 5200
 
>>> from machine import Pin, I2C
>>> micropython.mem_info()
stack: 500 out of 15360
GC: total: 102400, used: 7040, free: 95360
 No. of 1-blocks: 47, 2-blocks: 47, max blk sz: 74, max free sz: 5200
 
>>> gc.collect()
>>> micropython.mem_info()
stack: 500 out of 15360
GC: total: 102400, used: 6832, free: 95568
 No. of 1-blocks: 41, 2-blocks: 45, max blk sz: 74, max free sz: 5200
 
>>> i2c = machine.I2C(-1, scl = machine.Pin('X9'), sda = machine.Pin('X10'), freq = 10000)
>>> gc.collect()
>>> micropython.mem_info()
stack: 500 out of 15360
GC: total: 102400, used: 7008, free: 95392
 No. of 1-blocks: 42, 2-blocks: 43, max blk sz: 74, max free sz: 5200
 
>>> sensor = tsl2561.TSL2561(i2c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "tsl2561.py", line 47, in __init__
  File "tsl2561.py", line 104, in sensor_id
  File "tsl2561.py", line 66, in _register8
OSError: [Errno 19] ENODEV

>>> i2c.scan()
[35]
[\code]

Re: using tsl2561 light sensor with a pyboard

Posted: Thu Jan 18, 2018 6:13 am
by pythoncoder
It's most unlikely to be RAM related for two reasons. Firstly, if it were, the error message would be explicit. And secondly all the examples you give show that very little RAM is being used. There is a fault somewhere in the MicroPython code which is causing the device interface to fail. Does the line number not give any clues?

In general you're unlikely to hit RAM problems on a Pyboard unless your program runs to over about 1000 lines or you're using large buffers (very roughly 4KB or more). And then there are solutions.

Re: using tsl2561 light sensor with a pyboard

Posted: Fri Jan 19, 2018 4:57 am
by keithostertag
Thanks Peter. As a beginner I don't know what I don't know... so having a confirmation that it is not likely to be a memory related issue helps me to more easily concentrate on the code.

Keith

Re: using tsl2561 light sensor with a pyboard

Posted: Sun Sep 16, 2018 6:57 pm
by rp346@njit.edu
[quote=dhylands post_id=19129 time=1491890048 user_id=81]
Note that when you use -1 and specify the pins, then you're using a SW implementation of I2C. When you pass in a positive number, you're using the HW I2C.
[/quote]

Is this correct way to access HW I2C.

import tsl2561
from machine import Pin, I2C
i2c = machine.I2C(scl = machine.Pin(12), sda = machine.Pin(13), freq = 10000)
sensor = tsl2561.TSL2561(i2c)
sensor.read()