Light Sensor

Hi. I have been tasked by my son to automate the opening and closing of his chicken coop doors using a Raspberry Pi. I have devised a system based on the times of sunrise and sunset which works fine on my test rig. Unfortunately when it gets dark the chickens go to roost in the coop before sunset which means I need to close the doors to guard from foxes so ideally I need to change to a system based on light levels. One option would be to use a photo resistor and an ADC converter - but I doubt that my electronic skills are up to building that and at 76 they’re not likely to improve much :-). What I need is a complete solution that I can just connect to the GPIO pins on the Pi and read the light level.with software. Any suggestions gratefully received!

This may do what you want.

I have one in a portable weather clock I made. I use it to adjust my display brightness based on background light levels. In dim / dark conditions the display is dimmed. In bright sunlight its set to full bright to make it easier to read.

from ltr559 import LTR559
ltr559 = LTR559()

ltr559.update_sensor()
lux = ltr559.get_lux()
Lux =("{:06.2f}".format(lux))
Lux = int(float(Lux))
Lux = round(Lux)

if Lux < 5000:
        sense.low_light = True
        ledshim.set_brightness(0.5)
        ledshim.show()
elif Lux >= 5000:
        sense.low_light = False
        ledshim.set_brightness(1.0)
        ledshim.show()

That looks ideal! Many thanks. I assume the library can be downloaded?

John

You can get it here,

Running the git clone https://github.com/pimoroni/ltr559-python on that Pi should get you everything including the examples.
There is also a download Zip option under the green code button if you want to download it to your PC.

If you want to remote mount the sensor away from the Pi this cable may come in handy.


Solder a male header on the sensor and just ignore the 1 extra wire.

Many thanks! What a brilliant resource this is :-)

John

It’s not always obvious, but the libraries are linked too from the product pages. If you see Bold text it’s usually a link to the Python Library, and how to install it etc. There are often good tutorials in the Learn section to take things to another level and do very custom stuff.

As part of my project I am using a 4-relay HAT which has a passthrough set of pins. The relays are using pin 7 which I assume is a clock. I see that the light sensor also uses pin 7 - can both devices share pin 7 or can I use another pin for the light sensor which will probably be on the roof of the chicken coop with the raspberry pi inside. Also, are there any published limits as to the length of an extension cable? I was offered a 12" one but might need longer.

Thanks again.

Is this for the Breakout Garden sensor? As far as I can see BCM7/GPIO4 is used for interrupts, which you may not need. If your program just periodically asks the sensor what the light level is then as far as I can see you can ignore this, as by default it looks like it doesn’t use interrupts and so won’t actually touch BCM7.

I’m not sure about the length, this depends a bit on how the sensor board has been put together. You might need to just try it and see, but if you search around the web there are some anecdotal claims of fairly long lengths.

Near as I can tell, that INT pin, GPIO 4, is used for the Proximity sensing on the LTR-599.
Have a look at the proximity-interrupt.py example

INTERRUPT_PIN = 4
# Enable interrupts and set the pin to active LOW mode
ltr559 = LTR559(enable_interrupts=True, interrupt_pin_polarity=0)

I’m thinking if need be you could actually turn it off with enable_interrupts=False

Good - I don’t need the proximity bit :-)

Thanks again

Some Breakout Garden breakouts use it some don’t. The ones that don’t use it have the do not enter symbol (or whatever it is) on that pin, the ones that do use it have INT.
I have a RV3208 RTC breakout hooked up with my LTR-599, no issues that I am aware of. On the RV3208 I think its used if you set an alarm etc. Otherwise it does nothing.

Thanks. I’ve just orfered one so I’ll see what happens :-)

It shouldn’t be an issue with that sensor, but be aware that most plastics and some glass block UV light. My LTR-599 is teamed up with a VEML6075 UV sensor. All the clear plastic covers i had on hand messed with the UV sensor big time. I ended up bumming a couple of glass strips used for microscope slides from my dogs vet. It’s fused quartz crystal and as transparent as it gets for glass.
Do your readings and setup with what ever clear cover you plan on using in place. Other wise you may not get the readings you setup for and get mixed results.

Good point! Thanks again.

I’ve installed the ltr559 on a new Pi zero W and it is great! I then installed it on an older Pi zero W and had problems installing the library so installed git and then the library. I am now getting the following error message:

Traceback (most recent call last):
File “chickens_test.py”, line 156, in
ltr559 = LTR559()
File “/usr/local/lib/python3.7/dist-packages/ltr559/init.py”, line 212, in init
BitField(‘ALS’, 0x0F)
File “/usr/local/lib/python3.7/dist-packages/i2cdevice/init.py”, line 165, in init
import smbus
ImportError: /usr/local/lib/python3.7/dist-packages/smbus.cpython-37m-arm-linux-gnueabihf.so: file too short

I’m lost - any help gratefully received. :-)

If that Pi is running PiOS “Lite” you may be missing a dependency.
One option is to try the alternate install instructions

git clone https://github.com/pimoroni/ltr559-python
cd ltr559-python
sudo ./install.sh

It sounds like you may have already done that though?

Hi
Many thanks for responding. I have done that already, it looks as though it was only partially successful. I’ve found a file uninstall.sh but when I try to run it using ./uninstall.sh I get permision denied and if I try sudo ./uninstall.sh I get command not found. I’m still very new to Linux - should I just try and repeat the installation process?

TIA

I’ve tried repeating the process but had a fatal error as ltr559-python directory exists and is not empty - should I just delete it and try again? As I said I’m new to Linux and concerned that by just deleting files I may find myself in a situation I can’t recover from :-)

When things are really messed up, I usually just start over with a freshly reimaged SD card.
I like doing things, when possible from a GUI. I usually install the “with desktop” version and then when its all is set up, I set it to boot to command line. On my headless setups anyway. Everybody has their preferences on how they like to do things.
I only mention it because I’ve seen a few issues with the lite version being missing some stuff that is on the Desktop version.