Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    TSL2591 Board Interfacing

    Scheduled Pinned Locked Moved WiPy 2&3
    tsl2591
    10 Posts 2 Posters 993 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Offline
      jimmie
      last edited by

      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-hhR 1 Reply Last reply Reply Quote 0
      • robert-hhR Offline
        robert-hh @jimmie
        last edited by

        @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.

        J 1 Reply Last reply Reply Quote 0
        • J Offline
          jimmie @robert-hh
          last edited by jimmie

          @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.
          >>>
          
          robert-hhR 1 Reply Last reply Reply Quote 0
          • robert-hhR Offline
            robert-hh @jimmie
            last edited by robert-hh

            @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.

            J 1 Reply Last reply Reply Quote 0
            • J Offline
              jimmie @robert-hh
              last edited by jimmie

              @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

              robert-hhR 1 Reply Last reply Reply Quote 0
              • robert-hhR Offline
                robert-hh @jimmie
                last edited by robert-hh

                @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.

                J 2 Replies Last reply Reply Quote 0
                • J Offline
                  jimmie @robert-hh
                  last edited by jimmie

                  @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

                  1 Reply Last reply Reply Quote 0
                  • J Offline
                    jimmie @robert-hh
                    last edited by jimmie

                    @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-hhR 1 Reply Last reply Reply Quote 0
                    • robert-hhR Offline
                      robert-hh @jimmie
                      last edited by robert-hh

                      @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

                      J 1 Reply Last reply Reply Quote 0
                      • J Offline
                        jimmie @robert-hh
                        last edited by

                        @robert-hh

                        Thank you @robert-hh.

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

                        1 Reply Last reply Reply Quote 0

                        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                        With your input, this post could be even better 💗

                        Register Login
                        • 1 / 1
                        • First post
                          Last post

                        Pycom on Twitter

                        Pycom