I am doing some computations on Matlab and I need to send those values to an Arduino Leonardo through USB serial connection. I need to send 2 variables which can vary from -400 to +400. I'm saying their values because I was able to do this with small positive values (unsigned byte), but not larger and negative numbers. Please help! Thank you

  • 2
    Consider sending printable values just as if you had typed them into the serial monitor. It is not the most efficient solution, but it is one of the simplest, avoiding the format-matching and synchronization challenges of multibyte binary data. – Chris Stratton Feb 13 '15 at 7:20
  • I am analyzing images from webcam and continuously sending data to Arduino. I solved the part of sending data, but the problem is that the Serial.Available() is updated only once. It receives much of the data as one string. I need it to be updated after every sending from Matlab. There is no interrupt with each serial fprintf basically. – Martin V Feb 17 '15 at 23:13
  • Communications on the leonardo is packetized, so data will arrive in chunks that may or may not match your initial message size. – Chris Stratton Feb 17 '15 at 23:17
  • Turns out Matlab needed me to define a '\n' terminator in the serial fprintf function along with the type of data to be transferred. The problem was solved, thank you for the help. – Martin V Feb 18 '15 at 23:34

You cannot send larger values because byte only covers the range from 0-255.

To send larger values, you can break your int variable into 2 byte variables. Here's an example:

// On Arduino
int myVar = -123;
byte myVar_HighByte = myVar>>8; // get the high byte
byte myVar_LowByte = myVar; // get the low byte
// x86 compatible machines are little-endian so we send the low byte first

s = serial('COM10')
myVar = fread(s,1,'int16')

Disclaimer: Syntax might not be entirely correct, since I have no MATLAB or Arduino near me when I typed this. But you should get the idea. ;-)

Edit: On second thought, it might be easier to use a pointer.

// On Arduino
float myFloat = 3.14159265359;
byte* ptr = (byte*) (&myFloat);

s = serial('COM10')
myVar = fread(s,1,'float')

Having said that, you will still have to take care of the endianness, if you use a different microcontroller.

  • you can use all C datatypes available, is this case a short is the better option as it is 2 bytes and matlab can handle it with int16 – brtiberio Nov 19 '15 at 16:41
  • MATLAB can handle a wide range of numeric data types (see link.) The example code above applies to short (int16) data types just as well, but take note that only two bytes are sent instead of four bytes in the case of float data types. – bot3663369 Nov 20 '15 at 1:28
  • Trivia: Single precision floating point data is usually referred in C++ as float, whereas in MATLAB it's more commonly referred to as single. – bot3663369 Nov 20 '15 at 1:35
  • I agree with sentences stated up, but what I was trying to say is that there is no need to use int myVar and then shift it into lower and higher bytes because we can simply use short myVar and Serial.write(myVar). The float part is 100% correct. – brtiberio Nov 20 '15 at 17:32
  • Thanks for pointing that out. If the OP only needs integers within the range of -400:400, short would do the job. Clearly, I was too eager to provide a more generic solution to handle multi-types. – bot3663369 Nov 25 '15 at 0:55

If you want to send your data directly from Matlab to the Arduino, you can try using the "Matlab Arduino Support package". It lets you read/write pins on the arduino directly from Matlab command line or script just like you would with a VERY LOW END Data Acquisition card.

This has the advantage of removing the burden of managing the serial communication. I don't know how it handles large numbers though.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.