Main Content

Use the Raspberry Pi I2C Interface to Connect to a Device

This example shows how to create a connection to an I2C device, write data to the device, and read data from the device.

Warning

Excessive voltage and current can damage the Raspberry Pi™ hardware. Observe the manufacturer’s precautions for handling the Raspberry Pi hardware and connecting it to other devices. For more information, seehttps://www.raspberrypi.org/technical-help-and-resource-documents.

Create a connection to the Raspberry Pi hardware usingraspi.

mypi = raspi
mypi = raspi with properties: DeviceAddress: 'raspberrypi-computername' Port: 18725 BoardName: 'Raspberry PiModel B Rev 2' AvailableLEDs: {'led0'} AvailableDigitalPins: [4 7 8 9 10 11 14 15 17 18 22 23 24 25 27 30 31] AvailableSPIChannels: {} AvailableI2CBuses:{'i2c-0' 'i2c-1'}I2CBusSpeed:100000Supported peripherals

The default I2C bus speed is 100000 bits per second.

You can redisplay theAvailableI2CBusesandI2CBusSpeedproperties.

mypi.AvailableI2CBuses mypi.I2CBusSpeed
ans = 'i2c-0' 'i2c-1' ans = 100000

Show the location of the I2C pins on the GPIO header.

showPins(mypi)

The pin map shows that, for this model and revision of the board, thei2c-1bus is available on the GPIO header pinsI2C1_SDA (GPIO 2)andI2C1_SCL (GPIO 3).

树莓Pi hardware uses +3.3V. Do not connect Raspberry Pi hardware directly to devices that deliver higher voltages.

Before continuing, research the manufacturer’s product information to determine which settings the I2C device supports. Then, connect the Raspberry Pi board to the I2C device.

For example, with the MCP4725 12-bit DAC, connect:

  • I2C1_SDA (GPIO2)pin on the Raspberry Pi board to the SDA pin on the DAC.

  • I2C1_SCL (GPIO3)pin on the Raspberry Pi board to the SCL pin on the DAC.

  • GNDon the Raspberry Pi board to theGNDpin on the DAC.

  • +3.3Von the Raspberry Pi board to theVDDpin on the DAC.

  • VOUTpin on the DAC to the positive lead on the voltmeter.

  • GNDto the negative lead on the voltmeter.

Get the addresses of I2C devices that are attached to the I2C bus,'i2c-1'.

scanI2CBus (mypi'i2c-1')
ans = '0x62'

Create a connection to the I2C DAC at'0x62'连接和分配handle,i2cdac.

i2cdac = i2cdev(mypi,'i2c-1','0x62')
i2cdac = i2cdev with properties: Bus: 'i2c-1' Address: '0x62'

Write a value to the I2C device.

write(i2cdac,4092)

To read a value from an I2C sensor, physically connect the sensor, usescanI2CBusto get the address, usei2cdevto create a connection to the device. Then, usereadto get the value.

addr = scanI2CBus(mypi,'i2c-1') i2csensor = i2cdev(mypi,'i2c-1',char(addr)) read(i2csensor,1)

If you are not using I2C, disable I2C to make additional GPIO pins available.

Note

Before disabling I2C, clear thei2cdevobject, if created. For example,clear (i2csensor).

disableI2C(mypi)

When you use I2C again, enable I2C.

enableI2C(mypi)

To change the I2C bus speed,mypi.I2CBusSpeed, useenableI2Cwith thei2cBusSpeedargument.

disableI2C(mypi) enableI2C(mypi,400000) mypi.I2CBusSpeed
ans = 40000