I have used a load cell. Arduino is sending the load perfectly. When the load sensed by arduino crossess some value I want to send a simple signal to matlab. Then a MATLAB should capture the image. If someone has already worked on it please share how you have done it!.
1 Answer
You could use the MATLAB Support Package for Arduino or plain old serial communication. The former is pretty straightforward; see the documentation. Below is a simple example of how the latter would work.
Arduino:
void setup() {
Serial.begin(9600);
}
void loop() {
int load = getLoad();
if (Serial.available()) {
Serial.println(load);
}
}
MATLAB:
% Connect to Arduino
s = serial('COM1', 'Baudrate', 9600, 'Parity', 'none', 'Databits', 8, 'Stopbits', 1);
fopen(s);
set(s, 'Timeout', 2000, 'Flowcontrol', 'none');
s.ReadAsyncMode = 'continuous';
% Read data from Arduino
load = fscanf(s, '%d');
% Close connection when done
fclose(s);
Check out the following resources for more details:
- "Arduino Serial Data Acquisition" by Ye Cheng on MATLAB Central File Exchange
- "Arduino and Matlab: let them talk using serial communication!" by gianluca88 on Instructables
- "How To Send Data From The Arduino To MATLAB" by Miguel on AllAboutEE
- "How can I communicate from Arduino to MATLAB" by Bill Nace on Arduino Stack Exchange
- "Getting Started with Serial I/O" in the MATLAB Documentation
- "Write and Read Data" in the MATLAB Documentation
- matlabarduino.org
2 Comments
PramodHegde
Thank you mbaytas. Does your code work with all types of arduino ?
mbaytas
The general idea is the same, not only for all types of Arduino, but other devices that communicate over a serial port as well. I suggest you take a moment to look at the resources I posted above, because they cover a lot of implementation details which can make your life easier.