赞
踩
使用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()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。