How can I plot real time graph from i2c data that is transmitted over bluetooth?

18 ビュー (過去 30 日間)
Yong Kim
Yong Kim 2017 年 3 月 4 日
回答済み: Technical 2023 年 2 月 1 日
I am trying to plot a real time graph on MATLAB of wirelessly transmitted digital sensor data(I2C). The structure of the system is as follows: MCP9808(Digital Temperature sensor) -> Arduino Uno with HC-05(Bluetooth Module) -> MATLAB. I managed to plot a real time graph of the analogue temperature sensor.
The following is Arduino code which receives digital temperature value and transmits over to MATLAB
#include <Wire.h>
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(4, 5);
void setup()
{
Serial.begin(9600);
Wire.begin();
}
void loop()
{
uint16_t t;
Wire.beginTransmission(0x18);
Wire.write(0x05);
Wire.endTransmission();
delay(100);
Wire.requestFrom(0x18, 2);
t = Wire.read();
t <<= 8;
t |= Wire.read();
float temp = t & 0x0FFF;
temp /= 16.0;
if (t & 0x1000) temp -= 256;
Serial.println(temp);
BTSerial.println(temp);
delay(1000);
}
and this is the MATLAB code:
clear all; clc;
delete(instrfindall)
instrreset;
b = Bluetooth('YONG',1);
fopen(b);
figure
h = animatedline;
ax = gca;
ax.YGrid = 'on';
stop = false;
startTime = datetime('now');
while ~stop
a = str2num(fscanf(b));
% Get current time
t = datetime('now');
% Add points to animation
addpoints(h,datenum(t),a)
% Update axes
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
end
fclose(b);
It will be greatly appreciated if I could get some tips on this. Thank you in advance.
  2 件のコメント
Walter Roberson
Walter Roberson 2017 年 3 月 4 日
What difficulty are you seeing on the MATLAB side?
Yong Kim
Yong Kim 2017 年 3 月 4 日
I can't graph any values that I have received from digital sensor via bluetooth, the error I get is
Warning: Unsuccessful read: A timeout occurred before the Terminator was reached.
Error using matlab.graphics.animation.AnimatedLine/addpoints
Argument Y must be a vector.
Error in untitled2 (line 19)
addpoints(h,datenum(t),a)
Could you possibly help me on this please?
Thank you

サインインしてコメントする。

採用された回答

Sachin Kumar
Sachin Kumar 2017 年 3 月 7 日
The MATLAB code works fine here. The problem is in the arduino code, you need to initial "BTSerial" using below function in your setup(){}:
BTSerial.begin(baud_rate);
You can use disp(a) in MATLAB code to check whether the value received in MATLAB is proper or not.
  2 件のコメント
Yong Kim
Yong Kim 2017 年 3 月 7 日
Thank you so much, that was very silly of me!
Technical
Technical 2023 年 2 月 1 日
CAN I PLOT LIVE STREAMING DATA IN TO MATLAB?..DATA FROM AN ANALOGOUS SENSOR. ARDUINO UNO. PLEASE HELP.I AM USING MATLAB MOBILE

サインインしてコメントする。

その他の回答 (1 件)

Technical
Technical 2023 年 2 月 1 日
In a simplest way to get plot of an analogous sensor data from arduino to android through blue tooth module as like as serial plotter.? How to

カテゴリ

Help Center および File ExchangeModeling についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by