Tell me more ×
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
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

2 Answers

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

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.