LM75温度传感器Python代码
使用LM75a温度传感器在Jetson nano上做实时温度检测,运行的前提是打开了I2C总线,以及安装smbus2库便于使用I2C通信
sudo raspi-config
sudo pip install smbus2
import smbus2
import time# I2C地址(LM75A默认地址为0x48,如果已更改地址,请相应更改)
address = 0x48global te
te=0
# 打开I2C总线
bus = smbus2.SMBus(1) # 1表示I2C总线1,如果是I2C总线0,则使用0def 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.0print(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()
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
