码丁实验室,一站式儿童编程学习产品,寻地方代理合作共赢,微信联系:leon121393608。
温度传感器是一种检测温度变化的装置。它们的核心部件是热敏电阻。
Introduction
该模块是基于热敏电阻原理开发的,其电阻随环境温度的变化而变化很大。当温度升高时,阻值减小;当温度降低时,阻值就会增加。它能实时检测周围的温度变化。

此外,我们需要使用ADC0832来将模拟信号转换成数字信号,用于树莓派采集。
What you will need
- 树莓派
 - 网线
 - 温度传感器
 - ADC0832
 - 杜邦线
 
What you will do
1:构建电路

2:编辑并测试代码
现在,在模块上触摸热敏电阻,你可以看到当前的温度值显示在屏幕上并相应改变。

#!/usr/bin/env python
import ADC0832
import time
import math
def init():
    ADC0832.setup()
def loop():
    while True:
        analogVal = ADC0832.getResult()
        Vr = 5 * float(analogVal) / 255
        Rt = 10000 * Vr / (5 - Vr)
        temp = 1/(((math.log(Rt / 10000)) / 3950) + (1 / (273.15+25)))
        temp = temp - 273.15
        print 'temperature = %d C' % temp
        time.sleep(0.2)
if __name__ == '__main__':
    init()
    try:
        loop()
    except KeyboardInterrupt: 
        ADC0832.destroy()
        print 'The end !'

