0% found this document useful (0 votes)
10 views7 pages

218w1a04n1 AMC

The document discusses interfacing a light dependent resistor (LDR) sensor with a Raspberry Pi. It provides the circuit diagram and Python code to read LDR sensor values and output them. The circuit connects the LDR between the power rail and an RPi input pin. The code uses GPIO to read the pin, calculate an RC time constant based on light level, and print the values.

Uploaded by

Vallabh J
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

218w1a04n1 AMC

The document discusses interfacing a light dependent resistor (LDR) sensor with a Raspberry Pi. It provides the circuit diagram and Python code to read LDR sensor values and output them. The circuit connects the LDR between the power rail and an RPi input pin. The code uses GPIO to read the pin, calculate an RC time constant based on light level, and print the values.

Uploaded by

Vallabh J
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Advanced Microcontrollers

Home Assignment
M.Tejaswi
218W1A04N1 (ECE-D)

Q19) Sketch the LDR analog sensor and write the steps to interface it.

Ans) LDR (Light Dependent Resistor)

The light-dependent resistor (LDR) sensor is used to detect the


intensity of light in the surroundings. The LDR is a device constructed
from a sensitive semiconductor material i.e. cadmium sulfide, which
undergoes a dramatic shift in electrical resistance when exposed to light,
going from several 1000 Ohms in the dark to just a few Ohms, when
illuminated.

Most photoresistive light sensors employ cadmium sulfide(CdS).


However, other semiconductor substrate materials like lead sulfide (PbS),
lead selenide (PbSe), and indium antimony (InSb) can detect light
intensity as well. Since cadmium sulfide has a spectral response curve
similar to the human eye's and can be modulated with a handheld torch, it
is utilized to create photoconductive cells. The peak wavelength at which
it is most sensitive is typically between 560-600nm (nanometers), making
it part of the visible spectrum.

The ORP12 cadmium sulfide photoconductive cell is the most widely


used photoresistive light sensor. This photosensitive resistor's spectral
response is concentrated around 610 nm in the yellow-to-orange part of
the spectrum. When the cell is in the dark, its resistance is extremely high
at around 10M's, but it drops to about 100's when illuminated (lit
resistance). As the resistive path zigzags across the ceramic substrate, the
dark resistance increases and the dark current drops. Because of its low
price and wide range of possible applications, the CdS photocell is
frequently used in auto-dimming systems, light- and dark-sensing
controls for streetlights, and photographic exposure meters.

Below is an illustration of how a light-dependent resistor can be used as a


light-sensitive switch.

This simple circuit for detecting light consists of a relay activated by


exposure to sunlight. The photoresistor LDR and the resistor R1 make up
a potential divider circuit. In the absence of light, the LDR's resistance
rises into the Megaohm (M) range, and as a result, the transistor TR1
receives zero base bias, turning the relay off. The LDR's resistance drops
in response to more light, elevating the base bias voltage at V1. When the
base bias voltage of transistor TR1 reaches a certain threshold, as defined
by the resistance R1 in a potential divider network, the transistor turns
"ON," activating the relay, which controls some external circuitry. With a
return to darkness, the LDR's resistance rises, reducing the transistor's
base voltage and turning "OFF" the transistor and relay at a
predetermined level of illumination established by the potentiometer
circuit.

Changing the relay's "ON" or "OFF" point to a custom brightness is as


simple as swapping out the fixed resistor R1 for a potentiometer VR1.
The switching end of a simple circuit like the one depicted above may
need to be more consistent owing to fluctuations in temperature or supply
voltage. Using the LDR in a "Wheatstone Bridge" configuration and
substituting an Operational Amplifier for the transistor makes it simple to
construct a light-activated circuit with increased sensitivity.

Circuit Diagram of LDR with RPi4

To build the circuit of the LDR sensor with RPi4, follow these
instructions.

1. To begin, attach Pin1 of RPi4 to the breadboard's positive rail.


2. Then, attach pin 6 of RPi4 to the breadboard's ground rail.
3. Next, place the LDR sensor on the board with one end connected to
the power supply's positive rail.
4. Connect the opposite end of the LDR sensor to the Pin4 of RPi4
using a jumper wire.

Python Code for LDR sensor with raspberry Pi

#!/user/local/bin/python

import RPi.GPIO as GPIO

import time

Next, we change the GPIO modes to GPIO.BOARD so that the pins used
in the script match the hardware. One variable only needs to be set
because there is just one input/output pin. If you use a specific GPIO pin,
assign its number to this variable.
GPIO.setmode(GPIO.BOARD)

#define the pin that goes to the circuit

pin_to_circuit = 7

The following function we'll look at is RC time, and it takes a single input:
the circuit's PIN. In this code, we set the value of a variable named count
to zero, and then, when the pin is set to high, we return that number. Our
pin is then configured as an output before being brought low. Then we let
the program rest for ten milliseconds. When this is done, the pin is
converted to an input, and a while loop is started. In this loop, the
capacitor is charged until it is around 3/4 full, at which point the pin
swings high. Once the pin is set to high, we send the count back to the
primary method. This number can be used to toggle an LED, trigger an
action, or be stored to compile data on brightness fluctuations.

def rc_time (pin_to_circuit):


count = 0

#Output on the pin for

GPIO.setup(pin_to_circuit, GPIO.OUT)

GPIO.output(pin_to_circuit, GPIO.LOW)

time.sleep(0.1)

#Change the pin back to the input

GPIO.setup(pin_to_circuit, GPIO.IN)

#Count until the pin goes high

while (GPIO.input(pin_to_circuit) == GPIO.LOW):

count += 1

return count

#Catch when the script is interrupted, clean it up correctly

Try:

# Main loop

while True:

print(rc_time(pin_to_circuit))

except KeyboardInterrupt:

pass

finally:

GPIO.cleanup()

Using git clone, the source code can be downloaded. Here's a


command that will carry out your request.
git clone https://github.com/pimylifeup/Light_Sensor/

cd ./Light_Sensor

The code can also be copied and pasted, but only into a Python script.
When working with Python code, my preferred text editor is nano.

sudo nano light_sensor.py

To save your changes and leave the file, press CTRL+X then Y. Finally,
the following command will execute the code.

sudo python light_sensor.py

Hopefully, you've fixed the script and are now getting readings that
accurately reflect the light levels on the sensor. Be bold about posting a
comment if you need help.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy