leonhorka
Posts: 1
Joined: Tue Sep 10, 2019 3:54 am

Using a light sensor: Problems reading light/dark (output only prints out zeroes)

Tue Sep 10, 2019 4:07 am

I'm using a Raspberry Pi 3+ alongside a breadboard, a capacitor (1uf, 63v), three jumpers, and an analogue light sensor(also called light-dependent resistor or photocell). In fact, I'm using the exact same circuit as described here in the official Raspberry Pi website (chapter "Physical Computing with Python: Using a light-dependent resistor"):

https://projects.raspberrypi.org/en/pro ... mputing/11

What I want to do is to read light/dark using this set-up and in Raspbian (using the Mu program) I'm running the following code (also available in the previous link):

from gpiozero import LightSensor, Buzzer
Image
ldr = LightSensor(4) # alter if using a different pin
while True:
print(ldr.value)

It's supposed to be printing out values of the readings of light/dark accordingly but all the outputs I've been having are simply loads of zeroes (regardless if it's light or dark):

0.0
0.0
0.0

Any idea what may be causing this error in the readings?

jbudd
Posts: 1020
Joined: Mon Dec 16, 2013 10:23 am

Re: Using a light sensor: Problems reading light/dark (output only prints out zeroes)

Tue Sep 10, 2019 9:47 am

Does your code look like this

Code: Select all

from gpiozero import LightSensor, Buzzer
ldr = LightSensor(4) # alter if using a different pin
while True:
print(ldr.value)
or like this?

Code: Select all

from gpiozero import LightSensor, Buzzer
ldr = LightSensor(4) # alter if using a different pin
while True:
   print(ldr.value)
If you wrap code in code tags (there is a button at the top of the editor window), leading spaces are preserved. Correct indentation is essential in Python.

Have you checked you are using the right GPIO pins?
Is the capacitor plugged in the right way round?

pfletch101
Posts: 531
Joined: Sat Feb 24, 2018 4:09 am
Location: Illinois, USA

Re: Using a light sensor: Problems reading light/dark (output only prints out zeroes)

Thu Sep 12, 2019 4:29 pm

The default settings for the timeout within the LightSensor code may not be long enough for your environment (they seem to assume either a very well illuminated environment or a very sensitive sensor). Try playing with the optional charge_time_limit argument. I got good results in a normal home environment with

Code: Select all

from gpiozero import LightSensor
from time import sleep

# GPIO.setmode(GPIO.BCM)

sensorPin = 9

sensor = LightSensor(sensorPin, charge_time_limit=0.2)

try:
	while True:
		print(sensor.value)
		sleep(0.5)
except KeyboardInterrupt:
	pass

Return to “Troubleshooting”