代码如下:
from machine import Pin, I2C import time
i2c = I2C(1, scl=Pin(23), sda=Pin(22)) address = 0x5A
while True:
i2c.writeto(address, bytearray([0x07]) time.sleep_ms(500) # 读取温度数据 data = i2c.readfrom(address, 3) temp_raw = (data[0] << 8) + data[1] temp_celsius = (temp_raw * 0.02) - 273.15 temp_fahrenheit = (temp_celsius * 9 / 5) + 32 print("Temperature: {:.2f}°C / {:.2f}°F".format(temp_celsius, temp_fahrenheit)) time.sleep(1)