Home ยป 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

 

[codesyntax lang=”python”]

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)

for i in range(0,5):
    print GPIO.input(4)

[/codesyntax]

 

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

[codesyntax lang=”bash”]

sudo python ldr.py

[/codesyntax]

No block and unblock the LDR

 

Links

 

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More