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:
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.
To create a connection from MATLAB to the Raspberry Pi board, execute this command at the MATLAB Command Window.
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.
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'
.
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
'.
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
.
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.
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.
When you enable I2C, you can change the I2CBusSpeed(mypi)
property.