Problem when sending an order from Matlab to Arduino


I make connected the RFID reader with Arduino Nano. The Arduino is connected with the computer via USB. The Arduino contains the code that makes the RFID reader scan every half-second to make sure there is a card to read.

Arduino sketch

-------[ // This sketch is made to read an RFID tag every second, if present, and send that info to the serial COM port.
// In this example, I used a arduino Uno and a RC-522 RFID reader operating at 13.56 MGHz.
// Make sure you download and install the SPI & MFRC522 arduino libraries before running this script.
//Created by SdT @ MNI 07/2017
#include <SPI.h> // Make sure you have the SPI livrary to enable communication
#include <MFRC522.h> // This includes the library for the specific reader used in this test, the RFID-RC522
#define RST_PIN 9 // Define the reset pin on the arduino
#define SS_PIN 10 // Define the sda pin on the arduino
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
void setup()
Serial.begin(9600); // Initialize serial communications with the PC at 9600 baud rate
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Initialize SPI communication between arduino and rfid module
mfrc522.PCD_Init(); // Init MFRC522 RFID card
void loop()
if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) { // Look for new cards, and select one if present
delay(1000); // Set this time parameter to adjust the frequency of RFID scanning
for (byte i = 0; i < mfrc522.uid.size; i++) // Read, format and print the unique tag identifier (UID)
Serial.print(mfrc522.uid.uidByte < 0x10 ? " 0" : " ");
_ Serial.print(mfrc522.uid.uidByte*, HEX); //HEX or DEC or OCT or BIN*_
* }*
* Serial.println();*
At the computer I connect the Matlab with the Arduino where the Matlab scans the Arduino. The card number will be read in the Matlab. The problem starts when I want to send a command from Matlab to the Arduino to run the LED. Where execution is delayed and an error message (Updating server code on board Nano3 (COM5). Please wait.) appears. After that the LED is turned on, but the code in the Arduino stops working and does not read the RFID card even if the Arduino is turned off and turned on again. After that I delete the code on the Arduino and then reload it again to get back to work

You can't have Matlab open the COM port to the nano during the same time that the IDE is trying to use the same COM port to program your nano. Only one program can have it open at a time.

After reading the card number, I close the connection with the Arduino, and then I come back again and open a connection with it to send the command to turn on the Led

please help me

Every time you open the COM port to the Arduino, it resets the chip. This is the default behavior so the IDE can upload new code. There is a jumper you can cut to stop this behavior. After cutting the trace, the chip will no longer reset whevever a new COM link is opened. It also won't let you upload code anymore. You can cut the trace and solder in a switch to have it either open (no reset) or closed (reset for new code upload)

Why do you close the connection in the first place? Is there something wrong with leaving it open?