{"id":473,"date":"2012-10-17T12:25:08","date_gmt":"2012-10-17T12:25:08","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=473"},"modified":"2015-12-23T14:42:15","modified_gmt":"2015-12-23T14:42:15","slug":"using-the-i2c-interface","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/i2c\/using-the-i2c-interface","title":{"rendered":"Using the I2C interface"},"content":{"rendered":"<p>\n&nbsp;\n<\/p>\n<h4>\nEnabling The I2C Port<br \/>\n<\/h4>\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<h4>\nChecking For Connected Devices<br \/>\n<\/h4>\n<p>\nInstall the I2C tools (i2c-tools is a set of I&sup2;C programs that make it easy to debug I&sup2;C devices without having to write any code):\n<\/p>\n<pre>\r\n<code>\r\nsudo apt-get install i2c-tools\r\nsudo apt-get update\r\n<\/code><\/pre>\n<p>\nAt the command prompt type one of these depending on whether you are using the I2C0 or I2C1 port:\n<\/p>\n<pre>\r\n<code>\r\nsudo i2cdetect -y 0\r\n\/\/or\r\nsudo i2cdetect -y 1\r\n<\/code><\/pre>\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<h4>\nUseful Resources<br \/>\n<\/h4>\n<p>\n<a href=\"http:\/\/www.skpang.co.uk\/blog\/archives\/575\" target=\"_blank\">http:\/\/www.skpang.co.uk\/blog\/archives\/575<\/a>\n<\/p>\n<p>\n<a href=\"http:\/\/www.hobbytronics.co.uk\/raspberry-pi-raspbian-distro\" target=\"_blank\">http:\/\/www.hobbytronics.co.uk\/raspberry-pi-raspbian-distro<\/a>\n<\/p>\n<h4>\nReading And Writing I2C<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n#include &lt;unistd.h&gt;\t\t\t\t\/\/Needed for I2C port\r\n#include &lt;fcntl.h&gt;\t\t\t\t\/\/Needed for I2C port\r\n#include &lt;sys\/ioctl.h&gt;\t\t\t\/\/Needed for I2C port\r\n#include &lt;linux\/i2c-dev.h&gt;\t\t\/\/Needed for I2C port\r\n<\/code><\/pre>\n<pre>\r\n<code>\r\n\tint file_i2c;\r\n\tint length;\r\n\tunsigned char buffer[60] = {0};\r\n\r\n\t\r\n\t\/\/----- OPEN THE I2C BUS -----\r\n\tchar *filename = (char*)&quot;\/dev\/i2c-1&quot;;\r\n\tif ((file_i2c = open(filename, O_RDWR)) &lt; 0)\r\n\t{\r\n\t\t\/\/ERROR HANDLING: you can check errno to see what went wrong\r\n\t\tprintf(&quot;Failed to open the i2c bus&quot;);\r\n\t\treturn;\r\n\t}\r\n\t\r\n\tint addr = 0x5a;          \/\/&lt;&lt;&lt;&lt;&lt;The I2C address of the slave\r\n\tif (ioctl(file_i2c, I2C_SLAVE, addr) &lt; 0)\r\n\t{\r\n\t\tprintf(&quot;Failed to acquire bus access and\/or talk to slave.\\n&quot;);\r\n\t\t\/\/ERROR HANDLING; you can check errno to see what went wrong\r\n\t\treturn;\r\n\t}\r\n\t\r\n\t\r\n\t\/\/----- READ BYTES -----\r\n\tlength = 4;\t\t\t\/\/&lt;&lt;&lt; Number of bytes to read\r\n\tif (read(file_i2c, buffer, length) != length)\t\t\/\/read() returns the number of bytes actually read, if it doesn&#39;t match then an error occurred (e.g. no response from the device)\r\n\t{\r\n\t\t\/\/ERROR HANDLING: i2c transaction failed\r\n\t\tprintf(&quot;Failed to read from the i2c bus.\\n&quot;);\r\n\t}\r\n\telse\r\n\t{\r\n\t\tprintf(&quot;Data read: %s\\n&quot;, buffer);\r\n\t}\r\n\r\n\t\r\n\t\/\/----- WRITE BYTES -----\r\n\tbuffer[0] = 0x01;\r\n\tbuffer[1] = 0x02;\r\n\tlength = 2;\t\t\t\/\/&lt;&lt;&lt; Number of bytes to write\r\n\tif (write(file_i2c, buffer, length) != length)\t\t\/\/write() returns the number of bytes actually written, if it doesn&#39;t match then an error occurred (e.g. no response from the device)\r\n\t{\r\n\t\t\/* ERROR HANDLING: i2c transaction failed *\/\r\n\t\tprintf(&quot;Failed to write to the i2c bus.\\n&quot;);\r\n\t}\r\n<\/code><\/pre>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Enabling The I2C Port The I2C port needs to be enabled in Rasbian before it can be used. See here. Checking For Connected Devices Install the I2C tools (i2c-tools is a set of I&sup2;C programs that make it easy to debug I&sup2;C devices without having to write any code): sudo apt-get install i2c-tools sudo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-473","post","type-post","status-publish","format-standard","hentry","category-i2c"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/473","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=473"}],"version-history":[{"count":16,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/473\/revisions"}],"predecessor-version":[{"id":2355,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/473\/revisions\/2355"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}