Sending values from Matlab to arduino using serial communication
Mostrar comentarios más antiguos
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 comentarios
Walter Roberson
el 18 de Feb. de 2017
I would probably use
fwrite(arduino, uint8(doi));
Naseeb Gill
el 21 de Feb. de 2017
ahmed elshawadfy
el 13 de Nov. de 2017
how do you know that it takes 2 sec to open?
Sravani Vanama
el 19 de Nov. de 2019
how to print the values send from matlab to arduino??
Walter Roberson
el 20 de Nov. de 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.)
Respuestas (3)
Walter Roberson
el 21 de Feb. de 2017
0 votos
"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 comentario
Naseeb Gill
el 21 de Feb. de 2017
Editada: Naseeb Gill
el 21 de Feb. de 2017
Rouis Jihene
el 31 de Mayo de 2017
0 votos
Hello, i tried the code but i don't get the value (doi) in arduino ! why? please help
3 comentarios
Walter Roberson
el 31 de Mayo de 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
el 4 de Jun. de 2017
Hello Mr Walter Roberson, even i use the instruction that you told me but i doesn't work ! please help
Walter Roberson
el 4 de Jun. de 2017
Sorry, I do not have an arduino to test with.
Azrg
el 23 de Oct. de 2019
0 votos
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.
Categorías
Más información sobre MATLAB Support Package for Arduino Hardware en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!