Clear Filters
Clear Filters

Sending values from Matlab to arduino using serial communication

14 views (last 30 days)
I want to send numeric value from matlab to arduino but code is not working. Matlab code is as:
doi = 3 ;
arduino=serial('COM5','BaudRate',9600); % create serial communication object
fopen(arduino); % initiate arduino communication
fprintf(arduino, '%s', char(doi)); % send answer variable content to arduino
Arduino code is as:
int solenoidPin = 13; //This is the output pin on the Arduino
int doi;
void setup()
//pinMode(13, OUTPUT);
pinMode(solenoidPin, OUTPUT); //Sets that pin as an output
void loop()
doi =;
//doi = doi - 48;
if (doi == 1)
digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON
delay(1000); //Wait .15 Second
digitalWrite(solenoidPin, LOW);
else if (doi == 2)
digitalWrite(solenoidPin, HIGH); //Switch Solenoid OFF
delay(2000); //Wait .165 Second
digitalWrite(solenoidPin, LOW);
digitalWrite(solenoidPin, HIGH); //Switch Solenoid OFF
delay(3000); //Wait .180 Second
digitalWrite(solenoidPin, LOW);
I used str2num(doi) also instead of fprintf(arduino, '%s', char(doi)) but no output.
Please give suggestion to correct this.
Walter Roberson
Walter Roberson on 20 Nov 2019
There are a few possibilities for display:

Sign in to comment.

Answers (3)

Walter Roberson
Walter Roberson on 21 Feb 2017
"But what the problem is I do not know how to use _fopen(arduino) command between functions in GUI?"
fopen once and save the object somewhere so that you do not need to fopen again.
  1 Comment
Naseeb Gill
Naseeb Gill on 21 Feb 2017
Edited: Naseeb Gill on 21 Feb 2017
I already tried handles as you suggested as below:
function varargout = exam(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @exam_OpeningFcn, ...
'gui_OutputFcn', @exam_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
gui_mainfcn(gui_State, varargin{:});
% End initialization code - DO NOT EDIT
function exam_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = exam_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function edit1_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
% --- Executes on button press in pushbutton1.
% pushbutton to create serial communication between matlab and arduino
function pushbutton1_Callback(hObject, eventdata, handles)
arduino=serial('COM5','BaudRate',9600); % create serial communication object on port COM4
fopen(arduino); % initiate arduino communication
handles.arduino = arduino;
% --- Executes on button press in pushbutton2.
% After enter value in edit box press pushutton to send data to arduino
function pushbutton2_Callback(hObject, eventdata, handles)
ard = handles.arduino;
number = str2num(char(get(handles.edit1,'String')));
fprintf(ard, '%i', number);
% --- Executes on button press in pushbutton3.
% to clear the port and close arduino communication
function pushbutton3_Callback(hObject, eventdata, handles)
But arduino is not working. It just blink once when I gave input through edit box. And it is not showing any error too. Can you suggest me the solution by seeing above matlab GUI code. Thanks.

Sign in to comment.

Rouis Jihene
Rouis Jihene on 31 May 2017
Hello, i tried the code but i don't get the value (doi) in arduino ! why? please help
Rouis Jihene
Rouis Jihene on 4 Jun 2017
Hello Mr Walter Roberson, even i use the instruction that you told me but i doesn't work ! please help

Sign in to comment.

Azrg on 23 Oct 2019
The thing you want to send has to be in a while loop like
while true
Because Matlab has to keep trying to send the information until Arduino receives it.


Find more on MATLAB Support Package for Arduino Hardware 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!