ir_fuel
Posts: 2
Joined: Sat Jul 23, 2016 11:48 pm

Can't get I2C to work correctly

Sun Jul 24, 2016 12:07 am

Hi
I have a Raspberry Pi rev3 board, installed NOOBS and have everything configured to use I2C.
I connected an Adafruit 10DOF board (which is an LSM303 combined with a few other sensors) and I want to read the accelerometer values.
I have tried different languages (nodejs, C), but I think it's easier to show what's going on by simply using terminal commands.

When running

Code: Select all

i2cdetect -y 1
I can see that the board is correctly connected:

Code: Select all

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- 19 -- -- -- -- 1e --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- 6b -- -- -- --
70: -- -- -- -- -- -- -- 77
However when trying to read or write I always get errors:

Code: Select all

i2cget -y 1 0x19 0x20
Error: Read failed

i2cset -y 1 0x19 0x20 0x27
Error: Write failed
Running those commands with sudo has the same effect

Code: Select all

ls -l /dev/i2c-1
crw-rw-rw- 1 root i2c 89, 1 Jul 23 23:40 /dev/i2c-1
Any idea on how I can debug this?

User avatar
DougieLawson
Posts: 38886
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Can't get I2C to work correctly

Sun Jul 24, 2016 5:03 pm

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

ir_fuel
Posts: 2
Joined: Sat Jul 23, 2016 11:48 pm

Re: Can't get I2C to work correctly

Sun Jul 24, 2016 10:45 pm

Well, I can't just use that since it depends on Arduino libraries.
I did look into the code to see how they do things, but I think my test cases on the command line show the problem is somewhere else imho.

Just to add: i2cdump also works

Code: Select all

i2cdump -y 1 0x19
No size specified (using byte-data access)
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
10: XX XX XX XX XX XX XX 7f 25 03 71 64 e7 XX XX XX    XXXXXXX?%?qd?XXX
20: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
30: XX XX XX XX XX XX ff XX XX XX XX XX XX XX XX XX    XXXXXX.XXXXXXXXX
40: XX XX XX XX XX XX XX XX 00 0f 00 03 3f 00 00 01    XXXXXXXX.?.??..?
50: 00 00 07 XX 00 00 00 00 00 00 00 00 00 00 00 00    ..?X............
60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 33    ...............3
90: 93 03 8c 22 01 32 e4 a4 24 03 70 64 e0 20 80 00    ???"?2??$?pd? ?.
a0: 07 00 00 00 00 00 00 00 00 00 00 00 00 00 00 20    ?..............
b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................

christian-nils
Posts: 11
Joined: Wed Jun 22, 2016 9:27 am

Re: Can't get I2C to work correctly

Wed Jul 27, 2016 6:34 am

Hello!

A linux driver for ST accelerometers exists (https://github.com/raspberrypi/linux/bl ... ccel_i2c.c)
You just have to enable the driver by recompiling the kernel.

Then you should bind the hardware (For the binding, I guess you can have a look to https://github.com/raspberrypi/linux/bl ... lis302.txt which should be similar for the lsm303). It will automatically appears in /sys/bus/iio/devices/ afterwards.

You can then use libiio to write your code to access the hardware.

It should be something similar with the other sensors on the breakout board.

/CN

tsaG
Posts: 80
Joined: Thu Jun 26, 2014 1:30 pm

Re: Can't get I2C to work correctly

Thu Jul 28, 2016 12:26 am

Do you have an Oscilloscope? Try to hook it up to (at least) the SCK Line. I had this problem before. After doing several i2cget and i2cset the lines were always low and I had to powercycle (not only reboot) the Pi.

christian-nils
Posts: 11
Joined: Wed Jun 22, 2016 9:27 am

Re: Can't get I2C to work correctly

Thu Jul 28, 2016 5:49 am

tsaG and I had a problem with the i2c baudrate. You can try increasing the baudrate to something like 1Mbps to see if the accelerometer replies.

/CN

Return to “Interfacing (DSI, CSI, I2C, etc.)”