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

Re: Raspberry Pi Light Detector

Fri Apr 06, 2018 7:28 pm

bensimmo wrote:
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.
So this sensor only says Yes there is light or no there isn't light? I would need to find one that can give me a digital range I think.

For now though, with the demo code its printing a 0 for light and a 1 or dark.

How could I get it to say

Code: Select all

if 0 print LED_BRIGHTNESS 255
elif 1 print LED_BRIGHTNESS 100  

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

Re: Raspberry Pi Light Detector

Fri Apr 06, 2018 7:36 pm

Just read up on how the python PRINT function works.
Have a look in the education section for LEDs and switches.

That sensor does have an analogue out (the A pin) but you'll need, as mentioned, an ADC. Have a read of the links given before.

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

Re: Raspberry Pi Light Detector

Fri Apr 06, 2018 7:39 pm

Thanks bensimmo!! Been digging around on this for a couple days. Appreciate the help.

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

Re: Raspberry Pi Light Detector

Tue Apr 10, 2018 1:31 am

Incase someone else is looking at this wondering the same thing . This is what I ended up needing to code. It is very simple, but I really had not frame of reference so I had a difficult time even knowing what to google.

Code: Select all

if GPIO.input(4) == 1:
    LED_BRIGHTNESS = 75
else:
    LED_BRIGHTNESS = 255
Last edited by hilts50 on Tue Apr 10, 2018 11:58 am, edited 1 time in total.

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

Re: Raspberry Pi Light Detector

Tue Apr 10, 2018 6:55 am

:-)
All part of learning Python, learning the language basics first.


And correctly spaced out.
You use [ code ] tags around it.

Code: Select all

if GPIO.input(4) == 1:
    LED_BRIGHTNESS = 75
else:
    LED_BRIGHTNESS = 255
    

Paul Hutch
Posts: 369
Joined: Fri Aug 25, 2017 2:58 pm
Location: Blackstone River Valley, MA, USA
Contact: Website

Re: Raspberry Pi Light Detector

Tue Apr 10, 2018 3:24 pm

A quick stylistic comment, all caps with underscores is usually reserved for constants, since you're changing the value of LED_BRIGHTNESS it is not a constant so should be lower case.

Ref. https://www.python.org/dev/peps/pep-000 ... onventions

Of course as with everything in programming this is a suggestion/recommendation not a rule.

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

Re: Raspberry Pi Light Detector

Tue Apr 10, 2018 3:56 pm

Paul Hutch wrote:
Tue Apr 10, 2018 3:24 pm
A quick stylistic comment, all caps with underscores is usually reserved for constants, since you're changing the value of LED_BRIGHTNESS it is not a constant so should be lower case.

Ref. https://www.python.org/dev/peps/pep-000 ... onventions

Of course as with everything in programming this is a suggestion/recommendation not a rule.
Thanks for the insight. I got the main script off of GitHub and am just adding the light sensor to the mix. I'll see if I can go through all the parts of the code and make these changes to see how it impacts things.

Thanks,

Bill

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

Re: Raspberry Pi Light Detector

Tue Apr 10, 2018 8:10 pm

Use a find&replace on the keyword, that way you don't miss any.


Or i'm sure I used one somewhere that changed all at one of I wanted, perhaps.that was Thonny editor?
Or something else I used.

SeppPi
Posts: 13
Joined: Sat Sep 30, 2017 2:30 pm

Re: Raspberry Pi Light Detector

Wed Jun 19, 2019 7:56 am

Hello, I use the FlyingFish Light Detector (LM393 4pin). Is there a way to read it with GPIOZERO? Thanks for help. Greetings

User avatar
B.Goode
Posts: 8045
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Raspberry Pi Light Detector

Wed Jun 19, 2019 8:24 am

SeppPi wrote:
Wed Jun 19, 2019 7:56 am
Hello, I use the FlyingFish Light Detector (LM393 4pin). Is there a way to read it with GPIOZERO? Thanks for help. Greetings


Yes.


If you use the Digital output from the detector then it simply acts as a switch that is either On or Off (Open/Closed). So you can treat it as though it were a Button connected to gpiozero.


To use the Analogue output from the detector you will need an external analog-to-digital converter such as an MCP3008. Again this component is supported and documented with gpiozero.

SeppPi
Posts: 13
Joined: Sat Sep 30, 2017 2:30 pm

Re: Raspberry Pi Light Detector

Wed Jun 19, 2019 8:40 am

Thank you. Got it.

Return to “Python”