Why does my serial communication with Arduino is not working ?

14 views (last 30 days)
I'm trying to implement a communication from MATLAB to an Arduino Uno using Serial port.
I tried the simplest case I could imagine to test this communication but it still doesn't work.
My arduino code is the following :
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myservo.attach(3);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()>0)
{
myservo.write(80);
}
}
Basically, when Serial recieves anything, it moves the servo to a random position. I tried it with directly sending something in the serial monitor of Arduino and it works fine.
However, when I try to send anything with MATLAB using the simple following code :
clc;
delete(instrfindall);
clear s;
s=serial('COM16', 'BAUD', 9600);
fopen(s);
fprintf(s,"anything");
fclose(s);
The program runs, terminates, but nothing happens with the servo, indicating that nothing was recieved. As this is the simplest case I can imagine to test the communication between the two softwares, I really don't see where the problem comes from.
Thank you in advance for your help with this issue.

Answers (1)

Vinayak Choyyan
Vinayak Choyyan on 11 Apr 2023
Hi Hugo,
As per my understanding, you are facing issues when trying to communicate with your Arduino Uno over serial comm. I understand that you have used the Arduino app to confirm that your board is indeed working.
Please ensure the Arduino Uno support packages have been installed. Please refer to this documentation for details steps on how to configure and setup Arduino Uno with MATLAB https://www.mathworks.com/help/supportpkg/arduinoio/index.html.
Alternatively, use the Arduino Explorer App to do the above interactively. A details documentation of the same can be for here https://www.mathworks.com/help/supportpkg/arduinoio/ug/using-arduino-explorer.html.
I hope this resolves the issue you are facing.

Categories

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

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!