How to interface TCA9548A in Matlab Raspberry Pi?
18 次查看(过去 30 天)
显示 更早的评论
I am currently using the Matlab Raspberry Pi support package to try connect 2 I2C Sensor which have the address of 0x52 (TOF10120), and i use the TCA9548A I2C bus multiplexer to change one of the sensor address, but when i scan the I2C bus of the Raspberry i can only get the address of the Multiplexer which is 0x70 my setup is:
- 5V to Vin
- GND to GND
- SDA1 to SDA
- SCL1 to SCL
- SCL of sensor to SC2 and SC3 - GND to GND
- SDA of sensor to SD2 and SD3 - Vcc is 5V for both sensors
How do I interact with the multiplexer and obtain separate address from 2 same address sensor? Please Help
mypi = raspi
mypi =
raspi with properties:
DeviceAddress: 'raspberrypi-FDrv8rH0vE.local'
Port: 18734
BoardName: 'Raspberry Pi 2 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'}
AvailableWebcams: {'bcm2835-isp (platform:bcm2835-isp)','mmal service 16.1 (platform:bcm2835-v4l2)'}
I2CBusSpeed: 100000
scanI2CBus(mypi,'i2c-1')
ans =
1×1 cell array
{'0x70'}
0 个评论
回答(1 个)
Sylvain
2021-11-22
I am not sure if the MATLAB API would allow it, as it checks i2c addresses on the bus before detection
From my understanding if the API was allowing declaration of slaves without checks you would write the following commands:
if ~exist('tca9548A','var')
tca9548A = rpi.i2cdev('i2c-1',"0x70")
end
if ~exist('TOF10120','var')
tof10120 = rpi.i2cdev('i2c-1',"0x52") %<=== This will throw an error
end
tca9548A.write(1,'uint8'); %select chanel 0, disable others
tof10120.write(command,'uint8')
tca9548A.write(3,'uint8'); %select chanel 1, disable others
tof10120.write(command,'uint8'); %command to be determine as per the datasheet
tof10120.read(n,'uint8'); %n = number of byte to read
Hopefully a staff member could help. This question was also asked previously: https://uk.mathworks.com/matlabcentral/answers/456783-how-to-interface-tca9548a-1-8-multiplexer-with-simulink
1 个评论
Ryman Hashem
2021-12-7
编辑:Ryman Hashem
2021-12-7
This is a quite annoying problem and i hope there would be god examples in Matlab to understand the i2c communication with Matlab (currently only one example).
So ifigure out a wway to get the address problem with this quistion, however i dont have 2 sensors at the moment, but hopfully can work with multiple sensors. The idea is to communicate with the ultiplexer first and select one of the channels, then, make a ne connection with the device like there is no multiplexer. Please correct me if i"m wrong and hofully it will work. Note, this example for arduino, but the conecept shpud be the same for Rasbery pi.
%work for a single chip MCP4725 (address 0x60) and multiplexer (address 0x70)
a = arduino('COM3','mega2560','Libraries','I2C'); %Sometimes scanning of address after this line might show bith the multiplexer and the sensor address
%if both addresses not shown, then create the first device with the
%multiplexer address
dev = device(a,'I2CAddress','0x70'); %create a device with the multiplexer first
write(dev,1); %then write to a selected channel (channel 0 is selected here)
addrs = scanI2CBus(a); % now you can scan and you will see the address of the sensor (0x60)
dev1=device(a,'I2CAddress','0x60'); %make another device so you can communicate with the sesnsor
writeRegister(dev1,0,255) %write to the sensor register (in this example, I'm writting a value to the DAC)
%an update with 2 sensors and seems working fine
a = arduino('COM3','mega2560','Libraries','I2C');
dev = device(a,'I2CAddress','0x70');
addrs = scanI2CBus(a)
write(dev,1);%select channel one
dev1=device(a,'I2CAddress','0x60');
writeRegister(dev1,0,250)%0 address concidered general call as explained in the data sheet
voltage = readVoltage(a,'A0')
write(dev,2);%select channel 2
writeRegister(dev1,0,250)% We use the same (dev1) that was created as it is the same address
voltage = readVoltage(a,'A2')
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!