Main Content

enableI2C

Enable I2C interface

Description

example

enableI2C(mypi) enables the I2C bus at its default bus speed of 100000 bps. You can then use the Raspberry Pi™ hardware board pins I2C1_SDA (GPIO 2) and I2C1_SCL (GPIO 3) as I2C pins and interface the Raspberry Pi board with any I2C device to exchange data. The I2C bus is enabled by default. To disable I2C, use disableI2C.

example

enableI2C(mypi,i2cBusSpeed) enables the I2C bus and overrides the current bus speed.

Examples

collapse all

Enable and disable the I2C interface on the Raspberry Pi kernel and use the Raspberry Pi hardware board pins for I2C functionality and GPIO, respectively. Read temperature data in degrees Celsius from the TMP102 I2C temperature sensor interfaced with the Raspberry Pi hardware board. For more information on the TMP102 temperature sensor, refer to its datasheet. You can also exchange data between the Raspberry Pi and other I2C devices. For more information, refer to the datasheet of the specific I2C device. Perform these steps on the Raspberry Pi Linux® terminal to ensure that the I2C interface is enabled in the hardware kernel.

  1. Run this command:

    sudo raspi-config

  2. Select Interfacing Options > I2C.

    Raspberry Pi Kernel I2C Interfacing options selection on Linux terminal

  3. Select Yes when prompted to enable the I2C interface.

    Raspberry Pi Kernel I2C Interfacing enabling options

  4. Select Yes when prompted to automatically load the I2C kernel module.

  5. Select Finish.

  6. Select Yes when prompted to reboot.

Tip

You can also enable the I2C interface using the Raspberry Pi Resource Monitor App.

To create a connection from MATLAB® to the Raspberry Pi board, execute this command at the MATLAB Command Window.

mypi=raspi;
           DeviceAddress: 'raspberrypi-hysdu8X38o'
                    Port: 18734
               BoardName: 'Raspberry Pi 3 Model B+'
           AvailableLEDs: {'led0'}
    AvailableDigitalPins: [4 5 6 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27]
    AvailableSPIChannels: {‘CE0’,’CE1’}
       AvailableI2CBuses: {'i2c-1'}
             I2CBusSpeed: 100000
        AvailabelWebCams: mmal service 16.1 (platform:bcm2835-v4l2)
GPIO header pins I2C_SDA (GPIO 2) and I2C_SCL (GPIO 3) are used for the I2C functionality. GPIO pins 2 and 3 are not listed in the AvailableDigitalPins property as the I2C functionality is enabled by default and the pins are currently being utilized by the I2C functionality.

To display the pin mapping of the Raspberry Pi hardware board, execute this command at the MATLAB Command Window.

showPins(mypi);
I2C pin callout for Raspberry Pi 3 Model B+ GPIO

The revised pin map of the Raspberry Pi hardware board shows the location of the I2C pins, I2C_SDA (GPIO 2) and I2C_SCL (GPIO 3), on the i2c-1 bus.

After physically connecting your I2C-based temperature sensor to the I2C pins, get the addresses of the I2C devices attached to the I2C bus 'i2c-1'.

scanI2CBus(mypi,'i2c-1');
ans =

  1×9 cell array
  {'0x48'}  {'0x50'}  {'0x51'}  {'0x52'}  {'0x53'}  {'0x54'}  {'0x55'}  {'0x56'}  {'0x57'}

Create a connection from MATLAB to the I2C sensor at I2C address '0x48'.

tempSensor = i2cdev(mypi,'i2c-1','0x48');

The temperature reading of the sensor is digitized into 12 bits in Normal mode, with 8 bits in MSB and 4 bits in LSB. Each LSB equals 0.0625 degrees Celsius. Write the register address to read from first byte and then read two bytes of data from it. Set the data type to uint8.

write(tempSensor, 0x0, 'uint8');
data = read(tempSensor, 2, 'uint8');
temp = (double(bitshift(int16(data(1)), 4)) + double(bitshift(int16(data(2)), -4))) * 0.0625;
temp =

   30.0625

Clear the active I2C sensor connection before disabling it. After you disable the I2C functionality, you can use the Raspberry Pi I2C pins as GPIO pins.

clear tempSensor;
disableI2C(mypi);
mypi=raspi;
           DeviceAddress: 'raspberrypi-hysdu8X38o'
                    Port: 18734
               BoardName: 'Raspberry Pi 3 Model B+'
           AvailableLEDs: {'led0'}
    AvailableDigitalPins: [2 3 4 5 6 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27]
    AvailableSPIChannels: {‘CE0’,’CE1’}
       AvailableI2CBuses: {}
             I2CBusSpeed: 100000
        AvailabelWebCams: mmal service 16.1 (platform:bcm2835-v4l2)
The GPIO 2 and GPIO 3 pins are now available and can be used as GPIO pins. No I2C buses are available for I2C interfacing.

Before using I2C again, enable the I2C functionality.

enableI2C(mypi);

When you enable I2C, you can change the I2CBusSpeed(mypi) property.

disableI2C(mypi);
enableI2C(mypi,400000);
I2CBusSpeed(mypi);
ans =

       400000

Input Arguments

collapse all

Connection to a Raspberry Pi hardware board, specified as a raspi object.

I2C bus speed, specified as either 100000 or 400000.

Example: 100000

Data Types: double

Introduced in R2014a