LM75温度传感器Python代码

使用LM75a温度传感器在Jetson nano上做实时温度检测,运行的前提是打开了I2C总线,以及安装smbus2库便于使用I2C通信

sudo raspi-config

sudo pip install smbus2

import smbus2
import time

# I2C地址(LM75A默认地址为0x48,如果已更改地址,请相应更改)
address = 0x48

global te
te=0
# 打开I2C总线
bus = smbus2.SMBus(1)  # 1表示I2C总线1,如果是I2C总线0,则使用0

def read_write_temperature():
    # 读取温度寄存器(0x00),它包含了温度数据
    raw_temperature = bus.read_word_data(address, 0x00)

    # 将低8位和高8位数据进行合并
    temperature = ((raw_temperature << 8) & 0xFF00) | ((raw_temperature >> 8) & 0x00FF)

    # 转换为摄氏度温度
    temp = (temperature / 32.0) / 8.0
    print(f"当前温度:{temp:.2f} °C")
    time.sleep(1)  # 每秒更新一次温度
    return temp
def compare():
    te = read_write_temperature()  #t代表温度,用来判断温度是否到达阈值
    if (te >= 40):
        print("温度过阈值")
        time.sleep(3)
if __name__ == '__main__':
    try:
        while (te<40):
            compare()
       
    except KeyboardInterrupt:
        pass

# 关闭I2C总线
bus.close()


 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值