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

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

 

Related posts

Getting started with Tkinter on the Raspberry Pi

Raspberry Pi and Max7219 7 segment display

Raspberry Pi 2 and an 8×8 LED matrix

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