12

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.

2
  • 2
    possible duplicate of How can I use I2C to talk to sensors?
    – Alex L
    Commented Nov 18, 2012 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
    Commented Dec 19, 2012 at 23:20

4 Answers 4

6

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

0
5

I just started using the pigpio library and I am very impressed. I especially like the bit bang mode, since it allows you to use any two GPIO pins as an I2C interface, as long as they have pullup resistors. If you are using a PI2, there is not much of a disadvantage to bit banging, since you have 4 CPUs. The nice thing about the bit bang commands is that they take a list of "address, write, data, read, start, stop" command bytes so that you can run multiple I/Os with a single call. It is the only thing that I've found that reliably runs repeated start transactions, which are required by many devices which take a register number at the start of a read command. The library is well documented and easy to use.

Below is test program which reads the temperature registers on a MAX31785. 4 sets the address with the next byte, 2 = start, 7 = write which is followed by a byte count and data bytes, 3 = stop, 6 = read which is followed by a byte count. The call returns any data bytes read along with the number of bytes.

#include <stdio.h>
#include <stdlib.h>
#include <pigpio.h>

#define MAX31785_TEMP_REG 0x8D
#define MAX31785_TEMP0 6
#define MAX31785_TEMP_INT 12
#define PAGE_REG_OFFSET 6  // Offset in CmdBuf of the page register write value


main( int argc, char *argv[])
{
    int  rcnt;
    char ReadBuf[256];
    char CmdBuf[] = {4, 0x52,  // Chip address
                       2, 7, 2, 0x00, MAX31785_TEMP0, 3,    // Write page register to select temperature sensor
                       2, 7, 1, MAX31785_TEMP_REG, 2, 6, 2, 3, // Read temperature register
                       0 // EOL
                       };

  if (gpioInitialise() < 0) return 1;

  // Open bit banging I2C on standard I2C pins
  if (bbI2COpen(2, 3, 100000)) return 1;

  while(1)
  {
    // Loop over the 7 temp sensors
      for(CmdBuf[PAGE_REG_OFFSET] = MAX31785_TEMP0; CmdBuf[PAGE_REG_OFFSET] <= MAX31785_TEMP_INT; CmdBuf[PAGE_REG_OFFSET]++)  
      {     
    // Read the temp reg for the current page
          rcnt = bbI2CZip(2, CmdBuf, sizeof(CmdBuf), ReadBuf, sizeof(ReadBuf));

          if(rcnt == 2)
            // Print out degrees C
            printf("%2.1f ", (short)(((ReadBuf[1] << 8) | ReadBuf[0]) + 50)/100.0 );
          else
            printf("Error: %d\n", rcnt);
      }

    printf("\n");
    fflush(stdout);
    sleep(1);
  }

  bbI2CClose(2);

  gpioTerminate();
}
1
  • crj11, Thanks for posting your code example. It was the best example I was able to find and put me on the right track. The only problem I had with it was by using the pigpiod_if.h library requires the code to run as sudo, which was not acceptable for my project. I was able to switch to the pigpiod_if2.h library and make a few tweaks and it ran like a champ. Commented Mar 24, 2016 at 2:33
2

As mentioned by emcconville there is a i2c-dev header in the Linux userspace (#include <linux/i2c-dev.h>). Furthermore you need a character device 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

UPDATE:

This information is deprecated. Check this post for the use of device trees.

0

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
2
  • 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
    Commented Nov 19, 2012 at 23:18
  • 3
    WiringPi I2C library: wiringpi.com/reference/i2c-library
    – avra
    Commented Feb 5, 2014 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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