Raspberry PI LDR example

In this example we connected an LDR to our Raspberry Pi, we will connect the output to a GPIO pin and detect whether its light or dark

Here is a typical module

ldr module
ldr module

You connect the module to you Raspberry Pi as follows.

Pi GPIO headerPin NumberLDR module
3v31Vcc
GND6GND
GPIO 49SIG

Create a file called ldr.py. Enter the following code using your favourite text editor

 

import RPi.GPIO as GPIO
 
GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)
 
for i in range(0,5):
    print GPIO.input(4)

 

Save and run the example by typing the following in at the command line

sudo python ldr.py

No block and unblock the LDR

 

Links