Introduction: IWS-Hack LDR Light Sensors

The purpose of this instructable is to show you how to use a LDR (Light Dependent Resistor) with RaspberryPi to control an output device - in this case an LED.

Some terminology and abbreviations that may help:

LED = Light emitting diode. the long leg is the positive (that is important to get right or the current won't flow).
LDR = Light dependent resistors. These sensors detect the light intensity. COBBLER BOARD = the board that is shaped like a T. This board allows you to set a standard set of numbers to each pin (check the raspberry Pi board. You will see how hard it is to identify the pins (where the ribbon cable goes) GPIO = General Purpose Input/Output. These are the pins on the cobbler board that can be associated as either input or output signals.

AO - Analogue Output. In essence, this sensor sends what it is measuring on to the Raspberry Pi (or Aduino) board. The Analogue needs to be calibrated and while this is a little more complex, it can give you very specific measurements. DO = Digital Out. This option will give you a 0 or 1 binary signal. This means, that it will either tell you it is light or dark, but not an actual measurement.

FOR EXAMPLE: If you use a temperature probe and hook up to Digital Out (DO):

Could be only returned as HOT or Cold (Boolean)

While if you use a temperature probe and hook up to Analogue Out (AO) you get finer resolution: AO could be returned as 0oC or 30oC or 99.8oC

ANOTHER EXAMPLE: If you use a LDR sensor and hook up to Digital Out (DO):

Could be only returned as LIGHT or DARK (Boolean)

While if you use a temperature probe and hook up to Analogue Out (AO) you get finer resolution: AO could be returned as 0 or 10 or 66.7 or 100

Step 1: ​SELECTING AND PURCHASING YOUR LDR

This is an LDR (Light Dependent Resistor). This instructable is written for a LDR purchased from ElectroDragon.

You can purchase it from: http://www.electrodragon.com/product/photoresisto... You might notice on their website the picture may look slightly different and be red, and also have a different potentiometer (also called a POT or variable resistor) that is calibrated with a small screwdriver. In this photo the POT is a blue box with a grey cross.

A POT is a device that allows the user to set or adjust the resistance.

https://en.wikipedia.org/wiki/Potentiometer

Step 2: Connections of the LDR

So what are the connections on the LDR?

VCC = positive terminal (3.3v for raspberrypi or 5v/3.3v for Arduino)

GND = ground/earth/common/negative

AO - Analogue out

DO - Digital out

Be careful, sometimes the pins may be back back to front depending on your supplier.

If you look at the two LDR boards above, the board on the left has the VCC pin on the right hand side. While the right hand board has the VCC on the left hand side. That means the +ve lead could be connected to the AO port.

If you get the pins the wrong way around (with 3.3v), you probably don't need to worry, the LDR may just get hot. Try unplugging and re-wire.

This can be overcome if you know what each pin does (see the list at the top of the page).

Step 3: An Output Device - Wiring Up the LED

The board above has an LED wired into it.

If you look on the Pi board, you will notice how difficult it is to see which pin is which. The blue board is a 'cobbler board'. This cobbler board allows you to see and allocate pin numbers to a convention that makes it easy to set them later when writing your code.

The resistor is a 330ohm resistor. This is again the convention.

The 3.3v is pulled from the cobbler board to the positive bus (the row on the breadboard with a red line).

The ground (also sometimes called earth, common or negative) is pulled down to the negative bus (the row on the breadboard with a black line).

Notice the resistor travels from GPIO#26 to the LED. The program will need to be told to turn the GPIO #26 to HIGH to get the LED to come on.

Step 4: Using an Input Device (LDR) to Control the Output Device (LED)

So, in the previous step, you wired in an LED. You can program this LED to turn on or off. But wouldn't it be nice to be able to turn the LED on or off based on an input signal.

In this step we use the input from the LDR to send a signal to the Pi Board, which then decides if the LED should turn on - based on the input from the LDR.

The above diagram shows you how to wire the LDR into the breadboard.

Remember these wiring diagram are only suggestions. You can wire it other configurations. Consider the following:

1. View the instructable on breadboards if you need to improve your knowledge of breadboards and how the rows and 'bus bars' are connected

2. You have probably noticed, the LDR in the above diagram has only 3 ports - VCC, GND and SIG. We could not find an image of the LDR we used in an earlier step. Don't worry, this SIG port in the bread board above represents either the AO (analogue out) or DO (digital out) ports on the LDR you have bought... The manufacturers just represented both the AO and DO output ports with one SIG port. Here we wired GPIO #21 to the signal. BUT on your board, just wire #21 to either the DO or AO on the sensor - whichever you choose

3. GPIO #21 will need to be configured in the program as an INPUT

4. GPIO #26 will need to be configured in the program as an OUPUT

For experts: When we said above hook up to GPIO #21 and #26... you can use different GPIO pins, but you then need to change these in the code!

LED = Light emitting diode. the long leg is the positive (that is important to get right or the current won't flow).

LDR = Light dependent resistors. These sensors detect the light intensity.

COBBLER BOARD = the board that is shaped like a T. This board allows you to set a standard set of numbers to each pin (check the raspberry Pi board. You will see how hard it is to identify the pins (where the ribbon cable goes)

GPIO = General Purpose Input/Output. These are the pins on the cobbler board that can be associated as either input or output signals.

AO - Analogue Output. In essence, this sensor sends what it is measuring on to the Raspberry Pi (or Aduino) board. The Analogue needs to be calibrated and while this is a little more complex, it can give you very specific measurements.
DO = Digital Out. This option will give you a 0 or 1 binary signal. This means, that it will either tell you it is light or dark, but not an actual measurement.

FOR EXAMPLE:

If you use a temperature probe and hook up to Digital Out (DO): Could be only returned as HOT or Cold (Boolean)

While if you use a temperature probe and hook up to Analogue Out (AO) you get finer resolution: AO could be returned as 0oC or 30oC or 99.8oC

ANOTHER EXAMPLE:

If you use a LDR sensor and hook up to Digital Out (DO): Could be only returned as LIGHT or DARK (Boolean)

While if you use a temperature probe and hook up to Analogue Out (AO) you get finer resolution: AO could be returned as 0 or 10 or 66.7 or 100

Step 5: THE CODE:

import RPi.GPIO as GPIO

LDR_input = 21

LED_output = 26

def set_pins():

GPIO.cleanup()

GPIO.setmode(GPIO.BCM)

GPIO.setup(LDR_input, GPIO.IN, PUD_UP)

GPIO.setup(LED_output, GPIO.OUT)

def it_is_dark():

return GPIO.input (LDR_input) == GPIO.LOW

def led_on():

GPIO.output(LED_output, GPIO.HIGH)

def led_off():

GPIO.output(LED_output, GPIO.LOW)

def demo():

set_pins()

from time import sleep

max_reps = 100

current_rep = 0

while(current_rep < max_reps):

current_rep +=1

if it_is_dark():

print "It's dark dude"

led_on()

else:

print "I've see the light!!!"

led_off()

sleep(.5)

Step 6: The Working Product

Attachments