falken1701
Posts: 4
Joined: Mon Oct 15, 2012 8:30 am

Use I2C with Ruby

Mon Oct 15, 2012 8:43 am

hello,

i try to access the I2C bus with ruby on rails. I search the net for some information. The wiringpi gem seems to be a way, but from my point of view it is used to access the GPIO I/O's and not to access the I2C bus. Has someone tryed something like this?

regards
michael

User avatar
joan
Posts: 16247
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Use I2C with Ruby

Mon Oct 15, 2012 10:05 am

There is a proper i2c device. I'd use that as it's likely to be much simpler than rolling your own with wiringPi.

If you use the device I2C is accessed like any other file.

Have a look at http://www.raspberrypi.org/phpBB3/viewt ... 19#p191519

falken1701
Posts: 4
Joined: Mon Oct 15, 2012 8:30 am

Re: Use I2C with Ruby

Tue Oct 16, 2012 7:16 am

Hello Joan,

i read all this exaples befor. But there be only python and C exaples. I search for a way to access the I2C with ruby on rails.

regards
Michael

schroeder
Posts: 9
Joined: Wed Oct 17, 2012 8:34 am

Re: Use I2C with Ruby

Wed Oct 17, 2012 8:54 am

The only way is to use the c example via ruby ffi. That needs some c knowlege. I want to acess i2c via ruby too. But i didn't have to right it atm.

mikec79
Posts: 18
Joined: Thu Oct 18, 2012 12:27 am

Re: Use I2C with Ruby

Thu Oct 18, 2012 1:00 am

I'm also trying to learn how to use I2C with Ruby. In my case, I'm trying to use a LSM303 Tilt Compensated Compass from sparkfun. https://www.sparkfun.com/products/10703


Steps I've done so far:
  • Install ruby
  • connect LSM303 to r-pi
  • Test connection with i2cdetect
  • Experiment in irb
require "i2c/i2c"
@lsm303 = ::I2C.create("/dev/i2c-0")
@lsm303.read(0x1E, 0x02)
@lsm303.read(0x1E, 0x03)

I've also found the following:
https://github.com/meinside/raspi-adafr ... 8matrix.rb

mikec79
Posts: 18
Joined: Thu Oct 18, 2012 12:27 am

Re: Use I2C with Ruby

Fri Oct 19, 2012 2:21 am

I've also tried a different method from the command line.

Code: Select all

pi@raspberrypi:~$ i2cset -y 0 0x1e 0x00 0x14
pi@raspberrypi:~$ i2cset -y 0 0x1e 0x02 0x00
i@raspberrypi:~$ i2cget -y 0 0x1e 0x02
0x00
pi@raspberrypi:~$ i2cget -y 0 0x1e 0x03
0xff
pi@raspberrypi:~$ i2cget -y 0 0x1e 0x04
0x44
pi@raspberrypi:~$ i2cget -y 0 0x1e 0x05
0x01
pi@raspberrypi:~$ i2cget -y 0 0x1e 0x06
0x4f
pi@raspberrypi:~$ i2cget -y 0 0x1e 0x07
0xfe
pi@raspberrypi:~$ i2cget -y 0 0x1e 0x08
0x93
here's what I think is the equivalent in ruby:

Code: Select all

require "i2c/i2c"
require "i2c/backends/i2c-dev"

@lsm303 = ::I2C.create("/dev/i2c-0")

@lsm303.write(0x1E, 0x00, 0x14)
@lsm303.write(0x1E, 0x02, 0x00)

puts @lsm303.read(0x1E, 0x02)
puts @lsm303.read(0x1E, 0x03)
puts @lsm303.read(0x1E, 0x04)
puts @lsm303.read(0x1E, 0x05)
puts @lsm303.read(0x1E, 0x06)
puts @lsm303.read(0x1E, 0x07)
puts @lsm303.read(0x1E, 0x08)
But my results don't quite match

Code: Select all

?A
R?
??A
R??
?AR??
?AR??
?AR??

mikec79
Posts: 18
Joined: Thu Oct 18, 2012 12:27 am

Re: Use I2C with Ruby

Fri Oct 19, 2012 3:26 am

I think I'm getting closer. I need to read up on the unpack method and the << operator

Code: Select all

require "i2c/i2c"
require "i2c/backends/i2c-dev"

@lsm303 = ::I2C.create("/dev/i2c-0")

@lsm303.write(0x1E, 0x00, 0x14)
@lsm303.write(0x1E, 0x02, 0x00)

@lsm303.read(0x1E, 0x02)

mag_data = Array.new

(3..8).each do |i|
	mag_data.push  @lsm303.read(0x1E, 0x02)
end

Mx = (mag_data[0] << 8) + mag_data[1]
My = (mag_data[2] << 8) + mag_data[3]
Mz = (mag_data[4] << 8) + mag_data[5]

puts Mx.unpack("C")[0]
puts My.unpack("C")[0]
puts Mz.unpack("C")[0]


Return to “Other programming languages”