Skip to content

Instantly share code, notes, and snippets.

@aprovecharLab
Created March 3, 2019 17:21
Show Gist options
  • Save aprovecharLab/5f704971ac28e4112c41922352a47997 to your computer and use it in GitHub Desktop.
Save aprovecharLab/5f704971ac28e4112c41922352a47997 to your computer and use it in GitHub Desktop.
Reading the ADT7410 Temperature Sensor
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reading the ADT7410 Temperature Sensor\n",
"### with FTDI USB/i2c\n",
"- AdaFruit ADT7410 HIGH ACCURACY I2C TEMPERATURE SENSOR BREAKOUT BOARD: https://www.adafruit.com/product/4089\n",
"- AdaFruit FT232H USB to i2c interface: https://www.adafruit.com/product/2264\n",
"- PyFtdi library: http://eblot.github.io/pyftdi/index.html"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from pyftdi.i2c import I2cController\n",
"from os import environ\n",
"\n",
"import math\n",
"import time\n",
"\n",
"import struct"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"class FTDIi2c:\n",
" def __init__(self):\n",
" self.i2c = I2cController()\n",
" url = environ.get('FTDI_DEVICE', 'ftdi://ftdi:232h/1')\n",
" self.i2c.configure(url, clockstretching=False)\n",
"\n",
" def connect(self,address):\n",
" port = self.i2c.get_port(address)\n",
" return port\n",
"\n",
" def close(self):\n",
" # Close the I2C connection\n",
" self.i2c.terminate()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"class i2cTEMP:\n",
"\n",
" # adt7410 I2C Address with pins a0:low, a1:low\n",
" # adt7410_ADDRESS = 0x48\n",
" \n",
" # adt7410 Register map \n",
" adt7410_MSB = 0x00\n",
" adt7410_LSB = 0x01\n",
" adt7410_STATUS = 0x02\n",
" adt7410_CONFIG = 0x03\n",
" adt7410_ID = 0x0B\n",
" adt7410_SWRST = 0x2F\n",
" \n",
" # Configuration Parameters\n",
" # BIT13_RESOLUTION : 0b00000000\n",
" BIT13_RESOLUTION = 0x00\n",
" # BIT16_RESOLUTION : 0b10000000\n",
" BIT16_RESOLUTION = 0x80\n",
"\n",
" # OP_MODE_CONTINUOUS : 0b00000000\n",
" OP_MODE_CONTINUOUS = 0x00\n",
" # OP_MODE_ONESHOT : 0b00100000\n",
" OP_MODE_ONESHOT = 0x20\n",
" # OP_MODE_SPS : 0b01000000\n",
" OP_MODE_SPS = 0x40\n",
" # OP_MODE_SHUTDOWN : 0b01100000\n",
" OP_MODE_SHUTDOWN = 0x60\n",
"\n",
" # INTERRUPT_MODE : 0b00000000\n",
" INTERRUPT_MODE = 0x00\n",
" # COMPARATOR_MODE : 0b00010000\n",
" COMPARATOR_MODE = 0x10\n",
"\n",
" # INT_LOW : 0b00000000\n",
" INT_LOW = 0x00\n",
" # INT_HIGH : 0b00001000\n",
" INT_HIGH = 0x08\n",
"\n",
" # CT_LOW : 0b00000000\n",
" CT_LOW = 0x00\n",
" # CT_HIGH : 0b00000100\n",
" CT_HIGH = 0x04\n",
"\n",
" # BIT16_OP_MODE_1FAULT : 0b00000000\n",
" BIT16_OP_MODE_1FAULT = 0x00\n",
" # BIT16_OP_MODE_2FAULT : 0b00000001\n",
" BIT16_OP_MODE_2FAULT = 0x01\n",
" # BIT16_OP_MODE_3FAULT : 0b00000010\n",
" BIT16_OP_MODE_3FAULT = 0x02\n",
" # BIT16_OP_MODE_4FAULT : 0b00000011\n",
" BIT16_OP_MODE_4FAULT = 0x03\n",
"\n",
" deviceID = 0xC8\n",
"\n",
" adt7410_resolution = 1.0/128.0 # degC/lsb\n",
"\n",
" ###########################################################################\n",
" def __init__(self,port):\n",
" self.port = port\n",
" \n",
" def configure(self):\n",
" # Perform a software reset\n",
" self.port.write_to(self.adt7410_SWRST, 0)\n",
" \n",
" # get status\n",
" status = self.ready()\n",
" print ('status: ', hex(status))\n",
" if (status & 0x80):\n",
" print ('not ready')\n",
" \n",
" # configure\n",
" deviceConfig = self.port.read_from(self.adt7410_CONFIG, 1)[0]\n",
" deviceConfig = deviceConfig | self.BIT16_RESOLUTION\n",
" deviceConfig = deviceConfig | self.OP_MODE_CONTINUOUS\n",
" deviceConfig = deviceConfig | self.INTERRUPT_MODE\n",
" deviceConfig = deviceConfig | self.CT_LOW\n",
" deviceConfig = deviceConfig | self.INT_LOW\n",
" self.port.write_to(self.adt7410_CONFIG, [deviceConfig])\n",
" \n",
" # check configuration\n",
" deviceConfig = self.port.read_from(self.adt7410_CONFIG, 1)[0]\n",
" time.sleep(0.1)\n",
" return deviceConfig\n",
"\n",
" def device_ID(self):\n",
" device_id = self.port.exchange([self.adt7410_ID], 1)[0]\n",
" device_id = device_id & 0xF8\n",
" time.sleep(0.1)\n",
" return int(device_id)\n",
"\n",
" def ready(self):\n",
" status = self.port.exchange([self.adt7410_STATUS], 1)[0]\n",
" if status & 0x80:\n",
" return False\n",
" else:\n",
" return True\n",
"\n",
" ###########################################################################\n",
" # Read the temperature\n",
" def read(self):\n",
" raw = self.port.exchange([self.adt7410_MSB], 2)\n",
" if raw == []:\n",
" return []\n",
" result = float(struct.unpack('>h', raw)[0]) * self.adt7410_resolution # Celsius\n",
"# result = result * 9.0/5.0 + 32.0 # Fahrenheit\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"status: 0x0\n",
"Device Config: b'\\x80'\n",
"Device ID: 0xc8\n",
" \n",
"T: 20.171875\n",
"T: 20.1875\n",
"T: 20.1796875\n",
"T: 20.1796875\n",
"T: 20.1640625\n",
"T: 20.171875\n",
"T: 20.1875\n",
"T: 20.15625\n",
"T: 20.1796875\n",
"T: 20.1875\n",
"T: 20.1953125\n",
"T: 20.1875\n",
"T: 20.1640625\n",
"T: 20.1640625\n",
"T: 20.15625\n",
"T: 20.1484375\n",
"T: 20.1953125\n",
"T: 20.1640625\n",
"T: 20.203125\n",
"T: 20.1953125\n"
]
}
],
"source": [
"i2c = FTDIi2c()\n",
"\n",
"port_adt7410 = i2c.connect(0x48)\n",
"TEMP = i2cTEMP(port_adt7410)\n",
"\n",
"deviceConfig = TEMP.configure()\n",
"print ('Device Config: ',bytes([deviceConfig]))\n",
"id = TEMP.device_ID()\n",
"print ('Device ID: ',hex(id))\n",
"print(' ')\n",
"\n",
"for i in range(20):\n",
" if TEMP.ready():\n",
" temp = TEMP.read()\n",
" print ('T: ',temp)\n",
" else:\n",
" print ('not ready')\n",
" time.sleep(0.5)\n",
"\n",
"i2c.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment