pedalpete
pedalpete

Reputation: 21566

How to read data from i2c using i2cget?

I'm new to embedded devices and am trying to understand how to use i2cget (or the entire I2C protocol really).

I'm using an accelerometer MMA8452, and the datasheet says the Slave Address is 0x1D (if my SAO=1, which I believe is referring to the I2C bus being on channel 1 on my raspberrypi v2).

From the command line, I enter

sudo i2cget -y 1 0X1d

It returns

0X00

I think that means I'm attached to the correct device.

So now, I'm trying to figure out how do I get actual data back from the accelerometer?

The i2c spec says

i2cget [-y] i2cbus chip-address [data-address [mode]]

So I have tried

sudo i2cget -y 1 0x1D 0x01

where 0x01 is the OUT_X_MSB. I'm not sure entirely what I'm expecting to get back, but I figured if I saw some data other than 0x00, I might be able to figure that out.

Am I using ic2get wrong? Is there a better way to learn and get data from i2c?

The datasheet for my accelerometer chip is at http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Accelerometers/MMA8452Q.pdf

Upvotes: 6

Views: 61678

Answers (2)

apex
apex

Reputation: 859

I am way too late but this might help other people. You might be getting the 0x00 output every time you use i2cget is because you might have forgotten to set some mode. For instance, I was working on pcf8583 which is a clock and calendar chip and can also be use as a counter. My goal was to use this chip as a counter. It was connected to i2cbus1 with device address 0x51. So reading the data sheet, I found out that, the chip would work as a counter when the mode is set to 0x20 in the control register 0x00. The command I used for doing this:

i2cset 1 0x51 0x00 0x20

Now, I could read the counter pulses from a wind sensor with the command:

watch i2cget -y 1 0x51

watch is just a linux command that runs the specified command (i2cget) repeatedly every 2 seconds and displays the results on standard output.

Upvotes: 6

microMolvi
microMolvi

Reputation: 664

From the datasheet its clear that the default value of Status Register Address 0x00 is 0x00, so you are doing fine I guess. See Table 11 Register Map Address in the datasheet.

You may try reading the device ID at Register Address 0x0D. You should get value 0x2A when you read this register(0x0D).

Upvotes: 2

Related Questions