ad space

Arduino simulation in Proteus with Virtual COM port

Proteus is a popular microcontroller simulation software that allows you to simulate and debug microcontroller-based designs. It provides a comprehensive suite of tools for simulating various aspects of microcontroller systems, including the microcontroller, peripherals, external interfaces, analog and digital circuit. It is also capable of PCB layout design. One of the useful features of Proteus is its ability to simulate the communication between an Arduino board and a computer using a virtual COM port.

In this article, we'll look at how to set up a virtual COM port in Proteus and use it to control an LED connected to an Arduino board. That is you will learn how to use physical to virtual serial port to communicate with Arduino in Proteus for simulation. We'll use the Arduino Nano as an example in this tutorial, but the same principles apply to other Arduino boards.

Simulating an Arduino board in Proteus

To start, you need to add the Arduino Nano model to your Proteus design from the Proteus library. Also add a LED and a resistor from the library. Place them and make a simple circuit where the LED is connected to the Arduino Nano on pin via a 220Ohm resistor as shown below.

arduino nano led circuit diagram
Next, you need to add a RS232 COM port part to your design. To do this, search for "COMPIM" in the Proteus library, then place it on the design schematic editor canvas and add the RXD of the RS232 com port to RX pin of the Arduino. This is as shown below.

arduino nano led with com port circuit diagram

Setting up the COM port

Now the Arduino is able to receive data from the serial port. However we have to configure the com port component. Right click on the COM port part "COMPIM" and click on edit Properties to open its properties dialog box. In the Properties dialog box, you can set the baud rate, data bits, stop bits, and parity. In this example, we'll set both the physical and virtual baud rate to 9600, physical and virtual data bits to 8, physical and virtual stop bits to 1, and parity to None. See the setting below.

RS232 com port

Arduino Program Code for Serial Data

Below is the code to turn on or off an LED connected to pin 9 on an Arduino based on the data received from the serial port:

const int LED_PIN = 9;

void setup() {
  pinMode(LED_PIN, OUTPUT); // Set pin 9 as an output for the LED
  Serial.begin(9600); // Start the serial communication
  digitalWrite(9,HIGH);
}

void loop() {
  if (Serial.available() > 0) { // Check if there is data available on the serial port
    int data = Serial.read(); // Read the data
    if (data == '1') { // If the data is '1'
      digitalWrite(LED_PIN, HIGH); // Turn on the LED
    } else if (data == '0') { // If the data is '0'
      digitalWrite(LED_PIN, LOW); // Turn off the LED
    }
  }
}

In this code, the LED is connected to pin 9 and the pin is configured as an output in the setup() function. The serial communication is started with a baud rate of 9600.

In the loop() function, the code checks if there is any data available on the serial port using the Serial.available() function. If there is data available, the code reads the data using the Serial.read() function and checks if the data is either '1' or '0'. If the data is '1', the LED is turned on by setting the digital output of pin 9 to high using the digitalWrite() function. If the data is '0', the LED is turned off by setting the digital output of pin 9 to low.

With this code, you can control the state of the LED by sending '1' or '0' to the serial port from another device, such as a computer or another microcontroller.

Virtual Serial Port Emulator Program

Next we need to use virtual serial port emulator program that creates an virtual interface between the Proteus COM port part above and the PC/Laptop actual COM port. There are many free virtual serial port emulator software out there which you can easily find by simple google search. Here we will use the Virtual Serial Port Emulator(VSPE). 

After downloading and installing the program, open the program to configure the port. Go to Device then select Create. In the Device type option select Connector and click on Next. Then select the com port you want to use. Here COM1 is selected. The click Finish and a new virtual COM1 port is created. Click on the Run button to start the serial port emulation.

VSPE

Serial Port Terminal

The next program we need is a serial port terminal. Again there are lots of free serial port terminals and here we will use Tera Term. If you don't have a terminal yet, download and install one. Open tera term and select Serial option with COM1 as the com port. Make sure that the term term port is configured with correct baud rate of 9600bps and the data bits and parity bit are also configured correctly. You can configure these settings by going to Setup > Serial Port. See below for this.

tera term serial port

You send data with this terminal by typing on the keyboard but you will not see the character or words you typed in. Therefore to see what you have actually typed you have to turn on the local echo option. You can do this by going to Setup then select Terminal and there you have to check mark the Local Echo option as shown below.

echo

Controlling an LED with the virtual COM port

Now that you have set up the virtual COM port, the terminal and connected it to the Arduino, you can use it to control an LED. At this point ensure you have uploaded the above code to the Arduino in Proteus, the VSPE program is running and the tera term terminal is also running.

Click on Run button in Proteus to start the simulation. Then on the tera term type in "1" to turn on the LED and type "0" to turn off the LED. What is happening is that, when we type in "1" or "0", we are sending these values from the PC com port using tera terminal to the emulated COM1 port as shown below.

 The emulated COM1 port is configured in the Proteus RS232 COM port. So it receive the data sent from the tera term. The RS232 COM port is connected to the Arduino serial port receiver pin RX or pin 1. Thus Arduino is receiving the data sent from the PC. The Arduino code above reads the incoming serial data and either turns ON or OFF the LED according to whether 1 or 0 is received.

Video demonstration

The following video demonstrates how to configure the VSPE, tera term and Arduino to turn on and off the LED.

 

So in this way we can simulate Arduino with Serial port to send data to Arduino via PC serial com port interface. 

References:

[1] Serial communication from PC to Proteus

[2] Most Popular Electronics Circuit simulators

[3] Programming ATmega328p USART 

Post a Comment

Previous Post Next Post