# Pi Zero W and Light Sensor with LDR

I have a Raspberry PI Zero W and Light Sensor like on the photo: I connected it to Raspberry Pi as follow: GND to GND VCC to 5V SIG to GPIO-3

Using the following code below I am able to detect if its dark or bright outside (by covering the sensor with my hand):

``````    import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN)

for i in range(0,5):
print ( GPIO.input(3))
sleep(1)
``````

This gives me an output of either 1 or 0. It depends on the light condition. I would like to be able to get more detailed data than just 1 and 0, e.g power of light lux or something so I can track how much sun my plant is getting at the current time.

Is there something I can upgrade within my code or the issue is that the sensor is analog and Pi is digital?

• your last few words hit the target .... you need a sensor that produces a digital data signal or you need to use an ADC (analog to digital converter) – jsotola Mar 19 at 0:22

The module you have includes a resistive sensor and a comparator which produces a 1-bit value (0 or 1). There's nothing you can do to get a more detailed signal from that module.

There are two main approaches which allow to read analog values from resistive sensors with devices which don't have analog pins, such as raspberries. simulate this circuit – Schematic created using CircuitLab

Conceptually, this is a voltage divider which converts the variable resistance of the LDR into a variable voltage, and then the voltage is sampled by the ADC (PCF8591, ADS1x15, MCP3002 just to name a few) and transmitted to the Raspberry in the form of a digital signal (I2C, SPI, etc.) This is the proper way that achieves good precision and fast conversion time.

Here's a tutorial for ADC1015, explaining how to connect it to the Raspberry and read the analog values.

1. Poor man's ADC (based on a step response): simulate this circuit

This is also a voltage divider, but since a capacitor is used for the lower leg, the voltage rises with time, so that the sensor resistance becomes inversely proportional to the pulse length. The measurement cycle consists in driving the GPIO pin LOW to discharge the capacitor, then switch it to input and measure the time it takes for the pin to go from LOW to HIGH:

``````def discharge():
GPIO.setup(ldr_pin, GPIO.OUT)
GPIO.output(ldr_pin, False)
time.sleep(0.01)

def pulse_length():
count = 0
GPIO.setup(ldr_pin, GPIO.IN)
while not GPIO.input(ldr_pin):
count = count + 1
time.sleep(0.001)
return count