Practical data connection between MATLAB and microcontrollers using virtual serial port and MicroPython Pyboard: A survey

In this paper, a simple and practical method to hookup between Pyboard and computer using MicroPython and MATLAB is presented. With the presented way, MATLAB can connect to Pyboard with virtual serial port (VSP). This process is performed with a virtual port, without using MATLAB toolbox in all versions of this software and control prototyping is widely available on the hardware. This system can also be used in Simulink and widely be under the control of MATLAB to perform tasks. The system is based on (.py) file and (.m) file. One is made in MicroPython to perform analog to digital task and the second contains VSP source code to have a virtual connection with the proposed board and calculating codes to plot graphics. This way can cause the high speed of data sampling and data transfer in two different environments: Python interpretation environment and MATLAB environment. With the defined way, it is possible to make the devices that require calculation operations and the correlation of the computer and external environment with lower costs and fewer accessories. To validate the correctness of the proposed approach, an experimental prototype as a total harmonic distortion (THD) meter device has been built.


| INTRODUCTION
Recent wide range of microcontroller applications have determined their advances in commerce and industry. Moreover, MATLAB is the common software used in this major. Accordingly, the connection between MATLAB and microcontrollers is the significant part of this process, performed with MATLAB toolbox. However, the quality of this tool can differ in different versions of the software. As a result, it can affect the time of the process. MicroPython STM32F411RE microcontroller (Pyboard) is one of the newly established boards that has noticeable capacities and runs on the entire rewrite of Python3.4 that called 'MicroPython'. MATLAB/ Simulink software uses toolboxes for targets such as graphical modelling to be performed on boards and even, this way performs the connection between the microcontroller and MATLAB can take time in data transfer and running Simulink on the proposed microcontroller [1]. The process of data stimulation was performed with Simulink and its block diagrams; however, this process done with MATLAB connection toolbox can affect the quality of the transferring data between the software and microcontroller [2]. Real-time control prototyping in microcontrollers with MATLAB/ Simulink that is performed with the specific tools and specific stimulation blocks in MATLAB/Simulink that can differ in various versions of MATLAB [3]. Moreover, MATLAB graphical user interface (GUI) has the capability to control through MATLAB programming. This process is done with MATLAB/Simulink toolbox to have control on the microcontroller; However, this can have effects in time length of data transfer between MATLAB and microcontroller. Microcontrollers include interfaces that mostly used in electrical and computer projects. These interfaces can be under control with MATLAB/Simulink; however, since the type of the connection affects the quality of data communication, plays the main role and MATLAB toolbox is used in common boards [4]. The approach used in recent studies that are about the relation between MATLAB, MATLAB GUI and microcontrollers to make connections, is MATLAB toolbox [5][6][7][8]. However, the quality of this tool can differ in various versions of MATLAB.
Here, a connection performed with a virtual serial port (VSP) that is almost compatible with all versions of MATLAB. Moreover, this technique creates a correlation between two different environments (MicroPython and MATLAB) and maximises the sampling speed, due to the RAM of Pyboard. Accordingly, this way creates an optimum environment to perform tasks, for example, GUI, data transfer, microcontroller connections, cross-platform, web-platform and console applications. Therefore, it can be used in internet of things (IoT), commerce, education and industry. To show the capability of this method, a total harmonic distortion (THD) meter device is implemented as a test bed, whereas this method could be extended to run several industrial applications too.

| PYBOARD ARCHITECTURE
The MicroPython Pyboard is a microcontroller that runs MicroPython, performing a low-level Python 3 operating system that can be used to control electronic projects. Figure 1 depicts the overall schematic of a THD meter device based on Pyboard. Accordingly, this board consists of 168 MHz Cortex M4 CPU with hardware floating point, 1024KiB flash ROM and 192KiB RAM, micro USB connector, for power and serial communication. Micro SD card slot, supporting standard and high capacity SD cards, 3� 12-bit analog to digital (ADC) converters, available on 16 pins, 4 analog ground shielding, 2� 12-bit digital to analog converters, that are devised on pins X5 and X6. It includes an on-board 3.3 V LDO voltage regulator that can supply up to 250 mA, moreover, input voltage range 3.6-16 V and DFU boot loader in ROM that used for firmware easy upgrading.

| PROPOSED FRAMEWORK
This program is based on MicroPython framework that is designed to be used on microcontrollers, including MicroPython official boards and Arduino new series and so forth. Micro-Python is an efficient derivation of Python 3 series programming language and created with Python and C99. It includes a small portion of Python standard library and optimised to run on microcontrollers and in constrained environments. Micro-Python is packed full of advanced features such as an interactive prompt, precision integers, list comprehension, generators, exception handling and so forth. It is compact enough to run within 256k of code space and 16k of RAM.

| The concept of THD and DPF
THD and distortion power factor (DPF) are qualitative indexes that illustrate how close the waveform is to the sinusoidal one.
Initially, waveform and network voltage are sinusoidal, as standard. If a non-linear load is supplied with a sinusoidal waveform, current harmonics will be created. The non-linear load can include diodes and transformers. Current harmonics have harmful influences, for instance, harmonics cause an increase in transformer size, power losses and noise in control and protective systems. Therefore, THD measurement makes user awareness of the current consumption status. Pyboard and block diagram of the test bed THD measurement system is illustrated in Figure 1  Here, I 1 , I k , I and I dc are the rms values of the main harmonic, of the kth harmonic order, total rms value and average values of the load current, respectively. The rms value of the load current is calculated as:

| THD and DPF measurement
I ¼ ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi where ω = 2πf in which f = 50 Hz and a 1 , b 1 are as follows: Therefore, I 1 is calculated as: ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi ffi Also, the load active power and dc value of the load current are calculated as:

| System circuit
First, a THD test bed circuit has been made as shown in Figure 2, the circuit consists of a non-linear rectified inductive load with the addition of a half-wave rectifier to make a nonsinusoidal and asymmetric waveform of i(t), and the current measurement section. The AC and DC components of the load current are applied to pin Y12 and pin X8 of the Pyboard, respectively. The last part of the measurement section connects to the Pyboard to send data and analysed with MATLAB. The AC and DC components of the load current are applied to pin Y12 and pin X8 of the Pyboard, respectively. Moreover, this process requires code (Experiment-B. MicroPython Code), for Pyboard that previously coded in its 'main.py' file. Then, Pyboard connects to the MATLAB with virtual USB (Experiment-C. MATLAB Connection Codes), and the coded program starts to sample data and save it in 'THD data.txt' file, then the file is opened with MATLAB, and the samples are analysed.

| MicroPython section
Depending on Figure 3, in Section 5.2, current I is received from circuit with MicroPython codes written on board and pin Y12. The Pyboard side program that is written to sample the data of the load current and transferring to the computer is presented as:

F I G U R E 4 (a)
Overall algorithm of MATLAB VSP side first program to get data from VSP and to save in data file. (b) Overall algorithm of second 'm' file to calculate THD using data file. THD, total harmonic distortion; VSP, virtual serial port 488 -SAFARI AND SABAHI

| The results
The experimental prototype and waveforms of the load current are illustrated in Figures 5 and 6, respectively. From Figure 6, it is clear that load current is non-sinusoidal, data is analysed with the another MATLAB '.m' file according to Equations (1)-(5) and the result is shown in Table 1.

| COMPARISON
In comparison to other ways, the represented approach has the capability to be used in all versions of MATLAB/ Simulink and besides MATLAB GUI, there is no requirement to have modification on the communication protocol. Accordingly, the connection method does not vary and performs the connection between the software and microcontroller to transfer the data, the additional information is shown in Table 2 ADC data transfer and MATLAB code, for creating a virtual port, calculation part and the result plots. Due to the transfer time between Pyboard and MATLAB/Simulink, the virtual port plays the main role in the process.
As an experiment, the system used for a THD circuit to plot the THD graph.
In order, the results validate the possibility and correctness of this method. The experimental results, compared with the indicated specific measurement devices, validate the correctness of MicroPython microcontroller programming, Pyboard microcontroller, MATLAB codes and algorithms used for the study. With the presented way, it is possible to make the devices that require calculation operations and the correlation of the computer and external environment with lower costs and fewer accessories. The innovation of the study point can be summarised in five aspects: (1) With the technique presented in the study, it is not required to modify the data transfer protocol. (2) This approach even supports the GUI and Simulink without any inefficiencies of data transfer protocol. (3) Using VSP to have a communication between computer software and microcontrollers. (4) Using a capable microcontroller board called 'Pyboard' and an advanced microcontroller programming language called 'MicroPython'. (5) Creating an advanced microcontroller program, also crossplatform programs with the least lines of codes with the derivations of Python programming language, in comparison to other languages, especially C programming language, for microcontrollers.