树梅派学习 18. 烟雾传感器实验

Posted 编程圈子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了树梅派学习 18. 烟雾传感器实验相关的知识,希望对你有一定的参考价值。

烟雾传感器原理图:

接线图:

实体接线图:

代码

#!/usr/bin/env python
#------------------------------------------------------
#
#       This is a program for PCF8591 Module.
#
#       Warnng! The Analog input MUST NOT be over 3.3V!
#    
#       In this script, we use a poteniometer for analog
#   input, and a LED on AO for analog output.
#
#       you can import this script to another by:
#   import PCF8591 as ADC
#   
#   ADC.Setup(Address)  # Check it by sudo i2cdetect -y -1
#   ADC.read(channal)   # Channal range from 0 to 3
#   ADC.write(Value)    # Value range from 0 to 255     
#
#------------------------------------------------------
import smbus
import time

# for RPI version 1, use "bus = smbus.SMBus(0)"
bus = smbus.SMBus(1)

#check your PCF8591 address by type in 'sudo i2cdetect -y -1' in terminal.
def setup(Addr):
    global address
    address = Addr

def read(chn): #channel
    if chn == 0:
        bus.write_byte(address,0x40)
    if chn == 1:
        bus.write_byte(address,0x41)
    if chn == 2:
        bus.write_byte(address,0x42)
    if chn == 3:
        bus.write_byte(address,0x43)
    bus.read_byte(address) # dummy read to start conversion
    return bus.read_byte(address)

def write(val):
    temp = val # move string value to temp
    temp = int(temp) # change string to integer
    # print temp to see on terminal else comment out
    bus.write_byte_data(address, 0x40, temp)

if __name__ == "__main__":
    setup(0x48)
    while True:
        print 'AIN0 = ', read(0)
        print 'AIN1 = ', read(1)
        tmp = read(0)
        tmp = tmp*(255-125)/255+125 # LED won't light up below 125, so convert '0-255' to '125-255'
        write(tmp)
#       time.sleep(0.3)

运行效果:

以上是关于树梅派学习 18. 烟雾传感器实验的主要内容,如果未能解决你的问题,请参考以下文章

arduino中为啥要给烟雾传感器连一个电阻

Linux嵌入式学习-烟雾传感器驱动-字符设备驱动-按键驱动

求stc89C52型单片机与DS18B20传感器和MQ-2烟雾传感器电路连接图以及原理

烟雾传感器

社区安防系统

一种烟雾传感器的光电式迷宫老化探测数据自动校正技术