{"id":982,"date":"2013-08-21T20:28:02","date_gmt":"2013-08-21T20:28:02","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=982"},"modified":"2024-11-20T17:24:35","modified_gmt":"2024-11-20T17:24:35","slug":"using-the-i2c-interface-2","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-python\/i2c-programming-in-python\/using-the-i2c-interface-2","title":{"rendered":"Using the I2C Interface"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Enabling The I2C Port<\/h4>\n\n\n\n<p>\nThe I2C port needs to be enabled in Rasbian before it can be used. See <a href=\"https:\/\/raspberry-projects.com\/pi\/pi-operating-systems\/raspbian\/io-pins-raspbian\/i2c-pins\">here<\/a>.\n<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Checking For Connected Devices<\/h4>\n\n\n\n<p>\nAt the command prompt type one of these depending on whether you are using the I2C0 or I2C1 port:\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo i2cdetect -y 0\n\/\/or\nsudo i2cdetect -y 1<\/code><\/pre>\n\n\n\n<p>\nThe 7 bit I2C address of all found devices will be shown (ignoring the R\/W bit, so I2C address 0000 0110 is displayed as hex&nbsp;03).\n<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">SMBus (System Management Bus) Functions<\/h4>\n\n\n\n<p>SMBus (System Management Bus) is a subset from the I2C protocol<br>When writing a driver for an I2C device try to use the SMBus commands if possible (if the device uses only that subset of the I2C protocol) as it makes it possible to use the device driver on both SMBus adapters and I2C adapters.<\/p>\n\n\n\n<p>\nNote address is the 7 bit address&nbsp;excluding the read \/ write bit (it will be shifted left 1 bit when added to the read\/write bit)\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>long write_quick(int addr)<\/code><\/pre>\n\n\n\n<p>\nSend only the read \/ write bit\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>long read_byte(int addr)<\/code><\/pre>\n\n\n\n<p>\nRead a single byte from a device, without specifying a device register.\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>long write_byte(int addr,char val)<\/code><\/pre>\n\n\n\n<p>\nSend a single byte to a device\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>long read_byte_data(int addr,char cmd)<\/code><\/pre>\n\n\n\n<p>\nRead Byte Data transaction.\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nlong write_byte_data(int addr,char cmd,char val)<\/code><\/pre>\n\n\n\n<p>\nWrite Byte Data transaction.\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>long read_word_data(int addr,char cmd)<\/code><\/pre>\n\n\n\n<p>\nRead Word Data transaction.\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>long write_word_data(int addr,char cmd,int val)<\/code><\/pre>\n\n\n\n<p>\nWrite Word Data transaction.\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>long process_call(int addr,char cmd,int val)<\/code><\/pre>\n\n\n\n<p>\nProcess Call transaction.\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>long&#91;] read_block_data(int addr,char cmd)<\/code><\/pre>\n\n\n\n<p>\nRead Block Data transaction. &nbsp;&nbsp; &nbsp;\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>write_block_data(int addr,char cmd,long vals&#91;])<\/code><\/pre>\n\n\n\n<p>\nWrite up to 32 bytes to a device. &nbsp;This fucntion adds an initial byte indicating the length of the vals array before the valls array. &nbsp;Use write_i2c_block_data instead!\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>long&#91;] block_process_call(int addr,char cmd,long vals&#91;])<\/code><\/pre>\n\n\n\n<p>\nBlock Process Call transaction. &nbsp;&nbsp; &nbsp;\n<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">I2C Access Functions<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>long&#91;] read_i2c_block_data(int addr,char cmd)<\/code><\/pre>\n\n\n\n<p>\nBlock Read transaction.\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>write_i2c_block_data(int addr,char cmd,long vals&#91;])<\/code><\/pre>\n\n\n\n<p>\nBlock Write transaction.\n<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Code Example<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/python\n\nimport smbus\n\nbus = smbus.SMBus(1)    # 0 = \/dev\/i2c-0 (port I2C0), 1 = \/dev\/i2c-1 (port I2C1)\n\nDEVICE_ADDRESS = 0x15      #7 bit address (will be left shifted to add the read write bit)\nDEVICE_REG_MODE1 = 0x00\nDEVICE_REG_LEDOUT0 = 0x1d\n\n#Write a single register\nbus.write_byte_data(DEVICE_ADDRESS, DEVICE_REG_MODE1, 0x80)\n\n#Write an array of registers\nledout_values = &#91;0xff, 0xff, 0xff, 0xff, 0xff, 0xff]\nbus.write_i2c_block_data(DEVICE_ADDRESS, DEVICE_REG_LEDOUT0, ledout_values)\n\n#Write multiple bytes without creating an array\nbus.write_block_data(DEVICE_ADDRESS, 0x01, &#91;0x11, 0x23, 0x34, 0x14])\n<\/code><\/pre>\n\n\n\n<p>\n&nbsp;\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Enabling The I2C Port The I2C port needs to be enabled in Rasbian before it can be used. See here. Checking For Connected Devices At the command prompt type one of these depending on whether you are using the I2C0 or I2C1 port: The 7 bit I2C address of all found devices will be shown [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[],"class_list":["post-982","post","type-post","status-publish","format-standard","hentry","category-i2c-programming-in-python"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/982","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=982"}],"version-history":[{"count":11,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/982\/revisions"}],"predecessor-version":[{"id":3795,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/982\/revisions\/3795"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}