App Designer 2021a - Adafruit AHT20 Temperature Sensor (I2C) - Data Acquisition

2 views (last 30 days)
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

Das Siddharth
Das Siddharth on 28 Apr 2021
To whomsoever it might help, I was able to solve this on my own. The key is to tailor the write commands as per the app designer. For e.g. in the startupFcn(app) :
temp = device(a,'I2CAddress',0x38);
write(temp,0x70,'uint8'); % telling the sensor to write
function startButtonPushed (app,event)
write(temp,0xAC,'uint8'); %writing this hex code to initiate measurement
write(temp,0x71,'uint8'); %telling the sensor that now you're ready to read
pause(0.80);
v = read(temp,2,'uint32');
.
.
.
end
Then afterwards as per some bitshifting and stuff you'll be ready to acquire data in real-time.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!