0% found this document useful (0 votes)
34 views12 pages

An Example of Application of Microcontroller in Voltage, Current, and Power Measurements

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 12

An Example of Application of Microcontroller

in Voltage, Current, and Power Measurements

Aleksandar Dedić∗

Keywords: Measurement of voltage, current, and power, measuring transducers, microcontroller.

Abstract: This paper describes a device for measuring effective values of alternating
voltages, currents, and power, as well as phase positions of voltages and currents with
a frequency of 50 Hz, which is based on a microcontroller. To convert analog voltage
and current values into digital ones, voltage and current measurement converters
controlled by a microcontroller are used. In addition to the above, the microcontroller
receives data from the measuring transducers and processes them. The obtained
results are displayed in the form of a vector diagram and in digital form on the
graphic display.

1. INTRODUCTION

The device for measuring alternating voltage, current, and power, the description of
which is given in this paper, was originally designed and made for the needs of the
laboratory of the Faculty of Electrical Engineering, University of Montenegro, primarily for
performing laboratory exercises. During its design the following goals have been imposed:
all tasks should be performed by the microcontroller without the addition of external
memory, and the hardware accessories should only be circuits at the level of analog
electronics. This device is designed for measurements in an electrical network with a
frequency of 50 Hz, but it is not tightly bound to that frequency. Measurements are also
possible in cases of other frequencies, but the accuracy of the device decreases for
frequencies lower than 40 Hz and higher than 100 Hz.
This paper describes the second version of this device. The previous version was
described in two papers that were presented at the "Information Technologies" scientific
and professional conference. The first paper [1] described the way a microcontroller forms
an image on an LCD display. At that time, no measurement was performed, only the
simulation of the analog-to-digital (A/D) converters. In another paper [2], a description of
the operation of the voltage and current converter is given, as well as a description of the
complete device.

∗ Aleksandar Dedić is with the Faculty of Electrical Engineering, University of Montenegro, Montenegro
(phone: +382-20-245839; e-mail: [email protected]).
4 ETF Journal of Electrical Engineering, Vol. 26, No. 1, November 2020.

The difference between the first and second versions of the device is that 12-bit A/D
converters used in the first version were replaced by 14-bit A/D converters. Another
difference is that mean absolute values are measured in the first version, while effective
(RMS) values of voltage and current are measured in the second. Also, power measurement
was added, and significant changes were made in the microcontroller program. These
changes significantly improved the accuracy of measurements.
There were no major hardware requirements in the first version of the measuring device,
so any 16-bit microcontroller with a system clock frequency of 40MHz and a working
memory of 16kB could be used. The program became significantly more complex in the
second version, so a microcontroller with implemented digital signal controller (DSC) and a
system clock frequency of 70 MHz had to be used.

2. BASIC ELEMENTS
The block diagram of the measuring device is given in Fig. 1. It consists of the following
building blocks: voltage transducer, current transducer, reference voltage source,
microcontroller, and display.

Fig. 1. Basic elements of the device

The elements listed here were originally designed for the realization of the prototype of
the measuring device.
A. Voltage transducer
As shown in Fig. 2, this transducer consists of 2 parts: a voltage divider and an A/D
converter. Data is transferred from the converter to the microcontroller via an optocoupler.
This was done for safety reasons as the meter is designed to measure voltages up to 800V.

Fig. 2. Block-diagram of the transducer

The measuring device is designed for measuring voltages of 0 – 800 V. To improve the
accuracy of measurements, this range is divided into 4 measuring subranges: 0 – 0.9999 V,
A. Dedić: One Example of Application of Microcontroller in Voltage, Current, and 5
Power Measurements

1 – 9.999 V, 10 – 99.99 V, and 100 – 800 V. Changing the range is done by changing the
dividing ratio of the voltage divider. The measuring subrange is selected automatically by
the microcontroller.
A simplified circuit schematic of the voltage transducer is given in Fig. 3. Resistors R1,
R2, R3, and R4 form a voltage divider. The division ratio is selected by connecting one of
the resistors R2, R3, or R4 to the ground. As a switch, the integrated circuit IC1 ADG712
[3] is used. The control of the analog switch is performed by the microcontroller via the
integrated circuit IC2 74HC238. Operational amplifier IC3 serves as a buffer to match the
high impedance of the divider to the low impedance of the A/D converter. In this case, the
A/D converter LTC2312–14 (IC4) [4] is used. It performs the complete conversion
procedure by itself, without external clock pulses, and the data is downloaded when
requested by the microcontroller. Optocouplers HPCL-260L (IC6, IC7, and IC8) are used
for data transmission, and 6N137 (IC9 and IC10) are used for controlling the divider. The
outputs and inputs RA and RB are connected to the corresponding pins of the
microcontroller [5].

Fig. 3. Simplified circuit schematic of voltage transducer


B. Current transducer
In order to perform the current measurements using the A/D converter, a current
transducer is used. This transducer converts the current into a voltage that is fed to the input
of the A/D converter. In this case, a current measuring transformer is used for this
conversion. It is combined with an operational amplifier as shown in Fig. 4. This
combination is often called "an active current measuring transformer". With such a
converter, the voltage at the output of the operational amplifier is proportional to the current
through the primary coil of the transformer.
6 ETF Journal of Electrical Engineering, Vol. 26, No. 1, November 2020.

Fig. 4. Current measuring transformer

Simplified circuit schematic of current transducer is given in Figure 5.

Fig. 5. Simplified circuit schematic of current transducer

The converter is designed to measure current 0 – 30 A. In its implementation, two current


measuring transformers are used, one for currents less than 1A (TR1) and the other for
higher currents (TR2). The primary coils of the measuring transformers are connected in
series, and in the case of measuring currents greater than 1A, the primary of the transformer
TR1 is short-circuited by a relay contact.
To obtain greater accuracy, the measuring range of each of these two transformers is
further divided into two subranges by adding operational amplifier IC3 in a non-inverting
configuration and operational amplifier IC4 in a unity-gain configuration with a resistive
divider. This results in the first converter having the ranges 0 – 99.99 mA and 0.1 – 0.9999
A, and the second 1 – 9.999 A and 10 – 30 A. The range is selected by the analog switch
CD4052 (IC5). Control of the divider is performed by a microcontroller. The range
selection is done automatically. The A/D converter LTC2312–14 (IC7) is used for the
conversion to digital form. The outputs and inputs RA and RB are connected to the
corresponding pins of the microcontroller [5].
C. Reference voltage
The phase positions of voltage and current are determined in relation to the alternating
reference voltage. Since the frequency of the reference voltage must be the same as the
frequency of the parameters being measured, the voltage of the 220 V, 50 Hz electrical
network in which the measurement is performed is used as a reference. Optionally, the
A. Dedić: One Example of Application of Microcontroller in Voltage, Current, and 7
Power Measurements

reference voltage can be supplied from another source that is not connected to the electrical
network, for example when the measurements of the variable speed synchronous generator
are performed.
Data on the crossing of electrical parameters through zero are used to calculate the phase
positions. A comparator implemented in the microcontroller is used to determine the
moment when the reference voltage crosses zero. The moments when the voltage and
current cross zero are determined by the software, by analyzing the data from the A/D
converter.
D. Microcontroller
The microcontroller manages the transducers and A/D converters, downloads data from
the A/D converters, processes the received data, and generates the image that is shown on
the display.
This measuring device is based on the microcontroller dsPIC33EP128GP502 [5]. It has a
system clock frequency of 70 MHz and implemented DSC that enables easier data
processing. In this case, two 40-bit accumulators and an arithmetic unit are used from the
DSC, which enables the multiplication of two 16-bit numbers and the addition of the result
to the existing value in the accumulator during only one system clock. This microcontroller
does not have an EEPROM, so all tables and drawings are placed in the program memory.
E. Display
A TFT-LCD display VGG322403-7ULFWA [6] is used to display the image. This
display is intended for use in small TVs and it does not have the possibility to address
individual pixels. Pixel luminance data is transmitted sequentially from the first pixel in the
upper left corner to the last pixel in the lower right corner. In order to achieve such a
transfer, the microcontroller must generate pulses for vertical and horizontal
synchronization. These pulses must be generated according to the TV signal transmission
standard. The program for generating synchronization pulses and data transmission is
completely original. If the pulses are generated correctly, the data transfer does not have to
be continuous. Interruptions of up to 2 seconds are allowed without disturbing the picture.
In this case, the display is refreshed every 0.3 seconds, which is quite enough because faster
changes are harder to follow.
F. Program
An original program was created for the first version of the measuring device and it
served as the basis for the second version. In the introductory part of this paper, the
differences between the first and second versions are listed. It should be added here that, in
the second version, the numbers are displayed with a floating point. This is done to avoid
showing leading zeros at the beginning of the number even when it is a zero with a period.
The results of arithmetic operations are obtained in the form of multi-digit numbers, but
only 4 significant digits are displayed on the display, and the marks "k", "m" or "µ" are
added to the end of the numbers, if necessary. An exception is the phase position, because
the dot is always placed before the last digit. The algorithm for displaying numbers with a
floating point is completely original.
8 ETF Journal of Electrical Engineering, Vol. 26, No. 1, November 2020.

The program is written in the assembler programming language because the allowed
execution time is very short, so two processes often must be executed in parallel. For
example, during the data transfer from the A/D converter, data download and processing of
previously downloaded data are performed at the same time. This is achieved by inserting
commands for performing arithmetic operations after the command to start the conversion
and between the commands to download the data. With this mode of operation, it is not
possible to use a timer because the time intervals between individual downloads are very
short. Therefore, the data processing commands must be matched so that their number
corresponds to the delay times both for individual downloads and for the entire cycle.
In a similar way, the image to be displayed is not formed in its final form in the memory,
but only the coordinates of the points of the individual objects are calculated. For this
reason, between the commands for transferring data to the display, commands are inserted
that calculate the current coordinates of the pixels, compare them with the coordinates of
the points in the drawing, and, based on this, decide which pixels should be illuminated.
This version of the program contains about 8000 assembly instructions.

3. VOLTAGE, CURRENT, AND POWER MEASURING

The device measures current values of voltage and current, so effective values and active
power are calculated from these data. By definition, effective values are calculated by
integrating a quadratic function. However, the microcontroller does not have the ability to
work with functions in an analytical form, so the effective values are calculated by the
equation:

(1)

where U is the effective value of voltage, N is the number of samples, and un is the
instantaneous value of the voltage.
Calculation of the effective values is done by multiplying the sample values with
themselves and adding the result to the already existing values in the accumulator. These
operations are performed simultaneously with downloading the current sample values. At
the same time, the samples are counted. After the measurement cycle is completed, the
result is divided by the number of samples, and the square root is calculated.
Equation (1) refers to the voltage calculation. The current value is calculated in an
identical way.
The value of the active power P is calculated by the equation:

(2)

where N is the number of samples, un is the instantaneous value of the voltage, and in is the
instantaneous value of the current.
When calculating power, all arithmetic operations are performed with a sign, so the "-"
sign is set automatically if the power value is negative.
A. Dedić: One Example of Application of Microcontroller in Voltage, Current, and 9
Power Measurements

The sampling is performed during 4 full periods, after which the data is processed. This
is done to reduce the possibility of error in case of voltage, current and frequency
instability. At the same time, the data is averaged. Data conversion is performed at a speed
of 180 kS/s. This speed is necessary so that the phase positions of voltage and current can
be determined with a resolution of 0.1°. However, for measuring voltage and current, this
speed is not necessary, so every second sample is taken. The error that appears here is
negligibly small.
Data from the A/D converter is obtained in conversion units. All data is processed in
these units, and only the final result, before image formation, is converted into units of
voltage, current, and power. The conversion is done by multiplying by a constant which, in
this case, is 1.831161499 (&1.D4C7) for voltage and current, and 3.352905273 (&3.5A58)
for power. The values of the constants were obtained by calibration. The result of
multiplication is obtained in hexadecimal form. It is translated into decimal (BCD) form.
Only the first 4 digits are shown on the display. The others are discarded regardless of the
position of the decimal point in the multiplication result. The decimal point is set on the
display depending on the activated measuring ranges.

4. PHASE SHIFT
The measurement of phase shifts is done by counting samples from the moment when the
reference voltage passes through zero until the moment when the measured values pass
through zero. The moment when the reference voltage passes through zero is registered by
the comparator and determined by software. At the same time, zero values are not
registered, but the last sample that has a negative value, and the first one that has a positive
value. From these two data, the moment of passing through zero is calculated. In order to
obtain better measurement accuracy, data are taken during 4 full periods. The phase shift ϕ
in degrees is calculated according to the equation:

(3)

where M is the number of full periods of the measurement cycle (which is 4 in this case), N
is the number of samples of the full measurement cycle, and np is the number of samples
from the moment when the reference voltage passes through zero to the moment when the
quantity being measured passes through zero. Equation (3) gives a value that is 10 times
higher than the actual value. This is done to avoid arithmetic operations with decimal
numbers. The actual value is obtained by placing a decimal point before the last digit. The
phase shift obtained by equation (3) is not related to the frequency of 50 Hz because it is
calculated as a ratio of the number of samples. However, an increase in frequency entails a
decrease in resolution. Since the sampling speed is 180 kS/s, at the frequency of the
measured quantities 50 Hz, the phase shift is obtained with a resolution of 0.1°.
10 ETF Journal of Electrical Engineering, Vol. 26, No. 1, November 2020.

5. DISPLAYING THE RESULTS

The measurement results are displayed in the form of a vector diagram and in digital
form. A vector diagram is displayed on the left side of the display. It consists of a phase
axis that is stationary and two vectors that can rotate around a common point. The voltage
vector is displayed in red and the current vector in blue. The length of the vector is
proportional to the effective value of the voltage and current, and the angle corresponds to
the phase position in relation to the reference voltage. Since the determination of values
from the vector diagram is imprecise, the values are displayed in digital form on the right
side of the display.
The algorithm for forming the image is original and was made specifically for this
device, so it can be used for other applications only with major modifications. A
combination of vector and bitmapped graphics is used to form the image.
An area of 240x240 pixels, located in the left part of the display, is used to display
vectors. The vector is displayed as a line with an arrow at the end. The initial point of the
vector is fixed and has coordinates (121, 121), and the vector itself is displayed as a moving
object that rotates around the initial point.
The line representing the vector is defined by 2 points, namely the starting point, whose
value is always equal to 0, and the endpoint, whose value is proportional to the calculated
current or voltage value. The angle enclosed by the line with the reference axis is equal to
the phase shift between the quantity being measured and the reference voltage. For the line
to be displayed on the display, the coordinates of all the points that will be displayed, which
are between the first and the last, must be calculated. The coordinates of these points are
calculated using the interpolation method.
The given data represent a vector in the polar coordinate system, and they must be
translated into the Cartesian coordinate system. Data conversion is done by equations:

X=Acosφ (4)

Y=Asinφ (5)

where A is the value, i.e. the length of the vector, and φ is the angle. Instead of equation (5),
the following equation can be used:

Y=Xtgφ. (6)

Equation (6) is used because the values of the coordinates along the X-axis (abscissa)
must be rounded to integer values, so the coordinates along the Y-axis (ordinates) are
calculated for the new values of the abscissa.
Working with trigonometric functions is complicated. For this reason, a table of cosφ and
tgφ coefficients is created. It is enough for the values in the tables to be in increments of
one degree, because the data from the vector diagram is not taken, so the diagram does not
have to be precise.
The values of the coefficients are converted into hexadecimal form by multiplying by
4096 (&1000) and placing them in the table. The table formed in this way is placed in the
A. Dedić: One Example of Application of Microcontroller in Voltage, Current, and 11
Power Measurements

program memory [5]. The values in the tables are in the form of integer values but represent
decimal fractions. For this reason, multiplication operations are performed in fixed-point
arithmetic. Point coordinate values must have an integer form. For this reason, the decimal
part of the result is discarded, and the integer is rounded to a lower or higher value
depending on the value of the highest bit of the decimal part of the number.
There is no need to create a table of trigonometric functions for all angle values 0-360°. It
is enough to have a table for angles 0-45°. In this case, relative coordinates are calculated as
if the vector is in the first octant, and then they are converted into absolute coordinates, in
the required octant.
A vector can only be displayed as discrete points. The distance between the points is
equal to the distance between the centers of two neighboring pixels. The dimensions of the
display are limited, so the length of the vector can be a maximum of 119 points. Due to this
limitation, the length of the line can be a maximum of 109 points, and the length of the
arrowhead can be 10 points.
Arrowheads are not generated during vector formation, as this sometimes does not give a
good aesthetic appearance. They are made in the form of bitmapped objects and placed in
the program memory. It is not necessary that there is a drawing for every angle, it is enough
that there is one drawing for every 2°. The coordinates of the vertices are relative to the
reference point whose coordinates are 1.1. These coordinates are converted to absolute
during the process of forming the entire vector by adding all the coordinates to the
coordinates of the last point of the line. Figure 6. a) shows the place where the line meets
the tip of the arrow and the reference point.

Fig. 6. Arrowhead

The mentioned points are shown on the display so that they represent a straight line.
However, due to the small dimensions of the pixels, the line is too thin and has an
unsatisfactory aesthetic appearance. For this reason, another point is added to each point,
which can be above or below the basic one. Determining whether the supplementary point
will be above or below the basic one is done after calculating the value of the Y-coordinate.
Before discarding the decimal part of the result, its highest bit is checked. If the value of
that bit is 1, a point is added below the base, and if the value is 0, a point is added above the
base. The layout of the line is shown in Figure 6 b).
The display area where the vector is displayed has dimensions of 240x240 pixels, so
abscissas and ordinates can be displayed in the form of 8-bit numbers. The microcontroller,
which is used here, has 16-bit memory locations, so the values of both coordinates are
placed in one location.
12 ETF Journal of Electrical Engineering, Vol. 26, No. 1, November 2020.

Coordinate values can be placed in the memory location, but not color information. For
this reason, the memory locations reserved for vectors are divided into 3 groups. In the first
group are the coordinates of the points displayed in red, and in the second group are the
coordinates of the points displayed in blue. The coordinates of the points of the background
drawing are in the third group. These points are displayed in black. The background
drawing consists of a phase axis and corner marks. The phase axis is stationary, and its
coordinates are in the program memory in the form of absolute values. Degree markers are
bitmapped objects with point coordinates in relative form and are also located in program
memory. Each object has its own reference point. The positioning of the corner marks on
the drawing is done by adding the coordinates of the place on the drawing where these
objects should be located to the coordinate values of the reference point. This way of
positioning enables the corner markers to be easily moved on the background drawing, if
necessary. The data on the coordinates of the background drawing are transferred to the
operational memory at the start of the device's operation and are not changed during
operation.
During the formation of the final image, the vectors have priority over the objects of the
background drawing, so on the display, it looks as if they pass over the objects of the
background drawing.
An area of 80x240 pixels on the right side of the display is used to display the numbers.
The way of forming and displaying numbers is different from the way of forming and
displaying vectors. The numbers are displayed as bitmapped objects in a rectangle with
dimensions of 14x16 points. As an example, Figure 7 shows the appearance of the digit "4"
and the coding method. The figure first shows the appearance of the digit on the display,
and then the values that are placed in the memory locations in the binary and hexadecimal
forms are given. In this case, the points are not assigned pixel coordinates on the display,
but one bit is reserved for each point in the corresponding memory location. If the bit value
is equal to 1, the corresponding pixel on the display is illuminated.

Fig. 7. Appearance of the digit "4"

Horizontal dots form one line and are placed in a 16-bit memory location as a unique
number. In total, for each digit, 16 locations are required. The data formed in this way are
placed in the program memory. Drawings of the labels "V", "A", "W", "k", "m", "μ" and "-"
were made in the same way. The point was not made as a special object because it is small,
so that would significantly complicate the program for displaying numbers. Instead, a series
of digits with a period is prepared. In the place where it is necessary to place a period, the
digit without a period is replaced by a digit with a period.
The number field has 240 lines of 80 dots. For each line, 5 memory locations are
reserved in the operating memory. It makes a total of 1200 locations. For aesthetic reasons,
the first number is placed on the 16th line from the top of the display. The displaying is
A. Dedić: One Example of Application of Microcontroller in Voltage, Current, and 13
Power Measurements

performed by transferring the data from the memory location in the program memory,
which corresponds to the first line of the number drawing, to the first memory location
corresponding to the first line (in this case it is the 16th line). Then the data of the first line
of the second number is transferred to the next location in the operating memory. The
procedure is repeated until the data on all the first lines of numbers are entered, after which
the data on the first lines of letters or degrees are entered. In order for the digits on the
display to have the same distance between each other, the data for the lines of each digit
should, in the mentioned locations, be moved to the left or right, as necessary. This
procedure is repeated until all 16 lines of requested digits have been entered.
With this way of encoding numbers, information about which pixels should be
illuminated is placed in memory locations, but not information about color. Because of this,
the locations in the memory are divided into 3 groups. In the first group, the information
about the points that should be red is placed, in the second about the blue points and in the
third, the pink ones.
With this way of image formation, it is possible to have 12 kB space in the operating
memory for a complete image of size 320x240 pixels. More detailed explanations about
how the image is displayed on the display can be found in [1].
An example of displaying measurement results is given in Figure 8.

Fig. 8. Example of displaying measurement results

The measuring device is shown in Figure 9.

Fig. 9. The measuring device


14 ETF Journal of Electrical Engineering, Vol. 26, No. 1, November 2020.

6. CONCLUSION

The measuring device, which is described in this paper, was made to replace old analog
instruments for measuring voltage, current and phase shifts. The device has been
successfully used for many years in laboratory exercises and has also been used in some
experiments in the laboratory.
The LMG450 measuring instrument manufactured by Zimmer Electronic Systems was
used for calibration. The error of the measuring device is about 0.5% for voltage and 0.75%
for current. This accuracy of the device is higher than is necessary for its purpose. The
accuracy can be improved by making changes in the circuit schematic of the voltage and
current transducer, by installing a better measuring transformer and better-quality A/D
converters.
The measurement ranges are changed automatically during a short time interval of less
than 0.1 seconds. The device is protected against impermissibly high voltage and current
values, even when the measuring device is turned off. Another task that was imposed
during the design was solved: a simple cheap microcontroller was used, and no digital
components were added.
The working surface of the display has dimensions of 116x87 mm. The dimensions of the
box are 218x145x52 mm and the device is easily portable.

REFERENCES
[1] A. Dedić, "Graphical presentation of vectors of electrical quantities using a TFT display," in
Proc. Information technologies, 2010, pp. 41-44.
[2] A. Dedić, “A microcontroller based voltage and current measurement method,” in Proc.
Information technologies, 2015, pp. 68-71.
[3] Analog Devices Inc, CMOS Low Voltage 4 Quad SPST Switches ADG711 /ADG712 /ADG713,
www.analog.com
[4] Linear Technology Corporation, LTC 2312-14 14-Bit, 500ksps Serial Sampling ADC in TSOT,
www.linear.com
[5] Microchip Technology Inc, dsPIC33EP PIC24EP Family Reference Manual,
http://support.microchip.com
[6] Vbest Electronics LTD, VGG322403-7UFLWA(LF), http:// www.vbest.com.tw

You might also like