Send Arduino data from Proteus to Simulink

Proteus and Simulink are two powerful software tools used in the design and simulation of electronic systems. Proteus is a comprehensive software suite for the design and simulation of microcontroller-based circuits and embedded systems, while Simulink is a simulation and modeling tool developed by MathWorks that is widely used for modeling and simulating dynamic systems. These two software tools are often used together in the co-simulation of complex electronic systems. Co-simulation is the process of using multiple simulation tools to simulate different aspects of a system and then integrating the results to obtain a more accurate and comprehensive model. 

The tutorials in the references[1][2] explains how to transfer data from simulink to proteus to control a LED connected to an Arduino. In this tutorial, we will explore how to transfer data from Arduino in Proteus to Simulink as an example of co-simulation of Proteus and Simulink. This simple co-simulation teaches the basics of data transfer from Proteus to Simulink via Arduino serial port and receiving the data in Simulink using the Serial Receive block. This simple tutorial will help in the design and simulation of more complex electronic systems. 

Steps to setup co-simulation

Step 1: In Proteus, design the following circuit diagram. The potentiometer is required since we will vary the potentiometer and send the ADC value which ranges from 0 to 1023 to Simulink via the Arduino serial port. The RS232 connector called COMPIM in proteus is required. The Tx pin of Arduino is connected to the TxD of the RS232 connector. The virtual serial monitor is also connected to see the transmitted data over the serial port.

proteus arduino send data

The RS232 connector port is configured as shown below.

RS232 COM port
 
The video provided below shows how to configure Arduino and the serial port.

Arduino Program

Arduino code to send the potentiometer data from Proteus to Simulink via the serial port is below.

const int POT_PIN = A0;

void setup() {
  Serial.begin(9600); // initialize serial communication at 9600 bits per second
}

void loop() {
  int potValue = analogRead(POT_PIN); // read the analog value of the potentiometer connected to A0
  String potValueString = String(potValue); // convert the analog value to a string

  // add a termination character to the end of the potentiometer value string
  potValueString += '\r';

  //send the potentiometer value as a string with termination over the serial port
  Serial.write(potValueString.c_str(), potValueString.length()); 

  delay(100); // wait for 100 milliseconds before reading the potentiometer again
}

In the above code, the analog pin A0 is given an alias name POT_PIN. We have initialized the serial communication at a baud rate of 9600 bits per second in the setup() function.

In the loop() function, we read the analog value of the potentiometer connected to analog input pin POT_PIN using the analogRead() function. We then convert the analog value to a string using the String() function.

Next, we add a termination character to the end of the potentiometer value string using the += operator. In this example, we use the '\r' character as the termination character.

Finally, we send the potentiometer value as a string with termination character over the serial port using the Serial.write() function. We pass the potValueString.c_str() and potValueString.length() as arguments to the Serial.write() function. This sends the string as a series of individual bytes over the serial port.

Note that we also include a delay of 100 milliseconds in the loop() function to prevent the program from flooding the serial port with data.

Step 2: The next step is to make a model in Simulink that receives data from serial com port 1 and converts the received string to integer value for display. The following is Simulink model that uses Serial Receive block to which is configured to use COM2 port with 9600 baud rate and a sample time of 0.01seconds which is equal to 10ms. Note that we have used 10ms delay in the Arduino code above.

serial receive simulink model

The Serial Receive block is configured to use COM1 port with data size of [1 4] because we expect potentiometer value with maximum of 4 digits(0 to 1023). We have used the carriage return \r as the terminator which was also programmed in Arduino to send the data with \r delimiter. The data type is uint8. Although the following serial block configuration picture shows sample time of 0.001 we should use 0.01 because we have send the data using 10ms in the Arduino program.

serial receive block

The serial configuration block configuration is shown below.

serial configuration simulink model

This setting should match the RS232 port configuration in Proteus.

Nothing needs to be changed for the settings for the ASCII to String block, the String to Single block and the Display block. The video provided below shows all this configuration.

Step 3: The next step is to create virtual serial port pair between COM1 used in Simulink and COM2 used in RS232 serial com port in Proteus. We do this using a program called Virtual Serial Port Emulator(VSPE). This is a free program which you can google search download and install it. The tutorials in reference[1][2] provides details how to make a virtual serial port pair and the video below also shows this in details. Shown below is a screenshot



 
The tutorial in reference [4] explains in details how to configure the VSPE program.
 
Step 4: The final step is to perform co-simulation. We star the simulation in Proteus and run the simulation model in Simulink. The Arduino starts sending potentiometer value over its serial port. We can view the data send using the virtual terminal. The data send is received in Simulink using the Serial receive block. This data is unit8 data type in ASCII characters. This is then converted to String then to integer and displayed on the display block. By varying the potentiometer we can see the changed data is also displayed on the display block in Simulink. So this illustrates correct data transfer between Proteus and Simulink.

Video Demonstration of Proteus Simulink Co-Simulation

The following video shows in details how to configure the Arduino, RS232 serial port, Serial receive block in Simulink and other blocks in Simulink and shows the data transfer from Proteus to Simulink.

Conclusion

In conclusion, it was shown how one can send data using serial port of Arduino in Proteus to Simulink which uses the Serial receive block. This can be helpful in prototyping and simulation of Arduino or any microcontroller based system in Proteus with co-simulation with Simulink. Data transfer over the serial communication port was illustrated and thus proteus and simulink co-simulation was demonstrated. There are many advantages and application of Arduino in Proteus co-simulation with Simulink which is described in the the reference[4]. The reference[5] shows how to program Arduino with Simulink if co-simulation with Proteus is not desired.

References

[1] Interfacing Proteus and MATLAB-SIMULINK for Co-Simulation

[2] Send data from Simulink to Proteus for Arduino Co-Simulation

[3]  Serial communication from PC to Proteus

[4] Arduino in Proteus co-simulation with Simulink: Advantages & Applications

[5] Programming Arduino using Matlab/Simulink

Post a Comment

Previous Post Next Post