Why does my serial communication with Arduino is not working ?
11 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
Vinayak Choyyan
2023-4-11
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!