Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5323

Automation, sensing and robotics • Re: Simple light level detector using photoresistor?

$
0
0
To stay away* from RPi.gpio
Use gpiozero
https://projects.raspberrypi.org/en/pro ... mputing/10
Thanks, I had not found this when searching!

So I tried this suggestion for my own connections:

Code:

from gpiozero import LightSensor, Buzzerldr = LightSensor(18)  # alter if using a different pinwhile True:    print(ldr.value)
But when I tried it it was silent for a couple of seconds, then started to send an incredible number of data to the screen:

Code:

0.0repeated some 50 times 0.00.18500409985426813repeated some 50 times 0.185004099854268130.0repeated some 50 times 0.00.4979024995700456repeated some 50 times 0.4979024995700456
etc, etc until Ctrl-C was pressed
What can one do to make the program just read the value and print it, then exit?
All these zeros should not be there for instance.

But I tried commenting out the while :

Code:

from gpiozero import LightSensor, Buzzerldr = LightSensor(18)  # alter if using a different pin#while True:    print(ldr.value)
and when I tested I got this:

Code:

  File "/home/pi/projects/photoresistor/lightsensor.py", line 5    print(ldr.value)IndentationError: unexpected indent
Again I have not used Python before so I may have done something illegal, but both this and the previous example I tested use a while True loop which seems very strange to me, why not just read the value and exit?

*Why stay away, things have moved on in Pis and the Pi5 and maybe other cannot use it as is. Your Zero is fine, I assume an older OS too? Bullseye or buster?
This Zero is running bullseye.
Has been used to monitor outdoors temperatures for a couple of years now.
But I want it to also monitor outside light level.
It reports via MQTT.
All else is being done using bash scripting...

Statistics: Posted by Bosse_B — Tue Dec 03, 2024 9:53 am



Viewing all articles
Browse latest Browse all 5323

Trending Articles