App Designer 2021a - Adafruit AHT20 Temperature Sensor (I2C) - Data Acquisition
Show older comments
I am trying to acquire temperature data from the latest Adafruit's AHT20 sensor which has I2C communication. My plan was to make a GUI using the App Designer and with the push of a button to plot the data. I am not sure as how to begin the communication with the sensor as the datasheet is not very helpful. I have also attached the particular page of the datasheet for further reference. I saw an example : https://www.mathworks.com/help/supportpkg/arduinoio/examples/measure-temperature-from-i2c-device-on-arduino-hardware.html
But the address and read write is only specific to that particular sensor (Or is it ?). Bottomline is I want to acquire data but I am unable to do so. My initial code for attempting to do so is.
%% Startup Function
clear a;
delete(instrfind);
clear all; %#ok<CLALL>
clc;
global a temp;
a = arduino('COM5',"Uno",'Libraries','I2C');
scanI2CBus(a);
temp = device(a,'I2CAddress',0x38);
write(temp,0xBE,'uint8'); % initialization as per the data sheet
write(temp,0xAC,'uint8'); % beginning measurement (I guess)
app.StatusLamp.Color = [0.90,0.90,0.90];
%% Start Button Function
global temp;
app.flag = 0;
app.done = 0;
app.h = animatedline;
app.stop = false;
startTime = datetime('now');
while ~app.stop
% v = readVoltage(a,'A0');
v = read(temp, 2, 'uint8');
temperature = (double(bitshift(int16(v(1)), 4)) + double(bitshift(int16(v(2)), -4))) * 0.0625;
t = datetime('now') - startTime;
if v >= 0.5 && v <= 0.65
app.StatusLamp.Color = 'g';
else
app.StatusLamp.Color = [0.90,0.90,0.90];
end
%add point to animation
plot(app.UIAxes,datenum(t),temperature,'-mo',...
'LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',5);
addpoints(app.h,datenum(t),temperature);
app.UIAxes.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
hold(app.UIAxes,'on');
% fprintf(app.fileID,'%f %f \n','Time','Voltage');
app.stop = app.flag;
app.TemperatureCEditField.Value = v;
end
Accepted Answer
More Answers (0)
Categories
Find more on MATLAB Parallel Server in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!