enableI2C
Enable I2C interface
Add-On Required: This feature requires the MATLAB Support Package for BeagleBone Black Hardware add-on.
Description
enableI2C(
enables the indicated I2C bus and configures the GPIO pins for use as I2C pins.
After you start the hardware, the bbb
, bus_number
)i2c-1
bus is enabled by
default.
Examples
Enable, Connect, and Exchange Data with I2C Device
You can connect and exchange data with an I2C
device on I2C bus 1
.
Create a connection from the MATLAB® software 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: {}
Enable I2C bus 1
.
enableI2C(bbb,1)
Display AvailableI2CBuses
.
bbb.AvailableI2CBuses
ans = 'i2c-1' 'i2c-2'
AvailableI2CBuses
displays default bus 2
as i2c-1
.
The 1
is an internal index number. When you enable
bus 1
, AvailableI2CBuses
displays
it as i2c-2
.
After physically connecting your I2C device to the I2C
bus 1
pins, get the addresses of two I2C devices
attached to I2C bus 1
.
scanI2CBus(bbb,'i2c-2')
ans = {'0x55','0x20'}
Create a connection, i2csensor
,
from the MATLAB software to the I2C sensor at '0x20'
.
i2csensor = i2cdev(bbb,'i2c-2','0x20')
i2csensor = I2C with Properties: Bus: i2c-2 I2CAddress: 0x20 TimeOut: 10
Read two uint8
numbers from the sensor.
output1 = read(i2csensor,2);
Read the value of register 14 from the sensor.
output2 = readRegister(i2csensor,14);
Create a connection, i2cdisplay
,
from the MATLAB software to an I2C LED display at '0x55'
.
i2cdisplay = i2cdev(bbb,'i2c-2','0x55')
i2cdisplay = I2C with Properties: Bus: i2c-2 I2CAddress: 0x55 TimeOut: 10
Write characters to the display.
write(i2cdisplay,[hex2dec('20') hex2dec('51')])
Write a scalar hexadecimal value, hex2dec('08')
, to register 3
on an I2C device. uint8
is the default data type.
writeRegister(i2cdisplay,3,hex2dec('08'))
Input Arguments
bbb
— Connection to BeagleBone Black hardware
beaglebone
object
BeagleBone Black connection created using beaglebone
, specified as an
object.
bus_number
— Reference to specific I2C bus
2
(default) | 1
The bus number identifies the I2C bus being enabled. By default, bus
2
is always enabled.
AvailableI2CBuses
displays default bus
2
as i2c-1
. The
1
is an index number. When you enable bus
1
, AvailableI2CBuses
displays it
as i2c-2
.
I2C bus 1
shares pins with SPI bus
0
.
Example: 1
Version History
Introduced in R2015a
See Also
beaglebone
| scanI2CBus
| i2cdev
| read
| write
| readRegister
| writeRegister
| BeagleBone Black
Topics
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)