ganken
Posts: 13
Joined: Sat Dec 24, 2016 2:57 pm

Raspberry Pi Light Detector

Sat Dec 24, 2016 3:07 pm

I'm using a light detector module with pi 3 B. When I have the detector connected I always get the reading of 0 but when its not connected I get the reading of 1. Am I doing the right thing with the following code?

Code: Select all

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)

for i in range(0,5):
    print GPIO.input(4)

ghp
Posts: 1399
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Raspberry Pi Light Detector

Sat Dec 24, 2016 3:37 pm

Hello,
what type of light detector do you use, and how is this connected?
Regards,
Gerhard

ganken
Posts: 13
Joined: Sat Dec 24, 2016 2:57 pm

Re: Raspberry Pi Light Detector

Sat Dec 24, 2016 9:43 pm

I am using one of these and its connected via the GPIO pins

https://www.jaycar.com.au/arduino-compa ... e/p/XC4446

ganken
Posts: 13
Joined: Sat Dec 24, 2016 2:57 pm

Re: Raspberry Pi Light Detector

Sat Dec 24, 2016 9:53 pm

I have it connected as per this link

http://www.uugear.com/portfolio/using-l ... pberry-pi/

Thank you very much for your help

User avatar
DougieLawson
Posts: 35789
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Raspberry Pi Light Detector

Sat Dec 24, 2016 10:11 pm

That sensor is analog (it's just a LDR with a resistor divider), the UUGear thing you've found is digital (because it's got an ADC between the LDR and the interface).

You need something like a MCP3008 between your LDR and your Raspberry.
http://www.raspberrypi-spy.co.uk/2013/1 ... n-mcp3008/
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

ganken
Posts: 13
Joined: Sat Dec 24, 2016 2:57 pm

Re: Raspberry Pi Light Detector

Sun Dec 25, 2016 1:30 am

Oh right :cry: Thanks for that...

Will this work? Looks like its analog too but the support tells me that it will

http://www.freetronics.com.au/products/ ... F8hD7Z96YU

User avatar
DougieLawson
Posts: 35789
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Raspberry Pi Light Detector

Sun Dec 25, 2016 6:57 am

That's another analog device, they're using the Arduino as an ADC. The Arduino has six analog pins and a built-in ADC (which has a 10-bit resolution).
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

ghp
Posts: 1399
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Raspberry Pi Light Detector

Sun Dec 25, 2016 10:36 am

Hello,
perhaps you try the BH1750 Luminosity Sensor ? This has a digital interface I2C which can be used from raspberry pi. There are breakout boards availabe for this device.
Or you connect a simple ADC, as there are MCP3008, MCP3202 or a ADS1015 to the Pi and connect your analog sensor to these.
Regards
Gerhard

PiGraham
Posts: 3574
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Raspberry Pi Light Detector

Sun Dec 25, 2016 11:06 am

ganken wrote:I have it connected as per this link

http://www.uugear.com/portfolio/using-l ... pberry-pi/

Thank you very much for your help
That seems to be a simple light level switch that should work OK with the Pi.
Have you tried adjusting the sensitivity? Have yo tried covering the sensor so it's really dark? You could try a bit of black insulating tape, a big blob of Blue tack or a bit of tinfoil (be very careful not to let it touch the circuitry).

PiGraham
Posts: 3574
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Raspberry Pi Light Detector

Sun Dec 25, 2016 11:42 am

There is some confusion here.
ganken wrote:I am using one of these and its connected via the GPIO pins

https://www.jaycar.com.au/arduino-compa ... e/p/XC4446
That's an analogue sensor.

But this is digital
ganken wrote:I have it connected as per this link

http://www.uugear.com/portfolio/using-l ... pberry-pi/

Thank you very much for your help
From the second link:
using a high quality photoresistor
working voltage: 3.3~5V
output: digital switching  (LOW or HIGH voltage on D pin) and analog signal (voltage output on A pin)
using a wide voltage LM393 comparator that has good waveform
output current >= 15mA, can directly light LED.
with adjustable potentiometer to adjust the sensitivity
It seems you have an analogue sensor and are trying to use it as if was digital according to instruction for a digital module.

If you only need light/dark detection a photosensor and comparator module is ideal.

In fact you can use an LDR direct to a Pi GPIO input. Make a potential divider with a resistor, so that when the light level changes the voltage changes from high level to low. There is a likelihood of getting lots of pulses on and off as the ight gets close to the switching threshold. A comparator will probably give a cleaner changeover, but you can handle that with software.

ganken
Posts: 13
Joined: Sat Dec 24, 2016 2:57 pm

Re: Raspberry Pi Light Detector

Mon Dec 26, 2016 8:47 am

Thanks for the info.

I'm going to get this one.. Its a digital sensor should have more luck (I hope)

http://core-electronics.com.au/bh1750fv ... y-302.html

ganken
Posts: 13
Joined: Sat Dec 24, 2016 2:57 pm

Re: Raspberry Pi Light Detector

Mon Dec 26, 2016 8:49 am

PiGraham wrote:
ganken wrote:I have it connected as per this link

http://www.uugear.com/portfolio/using-l ... pberry-pi/

Thank you very much for your help
That seems to be a simple light level switch that should work OK with the Pi.
Have you tried adjusting the sensitivity? Have yo tried covering the sensor so it's really dark? You could try a bit of black insulating tape, a big blob of Blue tack or a bit of tinfoil (be very careful not to let it touch the circuitry).
How do adjust the sensitivity? I'm confused as to why would I get a reading of 1 when the module is not connected

User avatar
Burngate
Posts: 5967
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Raspberry Pi Light Detector

Mon Dec 26, 2016 10:57 am

ganken wrote:I'm using a light detector module with pi 3 B. When I have the detector connected I always get the reading of 0 but when its not connected I get the reading of 1.

Code: Select all

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)

for i in range(0,5):
    print GPIO.input(4)
ganken wrote:I'm confused as to why would I get a reading of 1 when the module is not connected
You're using GPIO 4 (header pin 7) which by default has pull-up enabled. That means, when nothing else is connected, it's pulled up to 3v3 and is detected as 1.

When you connect your LDR module (which seems to be just the LDR and a resistor in series between 3v3 and ground, with their junction connected to the output) :
either the LDR is at the bottom of the chain, and is low resistance because there's plenty of light, and it's pulling the output low;
or the LDR is at the top of the chain, it's high resistance because there's not enough light, and the resistor is pulling the output low.

Either way, the Pi's internal pull-up, at 50k, is very much weaker than the resistance on the module.

PiGraham
Posts: 3574
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Raspberry Pi Light Detector

Tue Dec 27, 2016 6:48 am

ganken wrote:
PiGraham wrote:
ganken wrote:I have it connected as per this link

http://www.uugear.com/portfolio/using-l ... pberry-pi/

Thank you very much for your help
That seems to be a simple light level switch that should work OK with the Pi.
Have you tried adjusting the sensitivity? Have yo tried covering the sensor so it's really dark? You could try a bit of black insulating tape, a big blob of Blue tack or a bit of tinfoil (be very careful not to let it touch the circuitry).
How do adjust the sensitivity? I'm confused as to why would I get a reading of 1 when the module is not connected
Following that link you gave shows a sensor module with digital and analog output. There is a potentiometer on that module (blue box with grey disc that has a cross cut into it). Turn the grey disc to adjust sensitivity to light.

http://www.uugear.com/product/uugear-li ... og-output/

But it seems you don't have the module you posted a link to, so this doesn't help.

What are you trying to achieve? Do you want to measure the light level, or just react when it gets light, or dark?

ganken
Posts: 13
Joined: Sat Dec 24, 2016 2:57 pm

Re: Raspberry Pi Light Detector

Tue Jan 03, 2017 11:19 am

PiGraham wrote:
ganken wrote:
PiGraham wrote:I have it connected as per this link

http://www.uugear.com/portfolio/using-l ... pberry-pi/

Thank you very much for your help

That seems to be a simple light level switch that should work OK with the Pi.
Have you tried adjusting the sensitivity? Have yo tried covering the sensor so it's really dark? You could try a bit of black insulating tape, a big blob of Blue tack or a bit of tinfoil (be very careful not to let it touch the circuitry).
How do adjust the sensitivity? I'm confused as to why would I get a reading of 1 when the module is not connected
Following that link you gave shows a sensor module with digital and analog output. There is a potentiometer on that module (blue box with grey disc that has a cross cut into it). Turn the grey disc to adjust sensitivity to light.

http://www.uugear.com/product/uugear-li ... og-output/

But it seems you don't have the module you posted a link to, so this doesn't help.

What are you trying to achieve? Do you want to measure the light level, or just react when it gets light, or dark?
I'm trying to detect between dark and light thats all I don't need to know the amount of light

I went ahead and got this one

http://www.raspberrypi-spy.co.uk/2015/0 ... ty-sensor/

Do I need to use the breadboard to connect? In this post both ADD and GND are connected to P1-06. As I am connecting the module directly to the pi I connected the ADD to P1-06 and the GND to P1-14. When I run "i2cdetect -y 1" it doesn't show anything.

I am new to this so appreciate all your help :)

User avatar
DougieLawson
Posts: 35789
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Raspberry Pi Light Detector

Tue Jan 03, 2017 11:22 am

Give us a clear photo of your wiring. Post the photo on http://imgur.com post the link on here.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

ghp
Posts: 1399
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Raspberry Pi Light Detector

Tue Jan 03, 2017 3:52 pm

Hello, you do not need a breadboard. According to the chip bh1750 datasheet, the ADD-line is connected to GND.
Both P1-06 and and P1-14 are GND, so this is ok.
I assume that you enabled the I2C-Interface with raspi-config ?
Regards,
Gerhard

ganken
Posts: 13
Joined: Sat Dec 24, 2016 2:57 pm

Re: Raspberry Pi Light Detector

Tue Jan 03, 2017 10:10 pm

ghp wrote:Hello, you do not need a breadboard. According to the chip bh1750 datasheet, the ADD-line is connected to GND.
Both P1-06 and and P1-14 are GND, so this is ok.
I assume that you enabled the I2C-Interface with raspi-config ?
Regards,
Gerhard
Yes I enabled the interface

ganken
Posts: 13
Joined: Sat Dec 24, 2016 2:57 pm

Re: Raspberry Pi Light Detector

Wed Jan 04, 2017 11:18 am

I moved the ground to P1-20 instead and now I can see the device when I run i2cdetect -y 1. Its showing 23 as device address
(It looks like P1-14 is something special its marked in white in this diagram, I'm not sure what that means though :) http://pi4j.com/pins/model-3b-rev1.html)

Anyway this is my output. But when I run the script on this page http://www.raspberrypi-spy.co.uk/2015/0 ... ty-sensor/ I am getting a 0 reading all the time with/without light. Is this script right?

Output from i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- 23 -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

ghp
Posts: 1399
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Raspberry Pi Light Detector

Wed Jan 04, 2017 12:44 pm

Hello,
luckily I found a BH1750 on my desk somewhere. With the code found in the link you posted, there are reasonable measurements.
Perhaps you have a typo somewhere ?
Regards,
Gerhard

ganken
Posts: 13
Joined: Sat Dec 24, 2016 2:57 pm

Re: Raspberry Pi Light Detector

Wed Jan 04, 2017 10:14 pm

ghp wrote:Hello,
luckily I found a BH1750 on my desk somewhere. With the code found in the link you posted, there are reasonable measurements.
Perhaps you have a typo somewhere ?
Regards,
Gerhard
Unlikely cause I downloaded the script from Bitbucket as per the page. I didn't solder the wires to BH1750 could it be the problem? I might solder it tonight and try again.

ganken
Posts: 13
Joined: Sat Dec 24, 2016 2:57 pm

Re: Raspberry Pi Light Detector

Thu Jan 05, 2017 10:10 pm

Turns out as I suspected it needed to be soldered. Did that last night and now I have readings.

Thanks everyone for your help. Really appreciate it :D

hilts50
Posts: 10
Joined: Sat Mar 31, 2018 2:56 am

Re: Raspberry Pi Light Detector

Fri Apr 06, 2018 4:46 pm

Im trying to use the digital Light found at Sensor http://www.uugear.com/portfolio/using-l ... pberry-pi/

I want it to print code into a python script Im working on.

If I use the code provided below....

Code: Select all

import RPi.GPIO as GPIO
 
GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)
 
for i in range(0,5):
    print GPIO.input(4)
How would I get the script to print LED_BRIGHTNESS values for me based on light ranges?

Would I use something like this?

Code: Select all

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN) 

for i in range(0,2):
        print LED_BRIGHTNESS = 100

for i in range(3,5):
		print LED_BRIGHTNESS = 255

User avatar
bensimmo
Posts: 4152
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Raspberry Pi Light Detector

Fri Apr 06, 2018 6:19 pm

You cannot.

That 'For loop' is just there to loop 5 times, nothing else.

Your sensor only has two values On and Off (or True/False ). It is effectively a switch.


EDIT
I have just not the sensor has an Analogue output, the Pi cannot interface with that directly and would probably need an ADC.

User avatar
bensimmo
Posts: 4152
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Raspberry Pi Light Detector

Fri Apr 06, 2018 6:29 pm

For the Analogue have a look at the background here, the MCP3008 is supported by the GpioZero Python3 module included in Raspbian. It also makes it simple to use.
https://learn.adafruit.com/raspberry-pi ... s/overview

http://gpiozero.readthedocs.io/en/stabl ... ith-an-adc

Return to “Python”