solved:MCP2221 USB to I2C bridge with MCP9808 sensor conversion problem, Python. - Page 1
Forum section: electronics / beginners

author: lk
author_rank: Regular Contributor
timestamp: March 17, 2019, 06:12:23 pm
content: Greetings,just for the fun of it i wanted to see if i could make a small usb temperature sensor, using the MCP2221A Usb to I2C bridge and a MCP9808 temperature sensor. I think i have the hardware working, i can read the data for the temperature. However I have trouble with the conversion of the temperature, i have included my code and the schematic, and the output. The temperature output contains two bytes, a high and a low. The high byte contains alerts[b15-b13] and sign and the top part of the temperature is in [b11-b8], as far i can see, that is only used at temperatures over 30C. The lower byte is all temperature, and it should be set at 0.0625C per bit. But this seems odd, when i calculate, i get a value of 6.375 for a temperature of ~23C.Anyone that can see what im doing wrong?I have attached the schematic for the sensor board, and the datasheet for the MCP9808 sensor. Not for the MCP2221a as communication seems to workbrLasseCode:############################################################## MIT License ## Copyright (c) 2017 Yuta KItagami ##############################################################from PyMCP2221A import PyMCP2221Aimport timeprint('-'*50)print('MCP2221(A) with MCP9808 ')print('-'*50)mcp2221 = PyMCP2221A.PyMCP2221A()mcp2221.Reset()mcp2221 = PyMCP2221A.PyMCP2221A()usbdata= * 2usbdata = 0x05#usbdata = 0x05addressdata= * 2mcp2221.I2C_Init()print()#Point to resolution register#usbdata = 0x08#usbdata = 0x03#mcp2221.I2C_Write(0x18,usbdata)#Setting resolution at 0.625 as per the datasheet page 29usbdata = 0x08usbdata = 0x03mcp2221.I2C_Write(0x18,usbdata)resolution = mcp2221.I2C_Read(0x18,1)print("Resolution Data: " + str(resolution))#check that the chip has the right Manfucaturer data, MCP9804 should be 0x54 or 84 in DECaddressdata = 0x06mcp2221.I2C_Write(0x18,addressdata)mfgdata = mcp2221.I2C_Read(0x18,3)print("MFG Data: " + str(mfgdata))# Point to temperature register and read temperature dataaddressdata = 0x05mcp2221.I2C_Write(0x18,addressdata)rdata = mcp2221.I2C_Read(0x18,3)#Below is the dataconversion datasheet looks like C but i have "ported" it to Pythonprint("Read Data: " + str(rdata))uppertempbyte = rdata#Zeroing Alert Bits.print("Read Data Upper pre mask " + str(uppertempbyte))uppertempbyte = uppertempbyte & 0x1fprint("Read Data Upper post mask: " + str(uppertempbyte))lowertempbyte = rdataif ((uppertempbyte & 0x10) == 0x10):uppertempbyte = uppertempbyte & 0x0ftempperatureminus = 256 - ( uppertempbyte * 16 + lowertempbyte /16)print("Negative Temperature: " + str(tempperatureminus))else:tempperatureplus = ( uppertempbyte * 16 + lowertempbyte /16)print("Positive Temperature: " + str(tempperatureplus))# At ~23c i get a positive temp of 6.375print("Read Data Upper: " + str(uppertempbyte))print("Read Data Lower: " + str(lowertempbyte))Quotelink to mcp9908 datasheethttp://ww1.microchip.com/downloads/en/DeviceDoc/25095A.pdf

author: lk
author_rank: Regular Contributor
timestamp: March 18, 2019, 01:34:30 pm
content: I assembled 2 more boards, and now it works, looks like the sensor on the first board was defective.