Use BeagleBone Black I2C Interface to Connect to Device
This example shows how to create a connection to an I2C device, write data to the device, and read data from the device.
Caution
Excessive voltage and current can damage the BeagleBone® Black hardware. Observe the manufacturer precautions for handling the
BeagleBone Black hardware and connecting it to other devices. For more information, see
the local copy of the BeagleBone drivers and documentation in the BeagleBone Black Getting
Started
folder on your host computer, or Getting Started with BeagleBone
Black.
Create a connection to the BeagleBone Black hardware.
bbb = beaglebone
bbb = beaglebone with properties: DeviceAddress: '192.168.7.2' BoardName: 'BeagleBone Black Rev 00C0' AvailableLEDs: {'USR0' 'USR1' 'USR2' 'USR3'} AvailableDigitalPins: {1x29 cell} AvailableAnalogPins: {'AIN0' 'AIN1' 'AIN2' 'AIN3' 'AIN4' 'AIN5' 'AIN6'} AvailablePWMPins: {} AvailableSPIChannels: {} AvailableI2CBuses: {'i2c-1'} AvailableSerialPorts: {} AvailableWebcams: {}
You can redisplay the
AvailableI2CBuses
property.bbb.AvailableI2CBuses
ans = 'i2c-1'
Show the location of the I2C pins on the GPIO header.
showAllPins(bbb)
For this model and revision of the board, the pin map shows that the
i2c-1
bus is available on the GPIO header pinsI2C1_SDA (P9_18)
andI2C1_SCL (P9_17)
.BeagleBone Black hardware uses
+3.3V
. Do not connect BeagleBone Black hardware directly to devices that deliver higher voltages.Before continuing, research the manufacturer product information to determine which settings the I2C device supports. Then, connect the BeagleBone Black hardware to the I2C device.
For example, with the MCP4725 12-bit DAC, connect:
I2C1_SDA (P9_18)
pin on the BeagleBone Black hardware to the SDA pin on the DAC.I2C1_SCL (P9_17)
pin on the BeagleBone Black hardware to the SCL pin on the DAC.GND
on the BeagleBone Black hardware to theGND
pin on the DAC.+3.3V
on the BeagleBone Black hardware to theVDD
pin on the DAC.VOUT
pin on the DAC to the positive lead on the voltmeter.GND
to the negative lead on the voltmeter.
Get the addresses of I2C devices that are attached to the I2C bus,
'i2c-1'
.scanI2CBus(bbb,'i2c-1')
ans = '0x62'
Create a connection to the I2C DAC at
'0x62'
and assign that connection to an object,i2cdac
.i2cdac = beaglebone.i2cdev(bbb,'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, use
scanI2CBus
to get the address, and usei2cdev
to create a connection to the device. Then, useread
to get the value.addr = scanI2CBus(bbb,'i2c-1') i2csensor = beaglebone.i2cdev(bbb,'i2c-1',char(addr)) read(i2csensor,1)
When you are finished using the I2C interface, restart the hardware to make additional GPIO pins available.