I am working on Raspberry Pi Model B.
I am trying to interface this device over the I2C Bus on the Rpi.
My hardware Connections are:
RPi Accelerometer
Vcc P1_1 ------------------Vcc
Gnd P1_9 ------------------Gnd
SDA P1_3 ------------------SDA
SCL P1_5 ------------------SCL
I am using the wiring Pi Library.
I compile the code by the command:
pi@raspberrypi ~/acc $gcc main.c acc.c -lwiringPi
and run it with the command:
pi@raspberrypi ~/acc $ sudo ./a.out
I do the following before executing my code:
pi@raspberrypi ~/acc $ gpio load i2c
pi@raspberrypi ~/acc $ i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- 1d -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Here I am getting the correct slave address, but when I try to read anything from the device I always a 00 from the device.
Here is my code.
Anything that I am missing out.
I would appreciate the help
EDIT:
pi@raspberrypi ~/acc $ i2cget 1 0x1D 0x0D
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will read from device file /dev/i2c-1, chip address 0x1d, data address
0x0d, using read byte data.
Continue? [Y/n] Y
0x00
pi@raspberrypi ~/acc $ sudo strace -e open 2>&1 | grep dev
pi@raspberrypi ~/acc $sudo ./a.out
Return Value is 0
Success setting up i2c device with the fd = 4
The value of the WHO_AM_I register is 0
Coudnot connect to the device
X : Y : Z : 0, 0, 0
X : Y : Z : 0, 0, 0
X : Y : Z : 0, 0, 0
i2cgetcommand? Also, could you check if proper/dev/i2c-Xdevice is being opened on your device by runningsudo strace -e open 2>&1 | grep dev? – Krzysztof Adamski Apr 22 '13 at 07:03i2cgetshould be thei2cbus number which should be 1 for you. Please checki2cget 1 0x1D 0x0D. Please also check thestracecommand I gave you and update your question with the results. – Krzysztof Adamski Apr 22 '13 at 07:42strace -e open ./a.out 2>&1 | grep dev, actually. The most important information is thati2cgetgives you0x00so it's not your program that is wrong. Unfortunately I don't know this device and I don't have enough time to read the datasheet so I can't help you any more. You have to double check all your wiring and read datasheet carefully. Makei2cget/i2csetworking before you start debugging the C program. – Krzysztof Adamski Apr 22 '13 at 08:49stracecommand is to check that properi2cport is used but maybewiringpidoes not use/dev/i2c-Xfiles so lets leave it for now. About your other question - it does means that yourI²Cconnection is kind of OK. Note thatMMA8452has internal pull-up resistors so does RaspberryPi. I'm not sure if it's not a problem. I have also found some people over Internet mentioning that this modules is (for some reason) not compatible with RaspberryPi. You could try using software I²C bus instead of hardware one also using pins without pull-ups but that requires kernel module compilation. – Krzysztof Adamski Apr 22 '13 at 09:17