Skip to content

Instantly share code, notes, and snippets.

@cyrusn
Last active March 10, 2021 05:59
Show Gist options
  • Save cyrusn/2ef380f2e16aeb0e7926e582a994a296 to your computer and use it in GitHub Desktop.
Save cyrusn/2ef380f2e16aeb0e7926e582a994a296 to your computer and use it in GitHub Desktop.
GY906
from smbus2 import SMBus
from time import sleep
# datasheet: https://www.sparkfun.com/datasheets/Sensors/Temperature/MLX90614_rev001.pdf
class GY906():
AMBIENT_TEMP_REGISTER = 0x06
OBJECT1_TEMP_REGISTER = 0x07
def __init__(self, bus, address):
self.bus = bus
self.address = address
def convert_data_to_temp(self, data):
return (data * 0.02) - 273.15
def get_temp(self, register):
data = self.bus.read_word_data(self.address, register)
return self.convert_data_to_temp(data)
@property
def ambient_temp(self):
return round(self.get_temp(GY906.AMBIENT_TEMP_REGISTER), 1)
@property
def object_temp(self):
return round(self.get_temp(GY906.OBJECT1_TEMP_REGISTER), 1)
if __name__ == "__main__":
bus = SMBus(1)
address = 0x5a
sensor = GY906(bus, address)
while True:
print(sensor.ambient_temp, sensor.object_temp)
sleep(1)
bus.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment