Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions Tutorials/i2c.md
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.
Copy link
Member

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


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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typos: micro-controllers > microcontrollers; i2C > I2C

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would > We will

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The accel-mma84 what? The MMA84 accelerometer integrated sensor?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or would it be more useful to say, the Tessel Accelerometer Module?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be a good idea to link to the MMA84 datasheet regardless as it will be useful for someone following along

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


![Fritzing Diagram](http://i.imgur.com/zK4U4S3.png)https://github.com/276linesofCode/Codes-Ideas/edit/master/tut-i2c.md#fork-destination-box
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the Github link supposed to be there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


```js
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: space after //

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

//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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did you find this address? From somewhere in the datasheet? How can readers discover this as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been added.

var i2c = new port.I2C(slaveAddress); // Initialize I2C communication
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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) {
Copy link
Member

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should I expect back from this read? Would anything come back from this component?


});

// 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you choose to use 0x0D for this Buffer value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been added.


// Print data received (buffer of hex values)
Copy link
Member

Choose a reason for hiding this comment

The 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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"...as specified in the register map on page X of the MMA84 datasheet"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

console.log('Buffer returned by I2C slave device ('+slaveAddress.toString(16)+'):', dataReceived);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What could I expect to be logged here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do MSB and LSB stand for? What do mean by "used for pairwise MSB and LSB"?

i2c.transfer(new Buffer([0x01]), 6, function (error, dataReceived) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you choose to use 0x01 for this Buffer? Did you find this instruction in the datasheet?

And why are you listening for 6 bytes of data?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been added.


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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does gCount mean or stand for? Can you add a paragraph after the sample code to explain how converting 8 bit data into 12 bit data works?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Frijol Could you please help us out as to what is exactly happening in this part of the code? We tried understanding it from our part but couldnt do so.

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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is happening here?

}
console.log('The x, y, z values are :',out); //Log the Array containing the x,y,z values

});
```
Copy link
Member

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The link to sparkfun for I2C Communication has been added.

Copy link
Member

Choose a reason for hiding this comment

The 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)