disableI2C
Disable I2C interface
Syntax
Description
disableI2C(
disables the I2C bus.
You can then use the Raspberry Pi® I2C pins I2C1_SDA (GPIO 2) and I2C1_SCL (GPIO 3) as GPIO pins. The I2C
bus is enabled by default. To re-enable I2C, use mypi
)enableI2C
Note
MATLAB® Support Package for Raspberry Pi Hardware does not support disableI2C
function
for the Bullseye operating system. Under Hardware Setup use the Configure
Peripheral Modules window to disable I2C interface on the Raspberry
Pi.
.
Examples
Connect and Read Temperature Values from I2C-Based Temperature Sensor
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.
Run this command:
sudo raspi-config
Select Interfacing Options > I2C.
Select Yes when prompted to enable the I2C interface.
Select Yes when prompted to automatically load the I2C kernel module.
Select Finish.
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)
Note
If you encounter errors after running the above command, try using additional
arguments (as listed in raspi
) or refer to Troubleshoot Connecting Issues to Raspberry Pi Hardware.
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);
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)
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
mypi
— Connection to Raspberry Pi hardware board
raspi
object (default)
Connection to a Raspberry Pi hardware board, specified as a raspi
object.
Version History
Introduced in R2014a
See Also
scanI2CBus
| read
| write
| readRegister
| writeRegister
| enableI2C
| MATLAB Support Package for Raspberry Pi Hardware
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 bat365 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)