-
Notifications
You must be signed in to change notification settings - Fork 30
Fixes #179 - I2C tutorial added #181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
d601803
df50c18
6b0e741
537b8cc
334c440
0980466
56a8e7a
904d788
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # I2C | ||
|
|
||
| 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. | ||
|
|
||
| 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. | ||
|
||
|
|
||
| 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) | ||
|
||
|
|
||
| https://github.com/276linesofCode/Codes-Ideas/edit/master/tut-i2c.md#fork-destination-box | ||
|
||
|
|
||
| ```js | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this code going to do? Introduce it! |
||
| var tessel = require('tessel'); //Import tessel | ||
|
|
||
| // Connect to device | ||
| var port = tessel.port.A; // Select Port A of Tessel | ||
|
|
||
| //This address of the Slave has been taken from https://github.com/tessel/accel-mma84/blob/master/index.js#L15 | ||
|
||
| //More about registers can be found at Page 19 of https://www.nxp.com/docs/en/data-sheet/MMA8452Q.pdf | ||
| var slaveAddress = 0x1D; // Specefic for accelerometer module | ||
|
||
| var i2c = new port.I2C(slaveAddress); // Initialize I2C communication | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be useful to note that this is a built-in function of a Tessel port, possibly even reference where in the code port.I2C is defined. Up to you, bonus points ;) |
||
|
|
||
| // Details of I2C transfer | ||
| var numBytesToRead = 1; // Read back this number of bytes | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we reading back this number of bytes here? |
||
|
|
||
| i2c.read(numBytesToRead, function (error, dataReceived) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is it we're reading here? |
||
|
|
||
| // Print data received (buffer of hex values) | ||
| console.log('Buffer returned by I2C slave device ('+slaveAddress.toString(16)+'):', dataReceived); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What should I expect back from this |
||
|
|
||
| }); | ||
|
|
||
| // Read/Receive data over I2C using i2c.transfer | ||
| // 0x0D is the WHO_AM_I Register which sends back an acknoledgement to the master for starting the communication | ||
| i2c.transfer(new Buffer([0x0D]), numBytesToRead, function (error, dataReceived) { | ||
|
||
|
|
||
| // Print data received (buffer of hex values) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the whitespace correct here? Looks a little too tabbed in |
||
| // The returned buffer from the I2C slave device should be [0x2A] | ||
|
||
| console.log('Buffer returned by I2C slave device ('+slaveAddress.toString(16)+'):', dataReceived); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What could I expect to be logged here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has been added. |
||
|
|
||
| }); | ||
|
|
||
|
|
||
| // Now, try to print the accelerometer data using i2c.transfer | ||
| // 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 | ||
| // 6 Bytes are used for pairwise MSB and LSB of the x,y and z axis | ||
|
||
| i2c.transfer(new Buffer([0x01]), 6, function (error, dataReceived) { | ||
|
||
|
|
||
| if (error) throw error; | ||
| //Create a blank array for the output | ||
| var out=[]; | ||
| //iterating three times the x, y, z values | ||
| for (var i=0;i<3;i++){ | ||
| var gCount=(dataReceived[i*2] << 8) | dataReceived[(i*2)+1]; //Converting the 8 bit data into a 12 bit | ||
|
||
| gCount=gCount >> 4; | ||
| if (dataReceived[i*2] > 0x7F) { | ||
| gCount = -(1 + 0xFFF - gCount); // Transform into negative 2's complement | ||
| } | ||
| out[i] = gCount / ((1<<12)/(2*2)); | ||
|
||
| } | ||
| console.log('The x, y, z values are :',out); //Log the Array containing the x,y,z values | ||
|
|
||
| }); | ||
| ``` | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does your code actually work & respond appropriately when run? I think you're missing a crucial step– look up SYSMOD in the datasheet |
||
|
|
||
| [Datasheet for accelerometer module](http://www.nxp.com/docs/en/data-sheet/MMA8452Q.pdf) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you include a link to a resource where folks can learn more about I2C communication, i.e. SparkFun?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks a lot for the reviews!! We will update the PR with the requested changes.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The link to sparkfun for I2C Communication has been added.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These seem like a separate section, not part of Sample Output |
||
|
|
||
| [More Informatiom on I2C Communication](https://learn.sparkfun.com/tutorials/i2c) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strike "simple-to-use"– that's a value judgment and might make some readers think along the lines of "well, I couldn't figure it out..". You can say it's "designed to be simple" if you want, but I think the rest of your description (two wires, unlimited devices) is more helpful