Skip to main content

Sensing Light

A Shopping List of Parts

1. LDR: cost less than ₹10
2. Resistor: 2.2KΩ , less than ₹1
3. Capacitor: 300nF, costs less than less than ₹5
4. Full size Breadboard: ₹ 80
5. Jumper wires: Female to female, male to female

This work is only costs less than ₹100 if you are an electronic hobbyist. Since an electronic hobbyist have atleast the components like LDR, breadboard, jumper wires etc. on his hand.

LDR

LDR

LDR is also known as Light Dependent Resistor or Photo Resistor or Photoconductive Cell or Photo Detector. The resistance of Photoconductive Cell decreases as the intensity of light increases. Photo Detectors are made up of semiconducting materials which possesses energy band equivalent to the order of that of photon. Cadmium Sulfide (CdS) is a potential candidate of photo detector whose Forbidden Energy Gap (FEG) ≅ energy of light. You can clearly see the CdS tracks on the top of LDR.

Circuit

Circuit Diagram
  1. Connect 3.3v Raspberry Pi pin(Pin 1) to positive rail of breadboard.
  2. Connect pin 6 of Raspberry Pi to negative rail of breadboard.
  3. Connect resistor, LDR and capacitor in series on terminal strip of breadboard as shown in figure.
  4. Connect broken (connectionless) end of resistor to positive rail of breadboard.
  5. Connect broken end of 330nF capacitor to negative rail of breadboard.
  6. The node joining to the LDR and 330nF capacitor to GPIO #4 using jumper wire.
This diagram is created using an older version of Fritzing. That is why the Raspberry Pi model B (Rev 2) 26 pin version is shown in the image. But the program is working on both Raspberry Pi 2 and RPi3.

The Fritzing is an no-profit open source software that allows you to draw the breadboard, schematic, and PCB of a circuit. It is a very straight forward application and it has a wide range of graphical symbols.`

if you have any doubt about about Raspberry Pi GPIO pins click here. In this blog chipprogrammer I used pin n for representing physical numbering of GPIO pins and GPIO #n to represent the BCM numbering of GPIO header where n is the number. However you should take care about numbering of GPIO. The above link will redirect to an interactive pin out of Raspberry Pi provided by Github.

Working of Circuit

You can also use 5V GPIO hader for the working of circuit. Here I am using 3.3V GPIO header for protecting the GPIO pins. The 2.2 KΩ resistor is also used for protecting the GPIO pins. Here we are connecting LDR for sensing the light. As I mensioned earlier the LDR is a variable resistor and when light increaes the resistance will decrease. If you are using a flash light to check the working of circuit you may able to see the RC time constant of the circuit approaches zero gradually. If you were used 5V GPIO header as your Vcc your GPIO pins may get damaged. Since your GPIO pins can only tolerate 3.3V, since it is working on 3.3V CMOS logic.

In the world of electronics or electrical engineering there is only two outputs: analog or digital. As we all know that the change in resistance with respect to change in intensity of light is a an analog value. But the Raspberry Pi has no inbuilt or on-chip analog-to-digital converter(ADC). We can overcome this problem using the 330nF capacitor. You may interested to know how that is possible? The analog I/O with Raspberry Pi is little trickier. When we talk about voltages and Raspberry Pi, the voltages less than 1.8V is considered off and voltages greater than 1.8V is considered as on. It is shown on the figure below.
Analog I/O of Raspberry Pi(courtesy Raspberry Pi learning)
The capacitor is just like your pocket and it wiill fill rapidly, if your time is good and it will become empty, if your fate and time is bad. It will charge faster if the variable resistance of LDR is lower due to the RC time constant is low. Also it will charge like a snail when the RC time constant is high at dark due to resistance of LDR become high. The time constant is given by:
て=RC

For LDR, resistance at dark is approximately 1㏁ and hence for this resistance and 330nF capacitance て= 330 msec. For LDR, resistance at light is 10㏀ and hence て=33msec. You can calculate this resistance using online time constant calculator provided by digikey.

The time module of the Python makes the program trickier and will help us to measure this time constants.

There is two methods to find out the light levels received by LDR. The first method is some complicated and in this method the Python time and RCtime of RPi.GPIO module is directly used for this program. The first program is abstracted in the gpiozero library and hence it easy to handle second program.

Method 1


import RPi.GPIO as GPIO, time
# Tell the GPIO library to use
# Broadcom GPIO references
GPIO.setmode(GPIO.BCM)
# Define function to measure charge time
def RCtime(PiPin):
    measurement = 0
    # Discharge capacitor
    GPIO.setup(PiPin, GPIO.OUT)
    GPIO.output(PiPin, GPIO.LOW)
    time.sleep(0.1)

    GPIO.setup(PiPin, GPIO.IN)
    # Count loops until voltage across
    # capacitor reads high on GPIO
    while (GPIO.input(PiPin) == GPIO.LOW):
        measurement += 1
    return measurement

# Main program loop
while True:
    print (RCtime(4)) # Measure timing using GPIO4
This program is available on our github repository and it is named as ldr_spy.py. Cloning of these programs are discussed at the end of this session.

Method 2


from gpiozero import LightSensor, Buzzer
ldr = LightSensor(4) # alter if using a different pin
while True:
    print(ldr.value)

The program shown in method 2 is named as ldr_RC_time_check.py in our GitHub repository. You have to run these Python codes and observe the outputs using following commands:

$sudo python ldr_spy.py
$sudo Python ldr_RC_time_check.py
Note: Don't run codes simultaneously.

Immortal Brightness

You have to note down the values of these RC time constants during the dark and bright circumstances. These values will help you to turn on and off light by comparing real world situation. However I am not writing these programs to turn on and off light. You can change values in the RCtime((4)) in the if and else statements of the Python code corresponding to our GitHub repository named as newldr.py. Also you can change ldr.value in the if and eligible statements of our program (ldrorg.py) from GitHub repository to turning your LED on at night.
Circuit for turn on LED at dark

The circuit diagram to turn on LED at night is shown above. There is no change in the above circuitry. In this circuitry we connected LED to the GPIO #17 through a 220Ω series resistor. You can use following commands to clone all programs from our GitHub repository:

$cd ~
$ git clone git://github.com/chipprogrammers/ldr.git
Then type following commands to turn on LED at dark circumstances.

$cd ldr
$sudo python newldr.py
                 Or
$sudo python ldrorg.py
You can also check the working of program by masking LDR with your finger tip. Don't forgot to run ldr_spy.py which is explained in method 1 to note down the RCtime(4) value to change the value on newldr.py. Also you have to run ldr_RC_time_check.py to note down the values of ldr.value explained in method 2 for change these values program ldrorg.py.

Click here to view all four programs explained in this post.

Comments

Post a Comment

Popular posts from this blog

8051 Simple Programs

Sum of 8-bit Numbers Stored in Memory Here  we will discuss about 8051 programs but we can't discuss about all of the 8051 instructions. For programming 8051 we should know about assembler directives as well as instruction set. Click  here to download Atmel c51 user guide that will discuss about 8051(c51 family microcontroller) instruction set, assembler directives, c51 cross assembler from Atmel.  Program ORG 00H MOV R0,#50H   ; get memory location in memory pointer R0 MOV R1,#51H   ; get memory location on memory pointer register R1 MOV A,@R0       ; get content of memory location 50H to accumulator ADD A,@R1        ; add content of A with content of memory location 51H and store result in A MOV R0,#52H    ; get 52H to memory pointer R0 MOV@R0,A         ; copy content of A to memory location 52H END Add 16-bit Numbers ...

Introduction to 8051 embedded C

For 8051 we need to include the file reg51.h. This file contains the all the definitions of 8051 registers. With this information C compiler produces hex file that can be downloaded into the ROM of the microcontroller. It is important to note that the size of the hex file produced by the assembly language is much larger than the hex file produced by C compiler. Apart from this fact, there is many reasons for writing programs in C instead of assembly: ●It is much easier and less time consuming to write programs in C assembly. ●C is more flexible; it is easier to modify and update. ●Programming in C allows to use code available in function libraries. ●Program written inC for one microcontroller is portable to other microcontrollers with little or no modifications. Data Types in 8051 Embedded C The table shown below lists the data types that are available in typical C51 compiler. The gives information about the size of the data variable and it's value range. Data type ...

Frequency of Oscillation of RC Phase Shift Oscillator

Derivation of Frequency of Oscillation We have to find out the transfer function of RC feedback network. Feedback Circuit of RC Phase Shift Oscillator Applying KVL to various loops on the figure, we get, $$I_1 \left(R+\frac{1}{j \omega C }\right) -I_2R=V_i \text{ ....(1)}$$ $$-I_1R+I_2\left (2R+\frac {1}{j\omega C}\right)-I_3R=0\text{ ... (2)}$$ $$0-I_2R+I_3\left(2R+ \frac{1}{j\omega C}\right)=0\text{ ...(3)}$$ Replacing \(j\omega\) with \(s\) and writing equations in the matrix form, $$\begin{bmatrix}R+\frac{1}{sC} & -R & 0 \\-R & 2R+\frac{1}{sC} & -R \\0 & -R & 2R+\frac{1}{2sC} \end{bmatrix}\begin{bmatrix}I_1\\I_2\\I_3\end{bmatrix}=\begin{bmatrix}V_i\\0\\0\end{bmatrix}$$ Using Cramer's rule to find out \(I_3\), $$\text{Let, }D=\begin{bmatrix}R+\frac{1}{sC} & -R & 0 \\-R & 2R+\frac{1}{sC} & -R \\0 & -R & 2R+\frac{1}{2sC} \end{bmatrix}$$ \(|D|=\begin{vmatrix}R+\frac{1}{sC} & -R & 0 \\-R & 2R+\frac{1}{...