Take the 2-minute tour ×
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

Is there a simple library to talk to i2c for C++? I have found some stuff for python and Java, but I wanted C++. I was hoping someone has ported the Arduino Wire library so I could use code I have already written for the Arduino. Thanks.

share|improve this question
1  
possible duplicate of How can I use I2C to talk to sensors? –  Alex L Nov 18 '12 at 14:58
1  
Disagree ... I am really asking if the Arduino wiring library was ported over to Pi so I can use code written for Arduino easily on the Pi. That doesn't seem to be the case, so any equivalent, easy to use library would be nice. However, that also seems to not exist, so I am left with using the i2c-dev code. –  kevin Dec 19 '12 at 23:20

3 Answers 3

There's a i2c-dev header in the Linux userspace. I can't remember if this header is shipped with the lm-sensors package, or if it will need to be installed from source. I'd check your distro's package repository. xGoat has a nice article covering preparation & usage.

#include <linux/i2c-dev.h>
/*
  Including i2c-dev header will allow the following I2C SMBus functions
  - i2c_smbus_access
  - i2c_smbus_write_quick
  - i2c_smbus_read_byte
  - i2c_smbus_write_byte
  - i2c_smbus_read_byte_data
  - i2c_smbus_write_byte_data
  - i2c_smbus_read_word_data
  - i2c_smbus_write_word_data
  - i2c_smbus_process_call
  - i2c_smbus_read_block_data
  - i2c_smbus_write_block_data
  - i2c_smbus_read_i2c_block_data
  - i2c_smbus_write_i2c_block_data
  - i2c_smbus_block_process_call
*/

The source code for i2c-tools (download) are good examples in C. I've seen a few simple C++ libraries wrapping these functions. I'd recommend authoring your own library to suit your needs. Other great examples can be found on Github, like this I2CBus library

share|improve this answer
    
Thanks ... I am already looking at this –  kevin Dec 19 '12 at 23:21

There is a WiringPi which I think does exactly what you want. There are also wrappers for Pascal, Java, Python, Perl, TCL and Ruby. Additionally, someone might want to explore similar links:

  1. http://www.susa.net/wordpress/2012/06/raspberry-pi-pcf8563-real-time-clock-rtc/
  2. http://binerry.de/post/26685647322/raspberry-pi-and-i2c
  3. http://www.lazarus.freepascal.org/index.php?topic=17404.0
share|improve this answer
    
Nope ... look again, it doesn't do i2c, just basic pin functions. The project is somewhat misleadingly named. Your first link has some nice c code, but not a simple wrapper to do i2c like with Arduino. I will probably have to write it myself. –  kevin Nov 19 '12 at 23:18
    
WiringPi I2C library: wiringpi.com/reference/i2c-library –  avra Feb 5 at 22:03

As mentioned by emcconville there is a i2c-dev header in the Linux userspace (#include <linux/i2c-dev.h>). Furthermore you need a character decive to read from. This can be done by loading the correct modules. i2c_bcm2708 for the low level driver and i2c-dev for generating the character decives for the busses. Apply ...

sudo modprobe -r i2c_bcm2708
sudo modprobe i2c_bcm2708 baudrate=32000

for loading them on the fly. Apply ...

sudo sh -c 'echo "i2c-dev" >> /etc/modules'
sudo sh -c 'echo "options i2c_bcm2708 baudrate=<your preferred baudrate>\n" > /etc/modprobe.d/custom.conf

and unblacklist i2c_bcm2708 in /etc/modprobe.d/raspi-blacklist.conf to make /dev/i2c-0 and /dev/i2c-1 show up permanently.

From now on you can follow the hints on how to use I²C del maestro himself.

I prefer this method because it is platformagnostic. Your can use linux/i2c-dev.h with other devices too, as long as there exists a I²C driver. WiringPi is bound to the rPi.

Regards

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.