/ examples / pi_busio_i2c.py
pi_busio_i2c.py
 1  import time
 2  import sys
 3  import board
 4  import busio
 5  
 6  print("hello blinka!")
 7  
 8  i2c = busio.I2C(board.SCL, board.SDA)
 9  
10  print("I2C devices found: ", [hex(i) for i in i2c.scan()])
11  
12  if not 0x18 in i2c.scan():
13      print("Didn't find MCP9808")
14      sys.exit()
15  
16  
17  def temp_c(data):
18      value = data[0] << 8 | data[1]
19      temp = (value & 0xFFF) / 16.0
20      if value & 0x1000:
21          temp -= 256.0
22      return temp
23  
24  
25  while True:
26      i2c.writeto(0x18, bytes([0x05]), stop=False)
27      result = bytearray(2)
28      i2c.readfrom_into(0x18, result)
29      print(temp_c(result))
30      time.sleep(0.5)