Go Down

Topic: Arduino Nano MATLAB Serial communication (Read 837 times) previous topic - next topic

Hennow

Hi,
I am trying to sample data a sensor connected to a Nano (with a CH340 chip) in MATLAB. I am trying to use serial communication through USB to do this at 38400 baud. The problem I am having is that whilst the data from my sensor looks correct in the serial monitor the data I get in MATLAB is delayed by ~1 second and often I get the same signal twice within my MATLAB data. The exact same code (other than change port) works perfectly on an Uno. I have tried two different Nanos and installed the CH340 drivers and none of this fixed the problem. Any help would be greatly appreciated.

ARDUINO CODE

int16_t data;
void setup() {
  Serial.begin(38400);
}
void loop() {
  MMG = analogRead(A1);
  Serial.println(data);
}

MATLAB CODE

s1 = serial('COM13');   
s1.BaudRate=38400; 
fopen(s1);

for i=1:20000
read=fscanf(s1);
data(i)=str2double(read);
end

PaulS

Quote
Any help would be greatly appreciated.
Your Arduino code won't even compile. So, that's clearly NOT the code you are running.
The art of getting good answers lies in asking good questions.

Go Up