Hi Zainoor,
To send a value from MATLAB to an Arduino and view it on the Serial Monitor, you need to make sure that both the MATLAB code and the Arduino code are correctly configured to communicate with each other. I see that the code for the Arduino seems mostly correct, however, there are a few issues in your MATLAB code that need addressing:
- Data Type: Ensure you are sending the data in the correct format that the Arduino can interpret. Since fprintf is used to send strings, you need to convert your numerical data into a string.
- Make sure the terminator matches what your Arduino expects.
- Ensure you close the serial port after communication to avoid locking issues.
You may try the following modifications for your MATLAB code:
O = 1; % The value you want to send
s = serial('COM3', 'BaudRate', 9600);
fopen(s);
% Send the data as a string
fprintf(s, '%d', O);
response = fscanf(s);
% Display the response
disp(['Arduino Response: ', response]);
% Close the serial port
fclose(s);
delete(s); % Clean up the serial object
For more information on working with Arduino, you can visit the following docmentation:
Hope this helps!
