A Power Frequency Sensing Device Using an Arduino Device and Zero-Crossing Algorithm and Its Implementation on Android App

In this study, an Arduino controller is used to build a low-cost data acquisition (DAQ) sensing device with a sampling frequency of up to 50 kHz. The utility power signal captured through the Arduino controller is converted into digital data, which are then transmitted through a WiFi module to an Android mobile device so that mobile devices can display the signal waveform and power quality information. Using a discrete Fourier series (DFS) filtering algorithm, noise and high harmonics are filtered out, and then a zero-crossing algorithm is applied to accurately calculate the fundamental frequency of the power signal. Since the power signal is an AC signal with high voltage, we designed a voltage step-down circuit for signal preprocessing to adjust the signal level to the standard level accepted by the Arduino controller. Research confirms that we can design a portable power signal acquisition device to view signal waveforms using an Android app and calculate the frequency variation of power signals. Additionally, the power signal data can also be conveniently transmitted to a cloud server to record and analyze unusual power events.


Introduction
In recent years, the use of nonlinear power system loads has been increasing, so the harmonic pollution of power line quality has been growing.When an imbalance between power supply and demand occurs between power systems, it results in system frequency variation, (1) and these changes are likely to cause accidents or damage to the power line system.Owing to current technological advances, conveniences in life, and increases in the amount of livelihood equipment, power consumption has been increasing so that frequency changes are quite common.The degree of change varies depending on the load characteristics and design of the power supply system. (2)n power line system operation, when the frequency is lower than some nominal value, the system is under the conditions of overload.However, if the frequency is higher than the nominal value, it shows an oversupply to the power lines.The frequency represents whether the power line reaches an equilibrium state between supply and demand, so frequency is a very important indicator of the reliability, safety, and economy of a power line system.In this study, an Arduino sensing device is used to build a low-cost data capture card combined with mobile devices to build a portable power line frequency detection device.By observing changes in the frequency, we can immediately detect the status of the power line system operation, and interferences in the power line quality can be monitored at the same time.We also present a full analysis, including data resolution and sampling frequency, to describe the feasibility and simplicity of our Arduino sensing device.

Research Methods
In this study, first, a power signal sensing module, composed of a signal conditional circuit, namely, Arduino, and a data transmission device, was developed; the system architecture is shown schematically in Fig. 1.In this sensing module, the power signal is pretreated through voltage step-down and voltage level shift processing.After that, the signal data processed by the Arduino analog-to-digital converter (ADC) module (3) is digitalized, packaged, and then transmitted to a mobile device.The signal collected by the Arduino is then monitored at any time in the Android app.A cloud server works to complete the function monitoring power line quality. (4,5)A discrete Fourier series (DFS) is used to filter out high harmonics and noise to collect the fundamental sine wave signal.The data matrix of the fundamental sine frequency is stored in the data array of the mobile device, and then the zero-crossing algorithm is used to estimate the actual frequency of sinusoidal components.The DFS algorithm and zero-crossing algorithm can be reused in monitoring the high frequency and amplitude of the original signal.

Power Signal Sensing Device
The power signal sensing device is composed of three modules: a signal conditional circuit, an Arduino ADC, and a data transmission module.Initially, the amplitude of the power signal first uses voltage level shifting to connect the utility power signal to the Arduino analog input.Additionally, we also analyze the frequency issues and data resolution with the ADC function of the Arduino.After that, the relationship between the sampling frequency and the delay time is thoroughly discussed.Finally, environmental information including the temperature, humidity,

Power signal level shifting circuit
Since the power signal input is a 60 Hz AC signal and 110 V root mean square (RMS), the utility power voltage peak is about 155.5 V.However, the signal level sent into the Arduino ADC module is only 0 to 5 V; the power signal level shifting circuit is shown in Fig. 3.The amplitude level of the power signal is adjusted using the following formula: where V out1 sets the R 2 R 1 ratio to 10 kΩ 1 MΩ = 0.01, making the AC voltage decrease from ±155.5 to ±1.55 V, and V out2 is added with a 2 V DC signal to accept the conversion from an analog to a digital signal.
The voltage level is adjusted to 0.5-3.6V to match the input signal of the Arduino ADC module.Figure 4 shows the multisim simulation results, where ch1 is the power signal input, and ch2 (triangle mark on the waveform) is the simulation signal after the voltage level adjustment.The simulation showed the desired consistency with the circuit design.e Arduino data acquisition (DAQ) approach is to use analogRead (0) instructions to convert analog information into digital information (ADC module).The chip used in the Arduino is the ATMEG chip; its oscillation frequency is 16 MHz and its internal ADC module prescaler setting is shown in Table 1.

Power signal acquisition in the Arduino ADC module
Generally, the default value of the ATMEG ADC (6) prescaler is 128, and the implementation of an ADC function requires 13 cycles.Therefore, the ADC processing speed is 16 MHz divided by (128/13) and the ADC sampling rate is 9600 Hz.If the prescaler is set to 16, the sampling rate in the Arduino ADC module can be theoretically increased to about 77 kHz (16M /16/13).If sampling accuracy is taken into account, the sampling frequency needs to maintain more than ten times the measured signal.Thus, we can use the Arduino ADC module to measure a periodical signal of 7.7 kHz (10 sampling points per period).Since the fundamental frequency of power signals is 60 Hz, and 25 times the harmonic wave is only 1.5 kHz, the Arduino ADC module is sufficient to meet the requirement of our study.

Data resolution design in Arduino
The Arduino ADC has 10-bit resolution, thus the captured dynamic signal amplitude can be resolved into 1024 (2^10) deciles.The internal reference voltage of the dynamic signals is 3.3 or 5 V, but the voltage also varies with the method of the Arduino power supply, as shown in Table 2.
For example, when the reference voltage is 5 V, the voltage upon receiving an analogRead(0) is converted to (1023 * analogRead(0) / 5).When the input voltage is 3 V, its value displayed by the analogRead(0) function is about 614.When the dynamic signal variable becomes small (e.g., 0.1-1 V), the reference voltage signal will become 1 V to improve the resolution.We can connect the AREF pin of the Arduino to 1 V, and software analogReference (EXTERNAL) is used to set the reference voltage to 1 V to improve resolution.To verify the actual sampling rate of the Arduino ADC, this  The instruction Micros() executes simultaneously to record the read time of each data.The partial data from 100 to 106 recorded in Table 3 are read and the program execution times are recorded.The captured ADC data is drawn by Excel as shown by the red line in Fig. 6.
It is worth noting that the sampling interval for the data obtained in Table 3 is 24 μs, and the sampling frequency is approximately 41.6 kHz.For the 200 data points shown by the red line in Fig. 6, there are 41 points for each cycle, so the actual sampling frequency by program is (1 kHz) × 41 = 41 kHz, which is quite consistent with 41.6 kHz.

Sampling frequency adjustment in the Arduino
To maintain the estimation accuracy for the following zero-crossing algorithm, we added a delay instruction code to let the Arduino ADC module have power signal data over a fixed duration and modulated the sampling rate of the Arduino ADC module.
The delay times for our experiment processes were set to 5, 10, 15, and 20 μs.The corresponding sampling points per period obtained by the zero-crossing algorithm were 39.449, 32.700, 28.124, and 24.374 points, respectively.Since we provide a frequency of 1 kHz for the Arduino ADC module, the sampling frequencies are 39.449, 32.700, 28.124, and 24.374 kHz, respectively.If we take the reciprocal of the sampling frequency, the sampling period can be obtained and plotted as shown by the red lines in Fig. 7. To predict the delay time influence on the sampling frequency, the least-squares method was used (8) as shown in Fig. 8 to predict the relationship of program latency time (X) and sampling period (Y).The linear regression equation is where c = 20.12601 and a = 1.040209.

Power Signal Analysis
A high-accuracy and simple zero-crossing algorithm accompanied by the DFS has been provided to calculate the power signal quality effectively.The zero-crossing algorithm uses a very simple concept and provides fast computing performance.However, the zero-crossing algorithm function is apt to be limited by large-amplitude harmonics, non-integral harmonics, or even noise.Therefore, we first use a DFS approach to filter out the high-order harmonics and noise.Then, the zero-crossing algorithm is applied to accurately obtain the baseband amplitude and frequency.4.1 Discrete Fourier sequence algorithms for extracting fundamental waves (9) The voltage signal to be measured can be expressed as −12) If the exact value of the fundamental frequency is unknown, we assume that the angular frequency of the fundamental frequency is ω a , V a (DFS parameter) is the estimated amplitude of fundamental signal, m is the number of samples sampled by the fundamental frequency wave period, and v n is the n-th sample value of signals.Since . With the fetched sample value v n , the DFS can be expressed as For the i-th data window, its sine and cosine components, A(i) and B(i), can be calculated as follows.
The term C(i) a = A(i) 2 + B(i) 2 is the estimated amplitude of the fundamental frequency of the i-th data window, φ f = B A , and C(i) is accurate only when ω a = ω.The signal sampling frequency is f s = m/T a = 1/T s , where T s is the sampling period.If the data window is swept by the measured signals, Eqs. ( 5) and ( 6) can provide A(t) and B(t) with the corresponding values of a cycle where the sine and cosine components, A and B, are periodic functions of time.If the frequency of the fundamental harmonic signal is equal to the basic frequency of the Fourier series ( A(t) and B(t) in frequency are orthogonal to each other; A(t) is the pure cosine wave, and B(t) is a pure sine wave; when T a ≠ T, A(t) and B(t) are not pure sine and cosine waves, but their fundamental frequency is still f.
Figure 9 shows a 60 Hz fundamental frequency and its harmonic signal, and the contents are assigned as 100% of the 1st harmonic, 40% of the 3rd harmonic, and 30% of the 5th harmonic.In practice, the cosine and sine signals in Eqs. ( 5) and ( 6) can be represented by the vector matrix of length m as Consider a power signal waveform to be applied by a zero-crossing algorithm.Such a sine wave can be represented as where A is the amplitude, ω = 2πf is angular frequency, and f is the signal frequency.When the t =kT s signal represented in a discrete manner is substituted, Eq. ( 11) may be expressed as In the process of signal sampling, the angle difference between two continuous points is where T s is the sampling interval.Assuming that the sampling points of a full cycle is m, the signal cycle is equal to the sampling interval multiplied by the number of points.Therefore, the signal frequency can be calculated as In practice, because the sampling frequency is not an integer multiple of the actual signal frequency, the signal period is not an integer multiple of the sampling period.Therefore, an accurately calculated m value is not usually an integer.When we observe the T i cycles, Fig. 10 shows zero crossing points (Y i−1 , Y i ) from negative to positive, and secondly, zero crossing points (Y i+m , Y i+m+1 ) from negative to positive, and the exact period of the sine wave can be expressed as Fig. 10.Sine waveform for zero-crossing algorithm calculation.
Because the sampling interval is not an integer multiple of the signal cycles, and m A is not an integer, according to Fig. 10, m A is obtained as where δ 2 is the time difference between the i-th sampling point and the zero point (T i ).In Fig. 10, using a definition similar to the trigonometric, Eq. ( 17) is expressed as The true frequency of test signals can be estimated as Even if y i or y m is separately located under the conditions of T i and T i+1 (zero amplitude value), Eqs. ( 17) and (18) can still be used for frequency detection.In terms of electric power measurement algorithms, the frequency estimation is based on zero-crossing algorithms, so the algorithm is simple and fast.As a matter of fact, several factors still exist that affect the computation of the zero-crossing algorithm such as greater harmonic amplitude, non-integer harmonics, and even noise.That is the reason why the modified Fourier series approach is proposed and applied in this research.In other words, by adding Fourier-series-based filtering, the traditional zero-crossing algorithm can considerably improve the calculation of the fundamental frequency. (13) our research, we try to focus on a convenient and low-cost mobile device for power signal measurement.It is worth noting that we have provided here not only a simple and inexpensive frequency measurement device but also an acceptable and comparable accuracy with an error rate smaller that 0.1% by using a simple computing Arduino device and an Android app.That is, we do not need a high-performance processor and memory space to process the frequency measurement when using the Fourier algorithm and zero-crossing technique.

Measurement and Verification
In Ref. 13, the traditional Fourier algorithm provided an error range about 0.1940-0.0110%,and the improved Fourier algorithm (13) yielded better results, a 0.0638-−0.0001%error range, which is calculated based on the empirical model and used the least error squares to fit the error compensation curve to correct the result.Table 4 shows the comparative results for M1, changing the amplitude of harmonic components; M2, changing the phase of harmonic components; and M3, changing both the phase and the amplitude of harmonic components.6) In this study, using the app Inventor2, we wrote the Android program, used LabVIEW simulation to mix three different frequencies and amplitudes of sine waves, and adopted NI-myDAQ to output signals while simultaneously receiving the waves in LabVIEW as shown in Fig. 11.Signals were captured by the Arduino and then transmitted to a web site via WiFi Shied; the Android app captures the web site data, which was then drawn by Google Chart API as shown in Fig. 12.Compared with a portable device drawing data received directly from NI-myDAQ, the measured error rate is less than 0.1%, which confirmed the Arduino hardware's reliability and practicality.

Power quality algorithm verification
The purpose of this study was to measure the signal quality of utility power.After stepping down voltage using the circuits shown in Fig. 13, the signals captured by the Arduino are processed by ADC via WiFi Shield network transmission, so the Android app can plot and monitor the signals after receiving them.After the power signal acquisition, Fourier filter calculation is based on Eq. ( 6) as in the flowchart shown in Fig. 12, and then Eq. ( 17) is used to calculate the zerocrossing algorithm.The cycle time and frequency of power fundamental signals are calculated as shown in Fig. 14. Figure 15 shows the step-down devices for utility power and the photos received by the mobile devices.Figure 16 shows the stepped-down utility power signals received by the Android app and drawn by Google Chart API plotting.Thus, the noise of this wave is filtered out by Fourier calculation to make it a sine wave, as shown in Fig. 17

Conclusions
In this paper, we have shown that the Fourier algorithm can be used as digital filter to extract cosine and sine waveforms of the fundamental frequency component, and the zero-crossing method can be applied to obtain the baseband amplitude and frequency.Additionally, we had shown that the Fourier algorithm accompanied with zero-crossing approach is robust and effective.Using the DAQ card NI myDAQ, the Arduino DAQ accuracy shows less than a 0.1% error rate, which is better than the traditional Fourier algorithm.Additionally, our research results show the suitability of a low-cost mobile device.
It is planned that power signals captured by a DAQ card device will be sent to a cloud server by Android mobile devices to analyze the operation of captured signals.Finally, mobile devices are used to monitor the measured results.Finally, we have completed in this paper research on the DAQ technology, development of the power line quality algorithm , development of the WiFi mobile phone application, and implementation of the client-server system for power signal analysis.

Fig. 1 .
Fig. 1. (Color online) Diagram of mobile device for power signal monitoring.

Fig. 12 .
Fig. 12. (Color online) Mixed signal drawing received on a mobile device.

Table 3
Arduino ADC module data.