Connecting arduino to matlab via BT while using I2C communication

1 次查看(过去 30 天)
I am currently on a project trying to send data from arduino to matlab. I have one arduino uno (master) that is recieving data from arduino uno (slave) and another board via I2C. Once the master gets that date I want it to send the data to matlab via BT HC05 connection. To start this procces I use the next code (this is the simplest version):
arduinoObj = serialport('COM4',9600);
pause(3);
flush(arduinoObj);
writeline(arduinoObj,'1');
arduinoObj.NumBytesWritten;
pause(1);
if arduinoObj.NumBytesAvailable>0
arduinoObj.NumBytesAvailable
x = readline(arduinoObj);
end
The problem is that the writeline command sometimes works and sometimes does not. With just an arduino, the board with I2C and de BT HC05 it works, the problem starts when adding a second arduino.

采纳的回答

Raj
Raj 2023-9-14
Hi Nerea,
I understand you are facing issues with simultaneous serial communications in Arduino Uno.
Here are some viewpoints you should consider before proceeding-
  • Arduino Uno has only one hardware serial port and 2 software serial ports.
  • You can have multiple software serial instances in your code, but you can only use one at a time.
Refer to the Arduino uno documentation for more
You need to take extra care while using simultaneous serial communications.
Also, it is likely that your ‘writeline’ function is only working sometimes due to the absence of a loop which is not making the Arduino send the data repeatedly.
Additionally, you can try switching to Arduino Mega that has 3 hardware ports as a master device which will help in this scenario of multiple serial communications.
You can also refer to the following documentation by MathWorks regarding connecton of Arduino with MATLAB
I hope this resolves the query you had!

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by