Display Arduino voltage and resistance output on MATLAB GUI

14 views (last 30 days)
I have developed a MATLAB GUI using Appdesign. This GUI ommuniates with arduino mega. the arduino is a diode and resistor tester. The gui selects either the resistor or diode tester. this also then displays the measured values on a 16x2 lcd. the problem i am facing is displaying these vaules in the GUI in a text box or simply displaying the value.
PLease help. thanks in advance.
  2 Comments
trishen beeharie
trishen beeharie on 23 Dec 2021
Could you please advise how to do it programitically. below is my arduino code then matlab code, basically i want di and R2 values to be displayed on matlab gui.
#include <Wire.h>
#include <hd44780.h>
#include <Wire.h>
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
hd44780_I2Cexp lcd;
int mode = 1;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
}
void loop() {
if (Serial.available() > 0) // if there is data to read
{
//digitalWrite(ledPin2,HIGH);
float matlabData = Serial.parseFloat(); // read data
if (matlabData==1)
{
mode=1;
}
else if (matlabData==2)
{
mode=2;
}
}
if(mode==1)
{
float D=0;
float Di=0;
D=analogRead(A0);
Di = D* (5.0/1023.0);
//if (D>4){lcd.setCursor(0,1);
//lcd.print("No diode");}
if(Di>0.08)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Voltage drop");
lcd.setCursor(0,1);
lcd.print(Di);
lcd.print("V");
delay(200);
}
}
else if(mode==2)
{
float raw = 0;
float Vin = 5;
float Vout = 0;
float R1 = 977; // value of 1k resistor
float R2 = 0;
float buffer = 0;
lcd.clear();
raw = analogRead(A1);
if(raw==0){lcd.setCursor(1, 1);
lcd.print(" Open ");}
if(raw){
buffer = raw * Vin;
Vout = (buffer)/1023.0;
buffer = (Vin/Vout) - 1;
R2 = R1 * buffer;
lcd.setCursor(0,0);
lcd.print("Reisistance");
lcd.setCursor(3, 1);
if(R2>999.99){
lcd.print(R2/977);
lcd.print(" K Ohm ");
}else{
lcd.setCursor(3, 1);
lcd.print(R2);
lcd.print(" Ohm ");
}
delay(200);
}
}
}
classdef ComponentTesterahbed < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
StartButton matlab.ui.control.Button
ResistanceTextArea matlab.ui.control.TextArea
ResistanceTextAreaLabel matlab.ui.control.Label
VoltagedropTextArea matlab.ui.control.TextArea
VoltagedropTextAreaLabel matlab.ui.control.Label
COMPONENTTESTERLabel matlab.ui.control.Label
OptionsButtonGroup matlab.ui.container.ButtonGroup
ResistorTesterButton matlab.ui.control.RadioButton
DiodeTesterButton matlab.ui.control.RadioButton
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: StartButton
function StartButtonPushed(app, event)
mode=0;
if(app.OptionsButtonGroup.SelectedObject==app.DiodeTesterButton)
mode="1";
elseif(app.OptionsButtonGroup.SelectedObject==app.ResistorTesterButton)
mode="2";
end
clear ar;
global ar;
ar = serial('COM5','BaudRate',9600);
%Open arduino USB port
fopen(ar);
pause(2);
fprintf(ar,'%s',mode);
pause(2);
%Close arduino USB port
fclose(ar);
end
% Selection changed function: OptionsButtonGroup
function OptionsButtonGroupSelectionChanged(app, event)
selectedButton = app.OptionsButtonGroup.SelectedObject;
if selectedButton==app.DiodeTesterButton
app.ResistanceTextArea.Visible='off';
app.ResistanceTextAreaLabel.Visible='off';
app.VoltagedropTextArea.Visible='on';
app.VoltagedropTextAreaLabel.Visible='on';
elseif selectedButton==app.ResistorTesterButton
app.VoltagedropTextArea.Visible='off';
app.VoltagedropTextAreaLabel.Visible='off';
app.ResistanceTextArea.Visible='on';
app.ResistanceTextAreaLabel.Visible='on';
end
end
% Callback function
function VoltagedropTextAreaValueChanged(app, event)
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 326 461];
app.UIFigure.Name = 'MATLAB App';
% Create OptionsButtonGroup
app.OptionsButtonGroup = uibuttongroup(app.UIFigure);
app.OptionsButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @OptionsButtonGroupSelectionChanged, true);
app.OptionsButtonGroup.TitlePosition = 'centertop';
app.OptionsButtonGroup.Title = 'Options';
app.OptionsButtonGroup.Position = [1 278 325 110];
% Create DiodeTesterButton
app.DiodeTesterButton = uiradiobutton(app.OptionsButtonGroup);
app.DiodeTesterButton.Text = 'Diode Tester';
app.DiodeTesterButton.Position = [11 64 89 22];
app.DiodeTesterButton.Value = true;
% Create ResistorTesterButton
app.ResistorTesterButton = uiradiobutton(app.OptionsButtonGroup);
app.ResistorTesterButton.Text = 'Resistor Tester';
app.ResistorTesterButton.Position = [11 43 102 22];
% Create COMPONENTTESTERLabel
app.COMPONENTTESTERLabel = uilabel(app.UIFigure);
app.COMPONENTTESTERLabel.HorizontalAlignment = 'center';
app.COMPONENTTESTERLabel.FontSize = 20;
app.COMPONENTTESTERLabel.FontWeight = 'bold';
app.COMPONENTTESTERLabel.Position = [6 416 316 24];
app.COMPONENTTESTERLabel.Text = 'COMPONENT TESTER';
% Create VoltagedropTextAreaLabel
app.VoltagedropTextAreaLabel = uilabel(app.UIFigure);
app.VoltagedropTextAreaLabel.HorizontalAlignment = 'right';
app.VoltagedropTextAreaLabel.Position = [123 204 73 22];
app.VoltagedropTextAreaLabel.Text = 'Voltage drop';
% Create VoltagedropTextArea
app.VoltagedropTextArea = uitextarea(app.UIFigure);
app.VoltagedropTextArea.Position = [84 128 150 60];
% Create ResistanceTextAreaLabel
app.ResistanceTextAreaLabel = uilabel(app.UIFigure);
app.ResistanceTextAreaLabel.HorizontalAlignment = 'right';
app.ResistanceTextAreaLabel.Visible = 'off';
app.ResistanceTextAreaLabel.Position = [130 204 65 22];
app.ResistanceTextAreaLabel.Text = 'Resistance';
% Create ResistanceTextArea
app.ResistanceTextArea = uitextarea(app.UIFigure);
app.ResistanceTextArea.HorizontalAlignment = 'center';
app.ResistanceTextArea.Visible = 'off';
app.ResistanceTextArea.Position = [85 128 154 60];
% Create StartButton
app.StartButton = uibutton(app.UIFigure, 'push');
app.StartButton.ButtonPushedFcn = createCallbackFcn(app, @StartButtonPushed, true);
app.StartButton.Position = [125 254 100 22];
app.StartButton.Text = 'Start';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = ComponentTesterahbed
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end

Sign in to comment.

Answers (1)

Sivani Pentapati
Sivani Pentapati on 4 Jan 2022
Hi trishen,
Based on my understanding, you want to display the measured values from arduino board in a GUI made with App Designer. Firstly you can receive the measured values in the form of serial data from the device using Arduino Hardware Simulink Model. Secondly you can execute this model at the backend to fetch the values and update them accordingly in the App Designer. Please refer to this link of how to interact with Simulink Models from App Designer.

Community Treasure Hunt

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

Start Hunting!

Translated by