{"action":"created","comment":{"url":"https://api.github.com/repos/tessel/t2-docs/pulls/comments/141737492","pull_request_review_id":65985147,"id":141737492,"diff_hunk":"@@ -0,0 +1,102 @@\n+# I2C\n+\n+I2C is a simple-to-use communication protocol which can have more than one master, with the upper bus speed defined, and only two wires with pull-up resistors are needed to connect almost _unlimited_ number of I2C devices.\n+\n+Each slave device has a unique address. Transfer from and to master device is serial and it is split into 8-bit packets. All these simple requirements make it very simple to implement I2C interface even with cheap micro-controllers that have no special I2C hardware controller. You only need 2 free I/O pins and few simple i2C routines to send and receive commands.\n+\n+Let us get the accelerometer values using the I2C protocol. We would be using the [accel-mma84](https://www.seeedstudio.com/Tessel-Accelerometer-Module-p-2223.html)\n+\n+![Fritzing Diagram](http://i.imgur.com/zK4U4S3.png)https://github.com/276linesofCode/Codes-Ideas/edit/master/tut-i2c.md#fork-destination-box\n+\n+```js\n+var tessel = require('tessel');\n+\n+// Connect to device\n+var port = tessel.port.A; // Use the SCL/SDA pins of Port A\n+\n+//This address of the Slave has been taken from https://github.com/tessel/accel-mma84/blob/master/index.js#L15\n+var slaveAddress = 0x1D; // Specific to device\n+\n+var i2c = new port.I2C(slaveAddress); // Initialize I2C communication\n+\n+// Details of I2C transfer\n+var numBytesToRead = 1; // Read back this number of bytes\n+\n+// Read data over I2C using i2c.transfer\n+i2c.read(numBytesToRead, function (error, dataReceived) {\n+  // Print data received (buffer of hex values)\n+  console.log('Buffer returned by I2C slave device ('+slaveAddress.toString(16)+'):', dataReceived);\n+\n+});\n+\n+// Read/Receive data over I2C using i2c.transfer\n+// 0x0D is the WHO_AM_I Register which sends back an acknowledgement to the master for starting the communication\n+\n+i2c.transfer(new Buffer([0x0D]), numBytesToRead, function (error, dataReceived) {\n+    // Print data received (buffer of hex values)\n+    // The returned buffer from the I2C slave device should be [0x2A]\n+  console.log('Buffer returned by I2C slave device ('+slaveAddress.toString(16)+'):', dataReceived);\n+\n+});\n+\n+/*\n+\n+  In the i2c.transfer function the dataReceived consists of all the 6 bytes of data 2 bytes each for the x, y and z values.\n+  Each of the x, y and z coordinates have two registers associated with them for storing the 12 bit long sample.\n+  The first 8 bits are stored in their respective OUT_MSB registers. These are the Most Significant first 8 bits.\n+  The next 4 bits are stored in their respective OUT_LSB registers. The remaining 4 bits are occupied\n+  by 0s. These lower 4 bits are redundant bits which are not required. The OUT_LSB and OUT_MSB store the 2's complement form of the coordinates.\n+\n+  The organisation of the registers can be seen in the datasheet inside section 6.1 (Data Registers), page number - 21\n+  The register address for OUT_X_MSB is 0x01. This can be found at Page 19 of https://www.nxp.com/docs/en/data-sheet/MMA8452Q.pdf\n+\n+*/\n+  i2c.transfer(new Buffer([0x01]), 6, function (error, dataReceived) {\n+\n+      // This is an exception. If an error is generated, it will throw the error\n+      if (error) throw error;\n+\n+      //This array is going to store the final x,y,z values\n+      var out=[];\n+\n+\n+      /*\n+\n+      The for loop is iterated 3 times, in order to extract the x,y,z values.\n+      The gCount variable is a bitwise OR operation between two binary numbers:\n+        1. The OUT_MSB of the respective coordinate, that is left shifted by 8 bits in order to make space for the remaining 8 bits","path":"Tutorials/i2c.md","position":67,"original_position":67,"commit_id":"537b8cc461d0e6bb51ea21da30efad0589501d27","original_commit_id":"537b8cc461d0e6bb51ea21da30efad0589501d27","user":{"login":"Frijol","id":454690,"avatar_url":"https://avatars2.githubusercontent.com/u/454690?v=4","gravatar_id":"","url":"https://api.github.com/users/Frijol","html_url":"https://github.com/Frijol","followers_url":"https://api.github.com/users/Frijol/followers","following_url":"https://api.github.com/users/Frijol/following{/other_user}","gists_url":"https://api.github.com/users/Frijol/gists{/gist_id}","starred_url":"https://api.github.com/users/Frijol/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Frijol/subscriptions","organizations_url":"https://api.github.com/users/Frijol/orgs","repos_url":"https://api.github.com/users/Frijol/repos","events_url":"https://api.github.com/users/Frijol/events{/privacy}","received_events_url":"https://api.github.com/users/Frijol/received_events","type":"User","site_admin":false},"body":"Phrasing tweak \"The 8 bits in OUT_MSB for a given coordinate are shifted left by 8 bits...\"","created_at":"2017-09-28T21:11:11Z","updated_at":"2017-09-28T21:55:41Z","html_url":"https://github.com/tessel/t2-docs/pull/181#discussion_r141737492","pull_request_url":"https://api.github.com/repos/tessel/t2-docs/pulls/181","author_association":"OWNER","_links":{"self":{"href":"https://api.github.com/repos/tessel/t2-docs/pulls/comments/141737492"},"html":{"href":"https://github.com/tessel/t2-docs/pull/181#discussion_r141737492"},"pull_request":{"href":"https://api.github.com/repos/tessel/t2-docs/pulls/181"}}},"pull_request":{"url":"https://api.github.com/repos/tessel/t2-docs/pulls/181","id":139166677,"html_url":"https://github.com/tessel/t2-docs/pull/181","diff_url":"https://github.com/tessel/t2-docs/pull/181.diff","patch_url":"https://github.com/tessel/t2-docs/pull/181.patch","issue_url":"https://api.github.com/repos/tessel/t2-docs/issues/181","number":181,"state":"open","locked":false,"title":"Fixes #179 - I2C tutorial added","user":{"login":"mittalshravika","id":23384503,"avatar_url":"https://avatars0.githubusercontent.com/u/23384503?v=4","gravatar_id":"","url":"https://api.github.com/users/mittalshravika","html_url":"https://github.com/mittalshravika","followers_url":"https://api.github.com/users/mittalshravika/followers","following_url":"https://api.github.com/users/mittalshravika/following{/other_user}","gists_url":"https://api.github.com/users/mittalshravika/gists{/gist_id}","starred_url":"https://api.github.com/users/mittalshravika/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mittalshravika/subscriptions","organizations_url":"https://api.github.com/users/mittalshravika/orgs","repos_url":"https://api.github.com/users/mittalshravika/repos","events_url":"https://api.github.com/users/mittalshravika/events{/privacy}","received_events_url":"https://api.github.com/users/mittalshravika/received_events","type":"User","site_admin":false},"body":"Fixes #179. I2C tutorial with relevant code and fritzing diagram added.","created_at":"2017-09-04T14:12:53Z","updated_at":"2017-09-28T21:55:41Z","closed_at":null,"merged_at":null,"merge_commit_sha":"1cc5d4ff84fb3de759e94fdd309bf03e0cd5fe6e","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/tessel/t2-docs/pulls/181/commits","review_comments_url":"https://api.github.com/repos/tessel/t2-docs/pulls/181/comments","review_comment_url":"https://api.github.com/repos/tessel/t2-docs/pulls/comments{/number}","comments_url":"https://api.github.com/repos/tessel/t2-docs/issues/181/comments","statuses_url":"https://api.github.com/repos/tessel/t2-docs/statuses/537b8cc461d0e6bb51ea21da30efad0589501d27","head":{"label":"276linesofCode:issue-179","ref":"issue-179","sha":"537b8cc461d0e6bb51ea21da30efad0589501d27","user":{"login":"276linesofCode","id":29427894,"avatar_url":"https://avatars0.githubusercontent.com/u/29427894?v=4","gravatar_id":"","url":"https://api.github.com/users/276linesofCode","html_url":"https://github.com/276linesofCode","followers_url":"https://api.github.com/users/276linesofCode/followers","following_url":"https://api.github.com/users/276linesofCode/following{/other_user}","gists_url":"https://api.github.com/users/276linesofCode/gists{/gist_id}","starred_url":"https://api.github.com/users/276linesofCode/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/276linesofCode/subscriptions","organizations_url":"https://api.github.com/users/276linesofCode/orgs","repos_url":"https://api.github.com/users/276linesofCode/repos","events_url":"https://api.github.com/users/276linesofCode/events{/privacy}","received_events_url":"https://api.github.com/users/276linesofCode/received_events","type":"Organization","site_admin":false},"repo":{"id":96078206,"name":"t2-docs","full_name":"276linesofCode/t2-docs","owner":{"login":"276linesofCode","id":29427894,"avatar_url":"https://avatars0.githubusercontent.com/u/29427894?v=4","gravatar_id":"","url":"https://api.github.com/users/276linesofCode","html_url":"https://github.com/276linesofCode","followers_url":"https://api.github.com/users/276linesofCode/followers","following_url":"https://api.github.com/users/276linesofCode/following{/other_user}","gists_url":"https://api.github.com/users/276linesofCode/gists{/gist_id}","starred_url":"https://api.github.com/users/276linesofCode/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/276linesofCode/subscriptions","organizations_url":"https://api.github.com/users/276linesofCode/orgs","repos_url":"https://api.github.com/users/276linesofCode/repos","events_url":"https://api.github.com/users/276linesofCode/events{/privacy}","received_events_url":"https://api.github.com/users/276linesofCode/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/276linesofCode/t2-docs","description":"Documentation for Tessel 2.","fork":true,"url":"https://api.github.com/repos/276linesofCode/t2-docs","forks_url":"https://api.github.com/repos/276linesofCode/t2-docs/forks","keys_url":"https://api.github.com/repos/276linesofCode/t2-docs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/276linesofCode/t2-docs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/276linesofCode/t2-docs/teams","hooks_url":"https://api.github.com/repos/276linesofCode/t2-docs/hooks","issue_events_url":"https://api.github.com/repos/276linesofCode/t2-docs/issues/events{/number}","events_url":"https://api.github.com/repos/276linesofCode/t2-docs/events","assignees_url":"https://api.github.com/repos/276linesofCode/t2-docs/assignees{/user}","branches_url":"https://api.github.com/repos/276linesofCode/t2-docs/branches{/branch}","tags_url":"https://api.github.com/repos/276linesofCode/t2-docs/tags","blobs_url":"https://api.github.com/repos/276linesofCode/t2-docs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/276linesofCode/t2-docs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/276linesofCode/t2-docs/git/refs{/sha}","trees_url":"https://api.github.com/repos/276linesofCode/t2-docs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/276linesofCode/t2-docs/statuses/{sha}","languages_url":"https://api.github.com/repos/276linesofCode/t2-docs/languages","stargazers_url":"https://api.github.com/repos/276linesofCode/t2-docs/stargazers","contributors_url":"https://api.github.com/repos/276linesofCode/t2-docs/contributors","subscribers_url":"https://api.github.com/repos/276linesofCode/t2-docs/subscribers","subscription_url":"https://api.github.com/repos/276linesofCode/t2-docs/subscription","commits_url":"https://api.github.com/repos/276linesofCode/t2-docs/commits{/sha}","git_commits_url":"https://api.github.com/repos/276linesofCode/t2-docs/git/commits{/sha}","comments_url":"https://api.github.com/repos/276linesofCode/t2-docs/comments{/number}","issue_comment_url":"https://api.github.com/repos/276linesofCode/t2-docs/issues/comments{/number}","contents_url":"https://api.github.com/repos/276linesofCode/t2-docs/contents/{+path}","compare_url":"https://api.github.com/repos/276linesofCode/t2-docs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/276linesofCode/t2-docs/merges","archive_url":"https://api.github.com/repos/276linesofCode/t2-docs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/276linesofCode/t2-docs/downloads","issues_url":"https://api.github.com/repos/276linesofCode/t2-docs/issues{/number}","pulls_url":"https://api.github.com/repos/276linesofCode/t2-docs/pulls{/number}","milestones_url":"https://api.github.com/repos/276linesofCode/t2-docs/milestones{/number}","notifications_url":"https://api.github.com/repos/276linesofCode/t2-docs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/276linesofCode/t2-docs/labels{/name}","releases_url":"https://api.github.com/repos/276linesofCode/t2-docs/releases{/id}","deployments_url":"https://api.github.com/repos/276linesofCode/t2-docs/deployments","created_at":"2017-07-03T06:39:02Z","updated_at":"2017-07-01T19:44:00Z","pushed_at":"2017-09-26T15:26:31Z","git_url":"git://github.com/276linesofCode/t2-docs.git","ssh_url":"git@github.com:276linesofCode/t2-docs.git","clone_url":"https://github.com/276linesofCode/t2-docs.git","svn_url":"https://github.com/276linesofCode/t2-docs","homepage":"https://tessel.gitbooks.io/t2-docs/content/","size":1084,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"tessel:master","ref":"master","sha":"41104e4114a1f5dc140cfd7c7bd8bc8043228e5a","user":{"login":"tessel","id":5573563,"avatar_url":"https://avatars1.githubusercontent.com/u/5573563?v=4","gravatar_id":"","url":"https://api.github.com/users/tessel","html_url":"https://github.com/tessel","followers_url":"https://api.github.com/users/tessel/followers","following_url":"https://api.github.com/users/tessel/following{/other_user}","gists_url":"https://api.github.com/users/tessel/gists{/gist_id}","starred_url":"https://api.github.com/users/tessel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tessel/subscriptions","organizations_url":"https://api.github.com/users/tessel/orgs","repos_url":"https://api.github.com/users/tessel/repos","events_url":"https://api.github.com/users/tessel/events{/privacy}","received_events_url":"https://api.github.com/users/tessel/received_events","type":"Organization","site_admin":false},"repo":{"id":40696875,"name":"t2-docs","full_name":"tessel/t2-docs","owner":{"login":"tessel","id":5573563,"avatar_url":"https://avatars1.githubusercontent.com/u/5573563?v=4","gravatar_id":"","url":"https://api.github.com/users/tessel","html_url":"https://github.com/tessel","followers_url":"https://api.github.com/users/tessel/followers","following_url":"https://api.github.com/users/tessel/following{/other_user}","gists_url":"https://api.github.com/users/tessel/gists{/gist_id}","starred_url":"https://api.github.com/users/tessel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tessel/subscriptions","organizations_url":"https://api.github.com/users/tessel/orgs","repos_url":"https://api.github.com/users/tessel/repos","events_url":"https://api.github.com/users/tessel/events{/privacy}","received_events_url":"https://api.github.com/users/tessel/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/tessel/t2-docs","description":"Documentation for Tessel 2.","fork":false,"url":"https://api.github.com/repos/tessel/t2-docs","forks_url":"https://api.github.com/repos/tessel/t2-docs/forks","keys_url":"https://api.github.com/repos/tessel/t2-docs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tessel/t2-docs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tessel/t2-docs/teams","hooks_url":"https://api.github.com/repos/tessel/t2-docs/hooks","issue_events_url":"https://api.github.com/repos/tessel/t2-docs/issues/events{/number}","events_url":"https://api.github.com/repos/tessel/t2-docs/events","assignees_url":"https://api.github.com/repos/tessel/t2-docs/assignees{/user}","branches_url":"https://api.github.com/repos/tessel/t2-docs/branches{/branch}","tags_url":"https://api.github.com/repos/tessel/t2-docs/tags","blobs_url":"https://api.github.com/repos/tessel/t2-docs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tessel/t2-docs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tessel/t2-docs/git/refs{/sha}","trees_url":"https://api.github.com/repos/tessel/t2-docs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tessel/t2-docs/statuses/{sha}","languages_url":"https://api.github.com/repos/tessel/t2-docs/languages","stargazers_url":"https://api.github.com/repos/tessel/t2-docs/stargazers","contributors_url":"https://api.github.com/repos/tessel/t2-docs/contributors","subscribers_url":"https://api.github.com/repos/tessel/t2-docs/subscribers","subscription_url":"https://api.github.com/repos/tessel/t2-docs/subscription","commits_url":"https://api.github.com/repos/tessel/t2-docs/commits{/sha}","git_commits_url":"https://api.github.com/repos/tessel/t2-docs/git/commits{/sha}","comments_url":"https://api.github.com/repos/tessel/t2-docs/comments{/number}","issue_comment_url":"https://api.github.com/repos/tessel/t2-docs/issues/comments{/number}","contents_url":"https://api.github.com/repos/tessel/t2-docs/contents/{+path}","compare_url":"https://api.github.com/repos/tessel/t2-docs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tessel/t2-docs/merges","archive_url":"https://api.github.com/repos/tessel/t2-docs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tessel/t2-docs/downloads","issues_url":"https://api.github.com/repos/tessel/t2-docs/issues{/number}","pulls_url":"https://api.github.com/repos/tessel/t2-docs/pulls{/number}","milestones_url":"https://api.github.com/repos/tessel/t2-docs/milestones{/number}","notifications_url":"https://api.github.com/repos/tessel/t2-docs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tessel/t2-docs/labels{/name}","releases_url":"https://api.github.com/repos/tessel/t2-docs/releases{/id}","deployments_url":"https://api.github.com/repos/tessel/t2-docs/deployments","created_at":"2015-08-14T05:09:24Z","updated_at":"2017-09-13T12:33:16Z","pushed_at":"2017-09-26T15:26:33Z","git_url":"git://github.com/tessel/t2-docs.git","ssh_url":"git@github.com:tessel/t2-docs.git","clone_url":"https://github.com/tessel/t2-docs.git","svn_url":"https://github.com/tessel/t2-docs","homepage":"https://tessel.gitbooks.io/t2-docs/content/","size":1063,"stargazers_count":21,"watchers_count":21,"language":null,"has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":25,"mirror_url":null,"open_issues_count":31,"forks":25,"open_issues":31,"watchers":21,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/tessel/t2-docs/pulls/181"},"html":{"href":"https://github.com/tessel/t2-docs/pull/181"},"issue":{"href":"https://api.github.com/repos/tessel/t2-docs/issues/181"},"comments":{"href":"https://api.github.com/repos/tessel/t2-docs/issues/181/comments"},"review_comments":{"href":"https://api.github.com/repos/tessel/t2-docs/pulls/181/comments"},"review_comment":{"href":"https://api.github.com/repos/tessel/t2-docs/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/tessel/t2-docs/pulls/181/commits"},"statuses":{"href":"https://api.github.com/repos/tessel/t2-docs/statuses/537b8cc461d0e6bb51ea21da30efad0589501d27"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"Frijol"}}