树莓派连接光敏电阻传感器通过模数转换显示光照强度的python代码
时间: 2023-06-11 18:05:49 浏览: 316
STM32通过ADC1读取光敏电阻的值转换光照强度.pdf
以下是树莓派连接光敏电阻传感器通过模数转换显示光照强度的 python 代码:
```python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN)
voltage = 3.3
def rc_time(pin):
count = 0
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.LOW)
time.sleep(0.1)
GPIO.setup(pin, GPIO.IN)
while (GPIO.input(pin) == GPIO.LOW):
count += 1
return count
try:
while True:
light_intensity = rc_time(18)
resistance = (voltage * 10000 - voltage * light_intensity) / light_intensity
print("Light Intensity: " + str(light_intensity))
print("Resistance: " + str(resistance))
time.sleep(1)
except KeyboardInterrupt:
pass
GPIO.cleanup()
```
希望对你有所帮助!
阅读全文