Class I2C


  • public class I2C
    extends Object

    WiringPi includes a library which can make it easier to use the Raspberry Pi’s on-board I2C interface.

    Before you can use the I2C interface, you may need to use the gpio utility to load the I2C drivers into the kernel:

    $ gpio load i2c

    If you need a baud rate other than the default 100Kbps, then you can supply this on the command-line:

    $ gpio load i2c 1000

    which will set the baud rate to 1000Kbps – ie. 1,000,000 bps. (K here is times 1000)

    This library depends on the wiringPi native system library. (developed by Gordon Henderson @ http://wiringpi.com/)
    Author:
    Robert Savage (http://www.savagehomeautomation.com)
    See Also:
    https://www.pi4j.com, http://wiringpi.com/reference/i2c-library
    • Field Detail

      • CHANNEL_0

        public static int CHANNEL_0
      • CHANNEL_1

        public static int CHANNEL_1
    • Method Detail

      • wiringPiI2CSetup

        public static int wiringPiI2CSetup​(int devid)

        wiringPiI2CSetup:

        This initialises the I2C system with your given device identifier. The ID is the I2C number of the device and you can use the i2cdetect program to find this out. wiringPiI2CSetup() will work out which revision Raspberry Pi you have and open the appropriate device in /dev.

        The return value is the standard Linux filehandle, or -1 if any error – in which case, you can consult errno as usual.

        E.g. the popular MCP23017 GPIO expander is usually device Id 0×20, so this is the number you would pass into wiringPiI2CSetup().

        Parameters:
        devid - I2C device id
        Returns:
        return -1 on error; else returns Linux file handle
        See Also:
        http://wiringpi.com/reference/i2c-library
      • wiringPiI2CRead

        public static int wiringPiI2CRead​(int fd)

        wiringPiI2CRead:

        Simple I2C device read. Some devices present data when you read them without having to do any register transactions.

        Parameters:
        fd - Linux file handle obtained from call to wiringPiI2CSetup
        Returns:
        return -1 on error; else data read from I2C device
        See Also:
        http://wiringpi.com/reference/i2c-library
      • wiringPiI2CWrite

        public static int wiringPiI2CWrite​(int fd,
                                           int data)

        wiringPiI2CWrite:

        Simple I2C device write. Some devices accept data this way without needing to access any internal registers.

        Parameters:
        fd - Linux file handle obtained from call to wiringPiI2CSetup
        data - data to write
        Returns:
        return -1 on error
        See Also:
        http://wiringpi.com/reference/i2c-library
      • wiringPiI2CWriteReg8

        public static int wiringPiI2CWriteReg8​(int fd,
                                               int reg,
                                               int data)

        wiringPiI2CWriteReg8:

        I2C device write. Write an 8-bit data value into the device register indicated.

        Parameters:
        fd - Linux file handle obtained from call to wiringPiI2CSetup
        reg - I2C device register address
        data - data byte to write (8 bits/1 byte)
        Returns:
        return -1 on error
        See Also:
        http://wiringpi.com/reference/i2c-library
      • wiringPiI2CWriteReg16

        public static int wiringPiI2CWriteReg16​(int fd,
                                                int reg,
                                                int data)

        wiringPiI2CWriteReg16:

        I2C device write. Write a 16-bit data value into the device register indicated.

        Parameters:
        fd - Linux file handle obtained from call to wiringPiI2CSetup
        reg - I2C device register address
        data - data bytes to write (16 bits/2 bytes)
        Returns:
        return -1 on error
        See Also:
        http://wiringpi.com/reference/i2c-library
      • wiringPiI2CReadReg8

        public static int wiringPiI2CReadReg8​(int fd,
                                              int reg)

        wiringPiI2CReadReg8:

        I2C device read. Read an 8-bit (1 byte) data value from the device register indicated.

        Parameters:
        fd - Linux file handle obtained from call to wiringPiI2CSetup
        reg - I2C device register address
        Returns:
        return -1 on error; else returns 8-bit value read from I2C device register
        See Also:
        http://wiringpi.com/reference/i2c-library
      • wiringPiI2CReadReg16

        public static int wiringPiI2CReadReg16​(int fd,
                                               int reg)

        wiringPiI2CReadReg16:

        I2C device read. Read a 16-bit (2 bytes) data value from the device register indicated.

        Parameters:
        fd - Linux file handle obtained from call to wiringPiI2CSetup
        reg - I2C device register address
        Returns:
        return -1 on error; else returns 16-bit value read from I2C device register
        See Also:
        http://wiringpi.com/reference/i2c-library