Sending values from Matlab to arduino using serial communication
Show older comments
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
fclose(arduino);
Arduino code is as:
int solenoidPin = 13; //This is the output pin on the Arduino
int doi;
void setup()
{
Serial.begin(9600);
//pinMode(13, OUTPUT);
pinMode(solenoidPin, OUTPUT); //Sets that pin as an output
}
void loop()
{
if(Serial.available()>0)
{
doi = Serial.read();
//Serial.println(doi);
//Serial.println("\n");
//doi = doi - 48;
if (doi == 1)
{
//Serial.println(1);
digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON
delay(1000); //Wait .15 Second
digitalWrite(solenoidPin, LOW);
}
else if (doi == 2)
{
//Serial.println(2);
digitalWrite(solenoidPin, HIGH); //Switch Solenoid OFF
delay(2000); //Wait .165 Second
digitalWrite(solenoidPin, LOW);
}
else
{
//Serial.println(3);
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.
Thanks.
5 Comments
Walter Roberson
on 18 Feb 2017
I would probably use
fwrite(arduino, uint8(doi));
Naseeb Gill
on 21 Feb 2017
ahmed elshawadfy
on 13 Nov 2017
how do you know that it takes 2 sec to open?
Sravani Vanama
on 19 Nov 2019
how to print the values send from matlab to arduino??
Walter Roberson
on 20 Nov 2019
There are a few possibilities for display:
- you can send the values to some kind of display such as an LCD display; https://www.arduino.cc/en/Tutorial/HelloWorld
- you can send the values back to MATLAB and have MATLAB display them
- if you use a different connection method between MATLAB and arduino, so that the communications between MATLAB and arduino is not through the serial port monitor, then you can send the values to the serial port and use some kind of monitor system on there.
- You can use a serial port (or USB port) monitor on the MATLAB host side to see what is getting sent. https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/capture-and-view-ing-usb-traces-with-microsoft-message-analyzer- . Or see https://freeusbanalyzer.com/ which permits short monitoring for free (and presumably there is a paid version.)
Answers (3)
Walter Roberson
on 21 Feb 2017
0 votes
"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.
http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
You could have a look at https://www.mathworks.com/matlabcentral/fileexchange/26371-simple-gui-for-serial-port-communication to see how they set up communications.
1 Comment
Naseeb Gill
on 21 Feb 2017
Edited: Naseeb Gill
on 21 Feb 2017
Rouis Jihene
on 31 May 2017
0 votes
Hello, i tried the code but i don't get the value (doi) in arduino ! why? please help
3 Comments
Walter Roberson
on 31 May 2017
On the arduino side, you are probably trying to read the data as text instead of as binary. The replacement for
fprintf(arduino, '%s', char(doi)); % send answer variable content to arduino
should be
fprintf(arduino, '%d\n', doi); % send answer variable content to arduino
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
Walter Roberson
on 4 Jun 2017
Sorry, I do not have an arduino to test with.
Azrg
on 23 Oct 2019
0 votes
The thing you want to send has to be in a while loop like
while true
fprint(something);
end
Because Matlab has to keep trying to send the information until Arduino receives it.
Categories
Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!