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 :
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;
clear all; %#ok<CLALL>
global a temp;
a = arduino('COM5',"Uno",'Libraries','I2C');
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';
app.StatusLamp.Color = [0.90,0.90,0.90];
%add point to animation
'MarkerFaceColor',[.49 1 .63],...
app.UIAxes.XLim = datenum([t-seconds(15) t]);
% fprintf(app.fileID,'%f %f \n','Time','Voltage');
app.stop = app.flag;
app.TemperatureCEditField.Value = v;

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
v = read(temp,2,'uint32');
Then afterwards as per some bitshifting and stuff you'll be ready to acquire data in real-time.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!