Clear Filters
Clear Filters

Arduino can talk to MATLAB, but not the other way round

2 views (last 30 days)
I've been trying for hours with this issue after realising that the Arduino support package (e.g a = arduino) wipes any code currently on the board and the Arduino in question will be running all its own code but just pulling some values from MATLAB. I'm not super comfortable with Arduino yet so I apologise for any incorrect terminology.
I'm trying to get MATLAB to simply send data to an Arduino using serial if the Arduino requests the data. To make this as simple as possible just so I can get things working I've written some basic example code to check everything works as it should. Here is the Arduino side:
---------------------------------------------------------
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // Sets the built-in LED as an output
Serial.begin(19200); // Begins a serial connection with a Baud rate of 19200
Serial.print("pull"); // Immediately prints "pull" to the serial for MATLAB to read
digitalWrite(LED_BUILTIN, HIGH); // Initially sets the built-in LED to on
}
// This loop simply makes the LED blink if there is any data available at all (because I just want to see if anything
// is being transferred).
void loop() {
if (Serial.available()>0)
{
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
}
---------------------------------------------------------
I've purposefully kept it as simple as possible to minimise issues. Here is the MATLAB code:
---------------------------------------------------------
fclose(instrfindall); % Ensures no devices are open.
close all; clear all; clc; % This is purely to make sure 100% there is a clean working environment. This won't be in any final code.
arduino_serial = serial('COM7', 'BaudRate', 19200);
arduino_serial.InputBufferSize = 4; % Input is always "pull" for this test.
wait_ini = 5; % Setting the waiting time
try
fopen(arduino_serial);
% This next part simply waits for wait_ini amount of seconds after opening to make sure everything has time to open.
for i_ini = 1:wait_ini
fprintf('Waiting: %d', ((wait_ini+1)-i_ini))
pause(1);
clc;
end
received_data = fread(arduino_serial); % Reads the data sent from the Arduino (i.e "pull"). This works fine.
if strcmpi(char(received_data'), 'pull') == 1 % Checks if the Arduino has sent "pull"
fprintf(arduino_serial, '%s', '1'); % Sends a '1' to the Arduino. I've also tried fwrite(arduino_serial, 1).
fprintf('Data pushed over serial!\n\n')
end
fclose(arduino_serial);
catch
fclose(instrfindall);
disp('Error!'
end
---------------------------------------------------------
Once the MATLAB code has run, the 'if' statement in the Arduino code should trigger and the LED should start blinking as per the code, yet it stays solid. I have no idea why I'm getting this wrong as MATLAB can receive the "pull" state from the Arduino.
If anyone can shed some light on this it would be much appreciated. I've double checked the COM port to make sure it's the right ones, tried resetting everything, using different USB ports (might sound stupid I know, but you've gotta try everything I guess). I'm using an Arduino Uno.
  1 Comment
Nagarjuna Manchineni
Nagarjuna Manchineni on 19 Jun 2017
From your Arduino code, it looks like the LED's will blink for every serial write. Have you tried to debug the loop function code from the Arduino IDE to verify if the control is getting delegated to loop function or not?
You can use Serial.print() to print whether the code is entering into the loop function or not.
Are you able to get the same data (received_data) which you have written using fprintf statements?
Also, your MATLAB Code looks good, it connects to the serial port and writes some data to the serial port and then tries to read the data and closes the connection.

Sign in to comment.

Answers (0)

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!