Home Programming Raspberry PI LDR example

Raspberry PI LDR example

by shedboy71

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 header Pin Number LDR module
3v3 1 Vcc
GND 6 GND
GPIO 4 9 SIG

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

 

You may also like