How to send data from MATLAB to Arduino an?

78 vues (au cours des 30 derniers jours)
Akarsh Sinha
Akarsh Sinha le 16 Déc 2012
Commenté : Reyhane Jalil le 2 Sep 2021
Hi, I was wondering how do I send data from MATLAB , say a number ,5 to my Arduino board. The board contains some code in it. For simplicity lets assume it multiplies the incoming number with 4. The output , 20 ,should be displayed in the command window. Is this possible ? I know about a.roundTrip('data') function. But the one in my query is something different. (I work on R2010a)
  2 commentaires
Josh
Josh le 18 Déc 2012
I have this same exact question. Any answers?

Connectez-vous pour commenter.

Réponse acceptée

Jonathan
Jonathan le 18 Déc 2012
Modifié(e) : Jonathan le 18 Déc 2012
I suppose you could do it through serial connections. If you have a serial port (or a serial to USB adapter and driver) you can create and open a COM object in MatLab to communicate with the Arduino board (usually arduino terminals D0 and D1 are serial TX/RX).
The code would be something like:
arduinoCom = serial('COM3','BaudRate',9600 . . . ); % insert your serial
%properties here
sendData = 5;
fopen(arduinoCom);
fprintf(arduinoCom,'%i',sendData); %this will send 5 to the arduino
fscanf(arduinoCom) %this will read response
%or use BytesAvailableFcn property of serial
You will have to look into using the correct terminators and do a bit of configuration with that. Let me know if this makes sense. Usually terminators are something like a linefeed / carriage return or an symbol (*, $, etc.)
  2 commentaires
Akarsh Sinha
Akarsh Sinha le 29 Mar 2013
Thanks Jonathan. I've figured out another way to do it. The Arduino IO Package works perfectly.It allows you to use Simulink too.
Break  Freezy
Break Freezy le 12 Mar 2015
Please can you help me , i wanna send 4 data from matlab to arduino how to realise without taping in command of matlab ?

Connectez-vous pour commenter.

Plus de réponses (4)

Nthai Papala
Nthai Papala le 19 Nov 2015
I need assistance. I need to send a binary data, say random bits (1 byte for example) from one computer to the other using LED and a phototransistor. The data must be generated in matlab and be sent over a serial port through arduino. I am using matlab 2015a and Arduino Uno and i need codes for both Arduino and Mat Lab.
Thanks in advance
  3 commentaires
Busra AYDIN
Busra AYDIN le 25 Jan 2021
hi, did you solve your problem?
Reyhane Jalil
Reyhane Jalil le 2 Sep 2021
hi i have same project ...can you send me detail of your work...reyhane.jalil.karim@gmail.com

Connectez-vous pour commenter.


Rouis Jihene
Rouis Jihene le 20 Juil 2017
Modifié(e) : Walter Roberson le 20 Juil 2017
Hello,
I want to send a value from matlab to arduino,i tried this code but it doesn't work:
in Arduino:
void setup() {
Serial.begin(9600);}
void loop() {
if (Serial.available()) {
Serial.println(Serial.read());}
}
In matlab:
a=serial('com7');
val=20;
fopen(a);
fwrite(a,'%i',val);
fclose(a);
Do you have any solution ? Please help and thanks.

Walter Roberson
Walter Roberson le 20 Juil 2017
Change
fwrite(a,'%i',val);
to
fprintf(a,'%i',val);
  6 commentaires
Walter Roberson
Walter Roberson le 20 Juil 2017
Try
fprintf(a,'%d\n',val);
Rouis Jihene
Rouis Jihene le 20 Juil 2017
Same problem! Do you have any other solution?(using Bluetooth or something else)

Connectez-vous pour commenter.


Syahmi Noordin
Syahmi Noordin le 29 Avr 2018
Modifié(e) : Syahmi Noordin le 29 Avr 2018
Hi, I want to ask about how to transfer more than one value from matlab GUI to arduino? I put 3 edit text functions in the Gui so I want the user to input value in each edit text function and those three values must be send to arduino for further calculation. Thanks.
  1 commentaire
Walter Roberson
Walter Roberson le 29 Avr 2018
get() the String property of the handles. fprintf(a, '%s %s %s\n', str1, str2, str3) where a is the arduino connection. This assumes that you want spaces between the strings and newline after the group of three.
You should probably do some sanity checking to be sure the strings are not empty and represent valid numbers.

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB Support Package for Arduino Hardware dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by