TSL2591 Board Interfacing



  • I am trying to integrate the TSL2591 lux board with a WiPy3. I found the code on the following page below. I have attached the library file from the same page.

    http://lincolnsmithy.com/index.php/2017/04/27/pycom-wipy-2-0/
    I am running this code in main.py

    # main.py — put your code here!
    import time
    from machine import Pin
    from machine import I2C
    
    import tsl2591
    
    tsl = tsl2591.Tsl2591(0) # initialize tls2591
    
    cnt = 0
    
    while cnt != 10:
    
      time.sleep(2)
      full, ir = tsl.get_full_luminosity() # read raw values (full spectrum and ir spectrum)
      lux = tsl.calculate_lux(full, ir) # convert raw values to lux
      print(lux, full, ir)
      cnt = cnt + 1
    

    When testing, I am getting the following error. I would appreciate any help as I am new to microPython.

    Traceback (most recent call last):
      File "main.py", line 6, in <module>
      File "tsl2591.py", line 50
    SyntaxError: invalid syntax
    Pycom MicroPython 1.18.1.r1 [v1.8.6-849-b0520f1] on 2018-08-29; WiPy with ESP32
    Type "help()" for more information.
    >>>
    


  • @robert-hh

    Thank you @robert-hh.

    Your help is making a steep learning curve a lot easier.



  • @jimmie main.py runs a loop. You can update the file when it is running. That is not a problem. But main.py is forced to stop, maybe by you pushing Ctrl-C or the uploader. I do not know which tool you are using.
    I typically do not put my code into main.py. I put it into a separate file and import that in main.py for execution, or import it manually from the REPL prompt. Nevertheless, if I changed my code, I have to unload that from the memory, and the fastest way is to issue a soft reset by Ctrl-C or a hard reset by pushing the button or calling machine.reset().
    P.S.: I would write the loop control in main.py as:
    for cnt in range (10):
    and omit the cnt = cnt+1 line



  • @robert-hh

    It is now working but when I update main.py, sometimes I get the error below.

    I have not worked with an interpreted language before, but can this happen because I am writing main.py to the micro while it is printing? (I shortened the sleep amount to 0.2).

    Running c:\Users\ttc\Desktop\Pycom Code\main.py
    ♦Traceback (most recent call last):
      File "<stdin>", line 16, in <module>
      File "tsl2591.py", line 163, in get_full_luminosity
      File "tsl2591.py", line 66, in read_word_data
    KeyboardInterrupt:
    ♦>
    


  • @robert-hh

    Thank you @robert-hh .

    I just saw your response. I have fixed it per your recommendation and attached my current library. It is now working.

    0_1539868810500_tsl2591.py



  • @jimmie The basic I2C communication fails. Check
    a) that really the device is connecetd to P8 for SDA and P23 for SCL
    b) that the devices address is 0x29 or 41 decimal. You can use

    from machine import I2C
    i2c = I2C(0, I2C.MASTER, pins=("P8", "P23"))
    i2c.scan()
    

    for that. It should return [41]. If an empty list or a list with MANY numbers is returned, then the device is not properly connected. If it returns a different single number, then that is the proper address of the device, to be changed in line 72. Take care of hex/decimal value.



  • @robert-hh

    Thank you @robert-hh for your time.

    I fixed the library code and it is working now.

    I have attached the library I am using. It uses P8 and P23 for SDA and SCL respectively.

    0_1539868697903_tsl2591.py



  • @jimmie The whole code requires proper indentation at the various levels. In python, identation is not just editorial sugar, but mandatory to define the code structure. Please grab a book or lesson on Python code style and make yourself familiar with it.



  • @robert-hh

    Thank you @robert-hh .

    The code is attached and the latest error is below.

    0_1539859710465_tsl2591.py

    0_1539859788099_main.py

    Traceback (most recent call last):
      File "main.py", line 6, in <module>
      File "tsl2591.py", line 54
    SyntaxError: invalid syntax
    Pycom MicroPython 1.18.1.r1 [v1.8.6-849-b0520f1] on 2018-08-29; WiPy with ESP32
    Type "help()" for more information.
    >>>
    


  • @jimmie Would you please post the code in the for you are using it. The link you gave is not suitable, ebcause it does not show the changes which you applied.


Log in to reply
 

Pycom on Twitter