218w1a04n1 AMC
218w1a04n1 AMC
Home Assignment
M.Tejaswi
218W1A04N1 (ECE-D)
Q19) Sketch the LDR analog sensor and write the steps to interface it.
To build the circuit of the LDR sensor with RPi4, follow these
instructions.
#!/user/local/bin/python
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)
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.
GPIO.setup(pin_to_circuit, GPIO.OUT)
GPIO.output(pin_to_circuit, GPIO.LOW)
time.sleep(0.1)
GPIO.setup(pin_to_circuit, GPIO.IN)
count += 1
return count
Try:
# Main loop
while True:
print(rc_time(pin_to_circuit))
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()
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.
To save your changes and leave the file, press CTRL+X then Y. Finally,
the following command will execute the code.
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.