An I2C Aside

I2C (pronounced I squared C) as it is commonly written is a communication protocol designed to allow small, low level (read: very close to the hardware, not easy to read the code) devices to communicate with each other. I first encountered it while competing in the First Tech Challenge Robotics competiton. Anyone who has every played with Lego Mindstorms will recognize the sensors and phone cables

(Source)
These sensors communicate with brain brick over I2C. In the world of small electronics there are a lot of useful devices that use I2C to communicate, for instance our magnetometer. I thought it would be useful to write up my understanding of the protocol here for future teams use as well as anyone else trying to use it.

I2C runs on 2 wires, SCL or the clock line, and SDA or the data line. The clock line is what times the slave devices and enables them to stay in sync. This is set and run by the master device(s). The data line is the actual zeros and ones going to and coming from the sensors. In order to get information from an I2C sensor you need to know a few things. First is it's slave address, this can usually be found in the sensors datasheet. I will follow along with this guide for using the MPU-6050 as an example. Here are some links to the
Datasheet
Product Page
You buy this device pinned out to standard 0.10" holes on sites such as sparkfun or from the OEM.
So with that out of the way lets have a look at the datasheet, on page 15 of 52 look at the line that has the Parameter I 2C ADDRESS. Notice that 2 address' are listed, with conditions AD0 = 0, and AD0 = 1. AD0 on the device is actually a pin that can be tied to ground or the logic voltage to change the address of the device. This is in case we happen to have another device that has the same address as the MPU. We aren't to worried about this so we'll assume AD0 = 0, either its floating or tied to ground.

Now we can see that the address of the device will be 1101000. This is binary for 104, and is the address that we will use to speak to the MPU. Usually this address is expressed in hexadecimal (do some googling, they're relatively quick to learn, or just use a converter) so we can cover it, which results in 0x68. Now this is the address we will use. If you have a different device just look through the datasheet for the I2C address and use that.

Now what we want to do is open communications to the device, so using the Adafruit_I2C() we create an I2C object to speak to, and we use the address of the device to tell the processor where to look for it. An I2C is kind of like a house, when looking for a specific piece of information you first need the street address (the slave address), then once you've arrived you still need to know where inside of the house the item is located (the register address). These pieces of information are stored in the chip in what are called registers, normally we would look to the datasheet again and look for the register address of the accelerometer axis, however since this chip has a lot of sensors it actually has a separate file which contains the register map here.

Page 30 of 47 contains the information about the accelerometers. Since each register can only hold 1 byte of information at a time for only 255 possible values and our sensor is more precise than that, we need to get 2 bytes separately from 2 registers. These are located at the ACCEL_XOUT values, 15:8 represent the 8 largest bits, or most significant byte, 7:0 represents the 8 smallest bits or the least significant byte. These are located at registers 0x3B and 0x3C so these are the ones we want to read. This is done by using the functions within I2C library which are detailed here.

These then need to be combined into a meaningful measurement, and since the most significant byte (MSB) represents the larger half of the number, we want to multiply it by 256 and combine it with the least significant byte. Although the addresses may be different this process for communicating with the sensors can be re-used. In some instances you will need to tell the device to give you a value, what this means is you will have to write to a register. All of the information one could possibly need will be in the datasheet.

Comments

Popular posts from this blog

Camera Field of View Test

Camera Test in the Sun

1 June 15