3

I am trying to read data from potentionmeter using an arduino microcontroller (tried both arduino UNO, arduino FIO) and using serial communication interface it to Simulink (I tried baud rates ranging from 57600-921600).

Here is the Arduino source code:

/*
  AnalogReadSerial
  Reads an analog input on pin 0, prints the result to the serial monitor.
*/
#define ana_pin A0 

void setup() {
  analogReference(DEFAULT);
  Serial.begin(57600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(ana_pin);
  // print out the value you read:
  Serial.print(sensorValue);
 // delay(500);        // delay in between reads for stability
}

I interfaced it with Tera Term software and there is instantaneous change of values corresponding to 3 V or 0V.

However when I tried to interface it with Simulink model using instrument control toolbox:

enter image description here

there is a 10 second lag when value changes from ASCII representation of 3V to 0V

enter image description here

The block sample time is 0.01 seconds and the model configuration parameters are adjusted accordingly (I tried it for 1 second or more and the delay still remains. Also, I was able to record data from another sensor and LPC1768 development board without any delay.

I also tried it with Arduino support libraries in Simulink:

enter image description here

And it seems that no data is received, as you can see from Scope1 in the png file the status signal is always 0. I have also attached hardware implementation properties from Arduino block in Simulink:

enter image description here

Could you help me understand what is going on and how to solve this issue?

Patrick Trentin
  • 7,126
  • 3
  • 23
  • 40
  • Is the length of lag constant when you change the bitrate? – Patrick Trentin Feb 15 '17 at 08:07
  • 1
    In arduino sketch you should try to replace `Serial.print(sensorValue);` with `Serial.println(sensorValue);`; it will add a termination char `\n` (or `\r\n` on windows). Most serial function wait for a special char to know that transmission has ended. In your failing case I think that simulink scopes has a timeout of 10s. – Arno Bozo Feb 15 '17 at 10:19
  • Thanks for your reply. As suggested I used Serial.println function and set the terminator in the Instrument control toolbox as <\r\n>. However the 10 sec delay between perceived value changes still persists. Using Serial.println function and setting the terminator in ICT as , the delay increases by approximately sec (total delay 20 sec). Intuitively its understandable as the delay accounts to reading and displaying the ASCII character for '\r\n'. So I think am pretty sure the delay is due to arduino and simulink interface. Also the length of lag is constant when I change bit rate. – Dimple Bhuta Feb 16 '17 at 03:08

1 Answers1

0

@Patrick Trentin - I get a delay of 4 seconds when I use baud rates of 230400, 460800 and 921600. For baud rate of 57600, 115200 I get a delay of 10 seconds. Thank you for pointing it out I did not focus on that previously. However since I will be using the sensor in an application which accurately needs reading every 0.01 sec. I dont think I can work with 4 sec delay.