Plasma2040 and ltr-559

It’s a bit maddening to see the ltr-559 sensor linked from the plasma2040 sales page but not have any working code. This code /should/ work, as I can import the ltr559 library into Thonny:

import time
import plasma
from plasma import plasma2040
from ltr559 import LTR559

sensor = LTR559()

try:
    while True:
        sensor.update_sensor()
        lux  = sensor.get_lux()
        prox = sensor.get_proximity()

        print("Lux: {:06.2f}, Proximity: {:04d}".format(lux, prox))

        time.sleep(0.05)
except KeyboardInterrupt:
    pass

But it doesn’t. I get this error:
MPY: soft reboot
Traceback (most recent call last):
File “”, line 10, in
File “/lib/ltr559/init.py”, line 213, in init
File “/lib/i2cdevice/init.py”, line 165, in init
File “/lib/smbus2/init.py”, line 23, in
File “/lib/smbus2/smbus2.py”, line 25, in
ImportError: no module named ‘fcntl’

which seems to be a dead end.

It feels like pimoroni wants us to use this sensor with the plasma2040, but I’m at my wits end here. Thanks for any help you can provide.

Found myself a clue, which lets me make readings.

import time
from plasma import plasma2040
from pimoroni_i2c import PimoroniI2C
from breakout_ltr559 import BreakoutLTR559

i2c = PimoroniI2C(plasma2040.SDA, plasma2040.SCL)

sensor = BreakoutLTR559(i2c)

try:
    while True:
        reading = sensor.get_reading()
        lux = reading[BreakoutLTR559.LUX]
        prox = reading[BreakoutLTR559.PROXIMITY]
        print("Lux:", lux, "Prox:", prox)

        time.sleep(0.05)
except KeyboardInterrupt:
    pass

This code works if and only if I print out the variable “reading” in its entirety. If I try to access any of "reading"s components using the subscript in the example above, I get the error

Traceback (most recent call last):
  File "<stdin>", line 14, in <module>
TypeError: 'NoneType' object isn't subscriptable

If I run the same code in the Thonny command line, it works just fine.

What’s the difference between running the code in the command line versus a program?

Yeah, you have to setup i2c to read the sensor.
I use an LTR-559 for auto brightness on my Interstate 75 W.
To get just the Lux, and ignore the Proximity I use the following.

reading = ltr.get_reading()
lux = reading[BreakoutLTR559.LUX]
1 Like

I figured out the problem-- sometimes the sensor returns “None”, so I need to check if the variable “reading” is non-null first.

    ltr_reading = ltr.get_reading()
    if ltr_reading:
        lux = ltr_reading[ltr.LUX]
        if lux < 5:
            for i in range(NUM_LEDS):
                hue = float(i) / NUM_LEDS / 10
                led_strip.set_hsv(i, hue + offset, 1.0, BRIGHTNESS)
            led.set_rgb(10*speed, 0, 255 - 10*speed)
        else:
            for i in range(NUM_LEDS):
                led_strip.set_hsv(i, 0, 0, 0)
            led.set_rgb(0, 0, 0)

All better now!

1 Like

Nice, I may give that a go on my Plasma setup. I tried to do something similar with a BH1745. That stalled and I never got back to it. I wanted to auto adjust the color and intensity. Right now I set the color with the buttons on an Encoder Wheel. Then adjust the brightness with the wheel (rotating ring).
Want to use a BH1745 for Ambient Lighting - Support - Pimoroni Buccaneers