12753 FACTA UNIVERSITATIS Series: Electronics and Energetics Vol. 38, No 1, March 2025, pp. 19 - 38 https://doi.org/10.2298/FUEE2501019S © 2025 by University of Niš, Serbia | Creative Commons License: CC BY-NC-ND Original scientific paper VEHICLE DETECTOR BASED ON THE MAGNETIC FIELD SENSOR AND THE FIXED-THRESHOLD ALGORITHM IMPLEMENTED VIA FINITE STATE MACHINE Milan Stojanović, Ljubomir Vračar University of Niš, Faculty of Electronic Engineering, Niš, Serbia ORCID iDs: Milan Stojanović https://orcid.org/0000-0002-3474-7968 Ljubomir Vračar https://orcid.org/0000-0003-0258-7012 Abstract. An automated vehicle detection process is one of the main objectives of Intelligent Transportation Systems. Different technologies, such as inductive loops, video surveillance, or acoustic sensors, are used for vehicle detection. A one of the most effective solution is based on magnetic field measurement. This detection method relies on analysis of the Earth's magnetic field changes induced by ferromagnetic materials inside a vehicle's structure. The vehicle detector composed of the magnetic field sensor used for measurements and the microcontroller with embedded detection algorithm is described in this paper. The detection algorithm compares changes in the magnetic field with a defined fixed-threshold value implemented as the finite state machine. Changing the state of the finite state machine in the defined order means that the vehicle is detected successfully. The method for determining the finite state machine parameters that depend on the fixed-threshold values and frequency of the measurements is described in detail. The vehicle detector is tested in the laboratory environment, and the results show the appropriate detection accuracy compared to the other detection algorithms in the literature. Key words: vehicle detector, magnetic field sensor, vehicle detection algorithm, finite state machine, fixed-threshold value 1. INTRODUCTION Automated traffic management as part of Intelligent Transportation Systems, is relayed on accurate vehicle detection. Information about a vehicle’s presence provides effective traffic light control. The vehicle detector, placed at the beginning of the street, can optimize the duration of the red and green lights depending on the number of detected vehicles. The detector also makes finding available parking spots easier. It can be installed at the entrance of the parking lot or in every parking spot with appropriate Received May 29, 2024; revised July 30, 2024, and September 22, 2024; accepted October 6, 2024 Corresponding author: Milan Stojanović University of Niš, Faculty of Electronic Engineering, Aleksandra Medvedeva 4, 18000 Niš, Serbia E-mail: milan.stojanovic@elfak.ni.ac.rs https://orcid.org/0000-0002-3474-7968 https://orcid.org/0000-0003-0258-7012 20 M. STOJANOVIĆ, LJ. VRAČAR modification of the detection algorithm. A vehicle detection process relies on various methods [1], typically categorized as intrusive and non-intrusive. Intrusive methods include inductive loops [2] and piezoelectric sensors [3]. The use of embedded traffic monitoring devices can cause disruptions in traffic flow during installation and maintenance due to their placement beneath the road surface or pavement, which can be a drawback. The main advantage of non-intrusive methods is the simple installation next to the road or on the road surface. This category includes methods based on video image processing [4], acoustic sensors [5]. The method based on the magnetic field sensor [6-9] belongs to both categories because it can be installed into the road or the surface of the road or pavement. All detection methods have advantages and disadvantages. The detectors based on inductive loops are a well-known technology that gives highly accurate results and is resistant to weather conditions. On the other hand, the installation process of inductive loops is complicated, and they have low resistance to vibrations caused by vehicles. Video detectors provide multi-line surveillance and plenty of data, but they are affected by weather conditions (rain, snow, fog), shadows, and time of day. Acoustic detectors have a low power consumption and easy installation process. However, they require high computational power for signal filtering and processing. The detectors based on the magnetic field sensor operate on a principle similar to that of the inductive loop detectors. Their primary advantages are a simple installation process, low power consumption, and the possibility of wireless communication. Of course, they have certain disadvantages, such as the influence of vehicles in adjacent lines on the measurement. Ferromagnetic materials contained in vehicles affect the Earth’s magnetic field [10]. A vehicle detection process is performed by analyzing changes in the Earth’s magnetic field induced by a vehicle. The magnetic field sensor can be placed on the road or at the side of the road. The detection algorithms, in both cases, are based on the measurement of the magnetic field per one single axis or two or three axes. Paper [11] describes the detection algorithm considering the magnetic field per single axis perpendicular to the road surface, with the magnetic field sensor placed at the side of the road. A more complex approach to vehicle detection is performed by analyzing the magnetic field per two axes. The detection algorithm presented in the paper [12] is based on finding the peaks and values of the magnetic signals per axis parallel to the vehicle travel direction and the other perpendicular to the road surface, with the sensor placed on the road. Paper [13] describes the detection of vehicles by the magnetic field sensor placed at the side of the road, using the detection algorithm that analyzes the angle between the magnetic field of the parallel and the perpendicular axes to the road surface. The most complex detection algorithm considers the magnitude of the magnetic field that is calculated using the measured values per all three axes [14]. The detection algorithm is implemented as a Finite State Machine (FSM) whose input parameter is some quantity of the magnetic field. The analysis of changes in the magnetic field enables, in addition to vehicle detection, to determine some other characteristics of vehicles. The different methods of magnetic signal analysis to determine the velocity of the detected vehicles using the one or two magnetic field sensors are described in the papers [15, 16]. The vehicle’s movement direction is determined by using the same magnetic signal of two sensors or by analyzing the ratio of magnetic field per two axes of a single sensor [17]. Each type of vehicle creates a unique intensity and waveform of magnetic field distortion depending on the quantity and distribution of ferromagnetic materials inside its structure. That provides the possibility to perform classification of the detected vehicles according to waveforms of Vehicle Detector Based on the Magnetic Field Sensor and the Fixed-threshold Algorithm Implemented... 21 measured magnetic field [18-20]. Heavy vehicles contain more ferromagnetic materials, influencing the higher changes in the magnetic field. The metal load in the truck could produce greater changes in the magnetic field and a different waveform of a part of the changes, which should be kept in mind when designing the detection algorithm. A more accurate classification is achieved by implementing the accelerometer and the magnetic field sensor [21]. The accelerometer counts the number of the vehicle’s axles by measuring the vibration intensity. Vehicles induce changes in the magnetic field whenever they move or stand still. That provides the realization of smart parking systems using vehicle detectors based on the magnetic field measurement [22, 23]. All detection algorithms are based on the threshold value of the magnetic field. If a vehicle or another ferromagnetic material is present in the sensor sensitivity area, the measured magnetic field value will be higher or lower than the predefined threshold value. The first group includes the algorithms with the fixed-threshold value, defined in advance, and is not changeable during the detector's operation. The algorithms, with threshold values that adapt to previously measured values, belong to the second group. Some novel research papers propose using the magnetic field sensor in combination with the Artificial Neural Network to improve the accuracy of vehicle detection and classification processes [24]. The strain sensor and the Computational Neural Network are used for the same purpose [25]. However, methods based on Neural Networks are computationally intensive, so solutions based on microcontrollers are still implemented widely. This paper describes the vehicle detector based on the magnetic field measurement induced by vehicles, which consists of a digital magnetic field sensor and microcontroller. The presented vehicle detector can be placed on the surface or immersed in the road, and it provides accurate vehicle detection in a velocity range from 3.6 km/h up to 120 km/h. This velocity range covers the vehicle's movement in a city environment and highways. The detection process relies on analyzing the magnetic field per single axis, the one vertical to the road surface. The data size, obtained by measurements on one axis, is not too large and does not require high computing power, which enables precise analysis in real-time using a microcontroller. A detection algorithm with a fixed threshold built into the microcontroller as FSM performs the analysis of measured values, which is a well- known approach in the scientific literature. The main advantage of our approach represents the simplicity of the FSM, which has only four states. Additionally, the process of determining the FSM parameters, which depend on the selected fixed-threshold value and the measurement frequency, is described in detail. This process is carried out using the magnetic signature of a single vehicle, and the detection algorithm will be tested with multiple signatures of the same type of vehicle to verify its accuracy. It represents another novelty of this paper regarding the available literature. The magnetic field distortion induced by the vehicle was recorded experimentally on the road, while the testing of the designed vehicle detector was conducted in the laboratory using the developed magnetic field generator. The rest of the paper is organized as follows. Section 2 presents the process of recording the magnetic signature of a vehicle. The vehicle detection algorithm is shown in Section 3. The FSM, part of the detection algorithm, is described in detail in Section 4. Section 5 contains the results of vehicle detection using the described detection algorithm in the laboratory environment. Finally, the conclusion and future research plans are described in Section 6. 22 M. STOJANOVIĆ, LJ. VRAČAR 2. RECORDING THE MAGNETIC SIGNATURE OF A VEHICLE The change in the magnetic field (magnetic signature) depends on the position of the sensor related to the vehicle, the vehicle's type, and the vehicle's velocity. The intensity of the change in the magnetic field depends on the distance between the vehicle and the sensor, which is especially important for a sensor installed next to the road. In this case, vehicles such as buses or trucks can induce false detection although they are moving in the further lane of the road. Vehicles that contain more ferromagnetic material cause greater changes in the magnetic field intensity than vehicles made of less ferromagnetic material. The time interval during which there is magnetic field distortion depends on the velocity and length of the vehicle. As the velocity of the vehicle increases, the duration of the magnetic field distortion decreases, which results in fewer measured values for the detection algorithm. In addition, longer vehicles affect the magnetic field over a longer time interval. The magnetic field distortion per x, y, and z-axis can be described as a magnetic dipole with a magnetic moment m centered in a vehicle using the Eq.(1)-Eq.(3). These equations are derived from Maxwell’s Equations and represent the relation between the magnetic field and magnetic moment per each axis [26]. 2 2 2 0 5 ( (2 )) 3 3 4 x y z x m x y z m xy m xz B r    − − + + = (1) 2 2 2 0 5 ( (2 )) 3 3 4 y x z y m y x z m xy m yz B r    − − + + = (2) 2 2 2 0 5 ( (2 )) 3 3 4 z x y z m z y x m xz m yz B r    − − + + = (3) where mx, my, and mz are the magnetic moments in each axis, µ0 is the permeability of the air, and r is the distance from the sensor to the dipole (vehicle). Although the presented equations reconstruct magnetic signatures accurately, they provide the starting point in the software magnetic signature simulation processes. The algorithm for vehicle detection in this paper is developed using the recorded magnetic signature. The author’s private vehicle (VW Polo9N) was used to record the magnetic signature. The system for measurement is composed of the microcontroller PIC18F45K22 and the three-axes digital magnetic field sensor BM1422AGMV [27]. Fig. 1 presents the vehicle influence on the Earth’s magnetic field lines, the placement of the three-axis magnetic Fig. 1 The influence of a vehicle on the Earth's magnetic field, and the magnetic signature of VWPolo 9N recorded by the magnetic field sensor placed on the road surface Vehicle Detector Based on the Magnetic Field Sensor and the Fixed-threshold Algorithm Implemented... 23 field sensor on the road, and the magnetic signature of VW Polo9N composed of 100 samples. The measurement frequency was 100 Hz, and the vehicle velocity was 20 km/h. The magnetic field sensor installed on the road surface measures the Earth's magnetic field. The x-axis is oriented parallel, and the y-axis is pendicularly to the vehicle movement direction. These two axes lie on the plate of the road surface. The z-axis is perpendicular to the vehicle movement and the road surface. The vehicle induces the highest intensity changes in the magnetic field per z-axis and the smallest per y-axis. Such changes are expected, considering the way the sensor axes are oriented. Because the intensity of the magnetic field changes is the greatest per z-axis, the detection process is based on these values. The sensor also detects changes in the magnetic field created by vehicles in the neighboring lane. However, these changes are most intense along the sensor axis perpendicular to that lane. Vehicles in neighboring lanes can be detected by these changes. The axis on which the magnetic field changes the most indicates that the vehicle is moving over the sensor or in the neighboring lane. In this way, the superimpose of magnetic signatures is avoided. The detection process using the measured values along all three is based on analyzing the changes of the magnetic field magnitude M, described by Eq. (4). x y zM B B B= + + (4) The magnetic field per x, y, and z-axes are denoted as Bx, By, and Bz, respectively. In this case, the change of value M is the input parameter for the FSM. This approach, in combination with a neural network, can provide more details about vehicles, but it demands more computation power and longer execution time. That is the main reason our approach uses the values measured along the single axis. Fig. 2 shows the magnetic signature of VW Polo9N (Bz), the baseline value (Bb), as well as the upper (Bup) and lower (Bdown) limit values. The baseline value represents the magnetic field distortion without a vehicle presence. Since the magnetic field is constant over a wide area, the distortion value is zero in the absence of vehicles or other ferromagnetic materials in the sensor’s sensitivity area. The detector can be installed where the magnetic field differs significantly from the values shown in the text. In that case, the magnetic field should be analyzed along other axes or the magnitude of the magnetic field. The difference between the upper/lower limit value, and the baseline value (by absolute value) presents the threshold value (Bth). During the movement of the VW Polo9N, at a velocity of 20 km/h, the changes in the magnetic field last 840 ms, or 85 samples. The duration of the magnetic signatures at the highest and lowest velocity is calculated considering that a change in vehicle velocity affects only the duration of the magnetic signature, but not the waveform of the signature. The maximum considered velocity is 120 km/h, therefore the duration of the magnetic signature is 139.87 ms or 14 samples, while the duration of the Fig. 2 The recorded magnetic signature of VW Polo9N per z-axis. Moving velocity is 20 km/h, and sensor placed on the road 24 M. STOJANOVIĆ, LJ. VRAČAR magnetic signature at the 3.6 km/h (minimum velocity) is equal to 4662 ms or 467 samples. Therefore, the number of samples depends on the vehicle velocity and the frequency of measurements. The vehicle detector’s structure is the same as the measurement system. The sensor measures the magnetic field, and the microcontroller determines vehicle passage based on the embedded detection algorithm that analyzes the measured values of the magnetic field. 3. THE DETECTION ALGORITHM The detection algorithm consists of four parts, as presented in Fig. 3. The first part includes the magnetic field measurement. The second part is to calculate the difference between the currently measured value and the baseline value. The third part is the updating of the baseline value. The fourth and last part is the FSM. Fig. 3 The block schematic of the detection algorithm 3.1. Magnetic field measurement The magnetic field measurement consists of reading the values measured by the sensor for all three axes. Although values from the z-axis are used during analysis, it is necessary to read the measurement values for all three axes from the sensor to enable the sensor to perform the following measurement. The values obtained from the sensor are 14-bit numbers written in two’s complement, so it is necessary to transform them into the appropriate format for analysis. Converting these values into decimal form gives the values of the magnetic field expressed in units of µT. The microcontroller collects measured data at a frequency of 100 Hz, and a new measured value is processed every 10 ms. 3.2. Calculation of the difference The difference between the currently measured value Bz and the baseline value Bb is calculated in the next step. The calculated difference, per absolute value, is noted as Bdiff. The distortion of the magnetic field caused by the presence of vehicles is of interest, while slow rate changes caused by the movement of the Earth or changes in its structure (the structure of the Earth's core), as well as changes in readings due to temperature changes, should be ignored. 3.3. Baseline updating The value measured by the sensor will differ with the temperature change, even at the same intensity of the magnetic field. The calculation of Bb should include these low- intensity changes, and eliminate them. In contrast, all high-intensity changes, possibly Vehicle Detector Based on the Magnetic Field Sensor and the Fixed-threshold Algorithm Implemented... 25 induced by vehicle, should be eliminated so they do not affect the baseline value. That is achieved by the slow rate limiter filter implemented in the software. If the Bdiff is smaller than the defined Bth, the Bb is updated, increased, or decreased. The updated Bb value is used for Bdiff calculation in the following measurement. If the Bdiff is greater than the Bb, the Bb is not changing. 3.4. Finite State Machine An FSM is a mathematical model used to represent and control the behavior of a system. It consists of a set of states, a set of input events and output actions, and a set of transitions between states [28]. The FSM, designed for vehicle detection algorithm, consists of 4 states: State 1 – below the threshold value, State 2 – above the threshold value, State 3 – present vehicle, and State 4 – the recalculation of the Bb, Bup, and Bdown values. The state change occurs when a certain condition is fulfilled during the specified time interval, as illustrated in Fig. 4. Fig. 4 The recorded magnetic signature of VW Polo9N per z-axis and FSM’s states. Moving velocity is 20 km/h, and sensor placed on the road State 1 - below the threshold value: The FSM is in this state before the first measurement. While Bdiff < Bth, the FSM remains in this state. When Bdiff ≥ Bth, the FSM goes to the State 2. State 2 - above the threshold value: If in State 2, Bdiff ≥ Bth, during the time interval t23, the FSM goes to State 3. It means, there is a distortion in the magnetic field that should be induced by a vehicle. Conversely, if Bdiff < Bth during the time interval t21, the previous distortion is not significant, and the FSM returns to State 1. State 3 - possible vehicle presence: Different vehicle models will cause different changes in the magnetic field. As shown in Fig. 4, due to the passing of the observed vehicle, Bz first decreases, and then increases, along the z-axis. When a vehicle is no longer in the sensor sensitivity area, the FSM should return to State 1. If Bdiff < Bth during the time interval t31, and FSM goes from State 3 to State 1, a vehicle is detected successfully. Another possible scenario in State 3 is Bdiff ≥ Bth during the time interval t34. If this happens, there is ferromagnetic material near the sensor or above it (it can also be a 26 M. STOJANOVIĆ, LJ. VRAČAR parked vehicle) that inserts a constant offset during the measurement, and the FSM goes to State 4 (Fig. 5). Fig. 5 Recorded magnetic field changes per z-axis and FSM’s states in case of constant presence of a ferromagnetic material. Obtained by a measurement where a ferromagnetic material is placed above the sensor State 4 – recalculation of Bb. The constant offset in the magnetic field represents the difference between Bz and Bb without the vehicle’s presence. Therefore, Bb must be recalculated to eliminate the influence of the offset. The new Bb value is calculated as the mean value of k measured values. The new values of Bup and Bdown represent the Bb value increased and decreased for Bth, respectively. Once a vehicle has been detected, the FSM transitions back to State 1, prepared to detect the next one. The duration of the FSM's states depends on the listed time intervals (t21, t23, t31, and t34). All time intervals are defined in the algorithm before the detection process starts, and the detection accuracy depends on their values. If the duration of the specified condition in the current state is fulfilled continuously during the appropriate time interval, the FSM goes to the next state. It implies the necessity to measure the duration of the current state using the microcontroller timer. The counter whose value increments if the necessary condition within the current state is fulfilled, is used instead of the timer. The frequency of condition examination is the same as the frequency of the measurement. If the condition is not fulfilled continuously, or the FSM goes to the next state, the counter resets itself. The duration of the FSM's states depends on specified time intervals and the Bth value. As the frequency of analyzing the measured data is equal to the frequency of measurement, time intervals for each state can be calculated using Eq. (5). 1N t f − = (5) N represents the number of consecutively measured values that satisfy the inspected condition, and f is the measurement frequency. In this way, the implementation of the algorithm on the microcontroller is simplified because it is not necessary to use the timer. Each time interval t corresponds to one counter value N, which determines the changing Vehicle Detector Based on the Magnetic Field Sensor and the Fixed-threshold Algorithm Implemented... 27 state of the FSM, as presented in Table 1. If a value of the counter within the present state reaches the threshold value, the FSM goes to the next state. The value of counter can be calculated using the Eq. (5). Table 1 Changing FSM’s state conditions Present State Timer Counter Magnetic field condition The Next State 2 t = t21 N = Nth21 Bdiff < Bth 1 2 t = t23 N = Nth23 Bdiff ≥ Bth 3 3 t = t31 N = Nth31 Bdiff < Bth 1 3 t = t34 N = Nth34 Bdiff ≥ Bth 4 The values Nth21, Nth23, Nth31, and Nth34 represent the values of the counters at which the state changes. The first number in the index represents the present, and the second number the next state of the FSM. For example: if the FSM is in State 2, and Bdiff < Bth during the time interval t21 (counter has value Nth21), the FSM goes to State 1. The diagram of the designed FSM with corresponding changes of states is shown in Fig. 6. Fig. 6 The diagram of the designed FSM implemented in the algorithm 4. DETERMINATION OF FSM PARAMETERS 4.1. Time interval t21 The time interval t21 determines after how much time the FSM will return from State 2 to State 1 if Bdiff < Bth. This change of state occurs in the case of short-term peaks of the magnetic field that are not equivalent to the changes caused by a vehicle. If one such change occurs, the FSM will go from State 1 to State 2, after which it is necessary to return to State 1 to avoid misinterpretation of the following change in the magnetic field. If the FSM does not return to State 1, after a short-term peak, two scenarios can occur. The first - a vehicle after a short-term peak. The FSM goes from State 1 to State 2 because of the peak. In this case, it will happen that the transition from State 2 to State 3, 28 M. STOJANOVIĆ, LJ. VRAČAR and the transition from State 3 to State 1, will occur earlier under the influence of a vehicle than it would be the case if the FSM were initially in State 1. However, that would not affect vehicle detection accuracy as shown in Fig. 7. Fig. 7 Recorded magnetic field changes per z-axis and FSM’s states in the case of one short-term peak and the VW Polo9N passing. A short-term peak is obtained by moving the ferromagnetic material over the sensor before the vehicle The second - two short-term peaks. The FSM goes from State 1 to State 2 because of the first peak (Fig. 8). An incoming peak may cause the change to State 3, and then return to State 1. Such a state change is identical to the one in the case that a vehicle passes over the sensor, which is considered a detection error. Therefore, the FSM should return from State 2 to State 1 when Bdiff < Bth, to avoid this scenario. Fig. 8 Recorded magnetic field changes per z-axis and FSM’s states in case of two short- term peaks. Short-term peaks are obtained by moving the ferromagnetic material over the sensor Vehicle Detector Based on the Magnetic Field Sensor and the Fixed-threshold Algorithm Implemented... 29 So, if the time interval t21 takes too long, it may happen that the FSM does not return to State 1 before the next possible peak occurs. In other words, the condition t21 < tbp must be met, where tbp is the time interval between two peaks. Since the value of tbp cannot be measured or predicted, t21 should have a minimum value, and the FSM should return from State 2 to State 1 immediately, when Bdiff < Bth is fulfilled. As only one measured value should satisfy this condition, the time interval t21 does not depend on the Bth. 4.2. Time interval t23 The time interval t23 determines when the FSM goes from State 2 to State 3 if Bdiff ≥ Bth. The value of this time interval is determined in the range from t23min to t23max. Time interval t23 should last long enough so the FSM does not move to State 3 due to the influence of short-term peaks, whose duration is shorter than t23min. A short-term peak is a magnetic signal occurring when a vehicle passes at a higher velocity than the maximum detectable velocity, as presented in Fig. 9. Fig. 9 Recorded magnetic field changes per z-axis in the case of one short-term peak, and its influence on the FSM’s state changes depending on time interval t23min. A short-term peak is obtained by moving the ferromagnetic material over the sensor before the vehicle On the other hand, the time interval t23 must be shorter than t23max, the time interval during which Bdiff ≥ Bth. If this condition is not fulfilled, the FSM will not go to State 3 under the influence of the vehicle, and when Bdiff < Bth the FSM will go from State 2 to State 1. This scenario represents an attempt to detect a vehicle whose velocity is higher than the maximum detectable velocity. Therefore, the value of t23max is determined under the maximum velocity at which vehicle detection is possible (Fig. 10). Time interval t23 should tend to the maximum value to suppress the influence of short- term peaks. In that way, peaks of different durations, and all changes in the magnetic field similar to vehicle induced at a velocity higher than the maximum, are eliminated. Based on Fig. 10, the measurement shows that t23max = 41.9 ms. The number of samples can be calculated using the known frequency of the measurement th23 23 1 5.19N f t=  + = . 30 M. STOJANOVIĆ, LJ. VRAČAR Fig. 10 The magnetic signature of VW Polo9N per z-axis at maximal detectable velocity and its influence on the FSM’s state changes depending on time interval t23max. The magnetic signature is obtained by scaling the measured one at 20 km/h Since the number of measurements must be an integer value, smaller than the calculated one, it is obtained that the limit value of the counter at which the FSM goes from State 2 to State 3 is Nth23 ≤ 5. 4.3. Time interval t31 Time interval t31 determines when the FSM passes from State 3 to State 1 if Bdiff < Bth. As in the case of t23, this time interval ranges from t31min to t31max. The t31min is determined experimentally, at the minimum velocity of the vehicle. When the vehicle (for which the measurement was performed) passes over the sensor, the magnetic signal has two amplitude values of the opposite sign (Fig. 11). Fig. 11 Recorded magnetic signature of VW Polo9N per z-axis with signed amplitudes. Moving velocity is 20 km/h, and sensor placed on the road Vehicle Detector Based on the Magnetic Field Sensor and the Fixed-threshold Algorithm Implemented... 31 There is a time interval (t31min) between these amplitudes, during which Bdiff < Bth. The time interval t31 must be greater than the time interval t31min, denoted in Fig. 12. If this is not considered, the FSM may detect amplitudes separately and detect two vehicles instead of one. Fig. 12 The magnetic signature of VW Polo9N per z-axis at minimal detectable velocity and its influence on the FSM’s state changes depending on time interval t31min. The magnetic signature is obtained by scaling the measured one at 20 km/h The time interval t31max, shown in Figure 13, begins when Bdiff < Bth within State 3 until the next vehicle arrives. If the t31 > t31max, the FSM will not return to State 1 and will not be ready to detect the next vehicle. The value t31max is measured at the highest velocity of the vehicle, and at the smallest value of the time interval between two consecutive vehicles. Fig. 13 The magnetic signatures of VW Polo9N per z-axis at maximal detectable velocity and its influence on the FSM’s state changes depending on time interval t31max. The magnetic signatures are obtained by scaling the measured one at 20 km/h 32 M. STOJANOVIĆ, LJ. VRAČAR The measured value of the time interval t31max, is equal to 532.55 ms. The interval of 32.55 ms starts at the moment when Bdiff < Bth. This interval ends when the vehicle is out of the sensor sensitivity area. A time interval of 500 ms represents the minimum time between the passages of two consecutive vehicles. The value of t31 should tend to the minimum value for the FSM to return to State 1 earlier so that it is achieved to be ready to detect the next vehicle. The measured value of t31min, at the vehicle velocity of 3.6 km/h, is 87.37 ms, so the number of samples is Nth31 = f  t31 + 1 = 9.73. The number of samples must be an integer value, greater than the calculated one, so Nth31 ≥ 10. 4.4. Time interval t34 When the FSM enters State 3, the difference between the measured and the baseline value of the magnetic field is greater or equal to the threshold value. If Bdiff ≥ Bth still applies after the time interval t34, it is considered that there is some ferromagnetic material above the sensor, or in its vicinity, due to which a change in the magnetic field occurred. In that case, the FSM moves to State 4. In State 4, the baseline value is recalculated, after which the FSM returns to State 1. After removing the ferromagnetic material, the FSM will go through the same states again. It is necessary to determine the optimal duration of the time interval t34 so that it has a minimal impact on the detection procedure. The minimum value of this time interval (t34min) begins at the moment when the FSM enters State 3 (Figure 14), and ends when Bdiff < Bth, at the minimum vehicle velocity. In this way, the change of State 3 to State 4 under the influence of the vehicle is avoided. Fig. 14 The magnetic signature of VW Polo9N per z-axis at minimal detectable velocity and its influence on the FSM’s state changes depending on time interval t34min. The magnetic signature is obtained by scaling the measured one at 20 km/h The value of t34min is equal to the difference between the time interval t23max at the minimum velocity of a vehicle (v=3.6 km/h), and the same interval at the maximum velocity of a vehicle (v=120 km/h). The measurement shows that t34min = 1066.72 ms. Based on the calculated value of the time interval t34, it is obtained that the number of the samples is th34 34 1 140.46N f t=  + = . Vehicle Detector Based on the Magnetic Field Sensor and the Fixed-threshold Algorithm Implemented... 33 The time interval t34 should be greater than the calculated value, therefore the value of the counter Nth34 ≥ 141. The value of t34max can be chosen arbitrarily. However, as this value increases, the FSM will enter State 4 and recalculate the new base value later. The consequence is an increased possibility that more vehicles will pass while the FSM is in State 3, so those vehicles will not be successfully detected. Therefore, the time interval t34 should have the minimum possible value. 5. RESULTS The analysis of the measured values has to finish before the next vehicle is encountered for it to be successfully detected. The time interval between the passages of two consecutive vehicles is unpredictable and can have an arbitrary value. Therefore, the detection algorithm must perform real-time analysis of measured values. Some approximations are introduced to facilitate the design of the algorithm. It includes the length of vehicles is 4 m, and the minimum time interval between the passages of two adjacent vehicles is 500 ms. The length of vehicles is approximated to 4 m because it corresponds to the vehicles used in the testing process. The selected time between the two adjacent vehicles represents the high-density traffic flow. The detection algorithm is based on measured values, which are out of the range determined by Bup and Bdown. The number of these samples depends on the selected Bth. The results in this paper should present the impact of the selected Bth value on the detection algorithm. All described time intervals, except for t21, depend on the threshold value (Figure 15). This dependence can be determined by measurement, as was done in the manner already described for the threshold value of 5 µT. These values were selected arbitrarily to describe the principle of determination of the FSM's parameter. The obtained values of time intervals and the FSM counters for different threshold values are presented in Table 2. Fig. 15 The recorded magnetic signature of VW Polo9N per z-axis and the FSM’s state changes depending on the threshold value. Moving velocity is 20 km/h, and sensor placed on the road 34 M. STOJANOVIĆ, LJ. VRAČAR The value of the time interval t23max decreases if the threshold value increases, while the value of the time interval t31min increases. The value of the time interval t34min also decreases, which can also be determined mathematically as the difference between the time intervals t23max at the lowest and highest vehicle velocity. Table 2 Values of the time intervals/counters of FSM for different values of the magnetic field threshold Bth (µT) t23max (ms)/ Nth23max t31min (ms)/ Nth31min t34min (ms)/ Nth34min 1 40.94/6 17.42/3 1322.88/134 2 40.35/6 34.98/5 1304.60/132 3 39.74/5 51.32/7 1283.94/130 4 39.14/5 69.13/8 1262.94/128 5 38.49/5 87.68/10 1242.86/126 6 37.89/5 105.94/12 1223.48/124 7 37.32/5 124.69/14 1204.51/122 8 36.65/5 144.75/16 1183.68/120 9 35.99/5 165.67/18 1161.65/118 10 35.32/5 185.85/20 1139.84/115 As already mentioned, the time interval t21 corresponds to the time needed for one measurement (one sample), and it does not depend on the threshold value, therefore it is not shown in Table 2. The obtained values of the time intervals and the counters are valid for vehicle detection whose velocities range from 3.6 km/h to 120 km/h. The accuracy of the detector operation was tested using the experimental setup presented in Fig 16. The signal generator creates the voltage waveforms that drive the solenoid to produce a magnetic field identical to the vehicle's magnetic signature. This way of the magnetic signature generation is described in our previous paper [29], and it can reconstruct the signatures with precision above 99%. The second way of creating the testing environment is using the microcontroller instead of the signal generator. The basic concept of that is described in the paper [30]. It provides more advanced testing options, but it is not yet complete. The microcontroller and the magnetic field sensor are parts of the vehicle detector, and results that represent the detection accuracy are stored in the laptop. Fig. 16 The block schematic and the experimental setup for testing the operation of the vehicle detector Vehicle Detector Based on the Magnetic Field Sensor and the Fixed-threshold Algorithm Implemented... 35 The testing process includes the four vehicles, which belong to the city car group, and their characteristics such as length or mass are similar (Table 3). The magnetic signatures of observed vehicles were recorded using the principle described in Section 2. The testing was performed for 12 ranges of velocities when every range includes 1000 vehicles and the time between two adjusted vehicles is from 500 ms up to 1000 ms. Fig. 17 shows the detection accuracy versus the velocity range. Table 3 The characteristics of the vehicles which magnetic signatures were used for testing Vehicle type Length (m) Mass (kg) VW Polo9N 3.743 960 Peugeot 307 4.202 1243 Fiat Grande Punto 4.030 1155 KIA Stonic 4.140 1157 Fig. 17 The detection rate vs. the different range of the vehicle’s velocity The detection rate is above 98 % in slow-moving conditions (4 km/h – 10 km/h) and above 99 % in the most common velocities in the city area (11 km/h – 50 km/h). The high detection rate is also achieved in higher velocities (51 km/h – 100 km/h). The velocities of the high-way are usually above 100 km/h, and the detection rate of the presented detector is above 98 % in the range from 100 km/h to 110 km/h, and above 93 % in the range from 110 km/h to 120 km/h. The reason for less accurate detection in the highest observed velocities is a consequence of the measurement frequency. Namely, the duration of the magnetic signature of the vehicle is shorter at a higher velocity, so the microcontroller has fewer data samples to decide the vehicle's presence. The increasing measurement frequency can provide more data and improve the detection rate at these velocities. A comparison of detection accuracy between other research and our approach is presented in Table 4. The algorithm described in the paper [11] has a detection rate of 99 % over the velocity range from 18 km/h to 108 km/h. The obtained result includes the 36 M. STOJANOVIĆ, LJ. VRAČAR 188 vehicles in the testing process. A high detection rate (98.68 %) is also achieved in the paper [12] with 645 tested vehicles, but this research does not specify the velocity range. Paper [13] presents an algorithm with a 97.5 % detection rate in the range from 32 km/h to 67 km/h, and the detection rate in paper [14] is 99.05 % in the range from 10 km/h to 15 km/h and 100 % in the range from 30 km/h to 40 km/h. The algorithm presented in this paper achieves satisfying precision over a wide range of velocities and provides a high detection rate. The testing process includes more vehicles in our approach, and FSM contains fewer states, which makes its implementation easier. Table 4 Detection accuracy – comparison with other works Paper FSM number of states Detection accuracy (%) Number of vehicles Velocity range (km/h) [11] 5 99 188 18-108 [12] 6 98.68 645 Not specified [13] 5 97.5 81 32-67 [14] Not specified 100 147 30-40 99.05 105 10-15 This paper 4 98.5 1000 4-10 99.2 11-20 100 21-30 99.9 31-40 99.6 41-50 99.3 51-60 99.9 61-70 100 71-100 98.7 101-110 93.2 111-120 As the waveform of a magnetic signature depends on the vehicle’s length, its duration is changed for longer and shorter vehicles. In both cases, the proposed algorithm can be adapted for detection by changing the values of FSM parameters. Another problem can arise if the time interval between two adjacent vehicles is shorter than 500 ms. That affects only the time interval t31min and can be solved by changing the value of Nth31min. 6. CONCLUSION Vehicle detectors based on different methods are used for automated vehicle detection, counting, and classification. This paper describes the vehicle detector based on the magnetic field measurement. It is composed of the magnetic field sensor which records the changes in the magnetic field induced by vehicles, and the microcontroller with embedded detection algorithm, which performs the analysis of the recorded data. The detection algorithm analyzes the magnetic field changes induced by vehicles per one axis perpendicular to the road surface. The detection process is composed of four steps. The magnetic field measurement is the first step. The calculation of the difference between the measured value and the baseline value of the magnetic field is performed in the second step. The third step includes the baseline value updating depending on the detected changes in the magnetic field. The fourth and final step is the FSM, which uses Vehicle Detector Based on the Magnetic Field Sensor and the Fixed-threshold Algorithm Implemented... 37 the calculated difference as the input parameter and gives information about vehicle presence. At a frequency of 100 Hz, the calculated difference is compared with the set fixed-threshold value. The slow rate limiter filter calculates the baseline value. The FSM consists of the four states. The FSM changes state when a defined condition is fulfilled continually during the specified time interval. The values of these time intervals depend on the measurement frequency and the set threshold value. The principle of determination of these interval values is described in detail in the paper. The results obtained by testing the detector using the laboratory equipment show a high detection rate, comparable with the other designs described in the literature. The developed detector is highly accurate over a wide velocity range and based on the simplified FSM composed of four states. Future research will focus on recording magnetic signatures for another vehicle category in different conditions (loaded and unloaded trucks) and upgrading the detection algorithm. The second research direction includes the practical realization of the stand-alone detector. Acknowledgement: This work was supported by the Ministry of Science, Technological Development and Innovation of the Republic of Serbia [grant number 451-03-65/2024-03/200102]. REFERENCES [1] M. Bernas, B. Płaczek, W. Korski, P. Loska, J. Smyła and P. Szymała, "A Survey and Comparison of Low-Cost Sensing Technologies for Road Traffic Monitoring", Sensors, vol. 18, no. 10, p. 3243, 2018. [2] A. N. Oluwatobi, A. O. Tayo, A. T. Oladele and G. R. Adesina, "The design of a vehicle detector and counter system using inductive loop technology", Procedia Computer Science, vol. 183, pp. 493-503, 2021. [3] S. Rajab, M. O. Al Kalaa and H Refai, "Classification and speed estimation of vehicles via tire detection using single-element piezoelectric sensor", J. Adv. Transp., vol. 50, pp. 1366-1385, 2016. [4] A. Arinaldi, J. A. Pradana and A. A. Gurusinga, "Detection and classification of vehicle for traffic video analytics", Procedia Computer Science, vol. 144, pp. 259-268, 2018. [5] S. Mohine, B. S. Bansod, P. Kumar, R. Bhalla and A. Basra, "Single Acoustic Sensor-Based Time– Frequency Spectrum Sensing Approach for Land Vehicle Detection", IEEE Sensors J., vol. 20, no. 13, pp. 7275-7282, 2020. [6] S. V. Marshall, "Vehicle detection using a magnetic field sensor", IEEE Trans. Veh. Technol., vol. 27, no. 2, pp. 65-68, May 1978. [7] L. Klein, M. K. Mills and D. Gibson, Traffic Detector Handbook: Third Edition – Volume I, United States Department of Transportation Federal Highways Administration, 2006. [8] A. Haoui, R. Kavaler and P. Varaiya, "Wireless magnetic sensors for traffic surveillance", Transp. Res Part C: Emerg. Technol., vol. 16, no. 3, pp. 294-306, 2008. [9] M. Hodoň, O. Karpiš, P. Ševčík, A. Kociánová, "Which Digital-Output MEMS Magnetometer Meets the Requirements of Modern Road Traffic Survey?", Sensors, vol. 21, no. 1, p. 266, 2021. [10] V. Markevicius, D. Navikas, A. Daubaras, M. Cepenas, M. Zilys and D. Andriukaitis, "Vehicle Influence on the Earth’s Magnetic Field Changes", Elektronika ir Elektrotechnika, vol. 20, no. 4, pp. 43-48, 2014. [11] S. Taghvaeeyan and R. Rajamani, "Portable Roadside Sensors for Vehicle Counting, Classification, and Speed Measurement", IEEE Trans. Intell. Transp. Syst., vol. 15, no. 1, pp. 73-83, 2014. [12] Z. He, H. Zhu and F. Yu, "A vehicle detection algorithm based on wireless magnetic sensor networks", In Proceedings of the 4th IEEE International Conference on Information Science and Technology, Shenzhen, China, 2014, pp. 727-730. [13] Q. Wang, J. Zheng, H. Xu, B. Xu and R. Chen, "Roadside Magnetic Sensor System for Vehicle Detection in Urban Environments", IEEE Trans. Intell. Transp. Syst., vol. 19, no. 5, pp. 1365-1374, 2018. [14] B. Yang and Y. Lei, "Vehicle Detection and Classification for Low-Speed Congested Traffic With Anisotropic Magnetoresistive Sensor", IEEE Sensors J., vol. 15, no. 2, pp. 1132-1138, 2015. [15] V. Markevicius, D. Navikas, M. Zilys, D. Andriukaitis, A. Valinevicius and M. Cepenas, "Dynamic Vehicle Detection via the Use of Magnetic Field Sensors", Sensors, vol. 16, no. 1, p. 78, 2016. 38 M. STOJANOVIĆ, LJ. VRAČAR [16] Y. Feng, G. Mao, B. Cheng, C. Li, Y. Hui, Z. Xu and J. Chen, "MagMonitor: Vehicle Speed Estimation and Vehicle Classification Through A Magnetic Sensor," IEEE Trans. Intell. Transp. Syst., vol. 23, no. 2, pp. 1311-1322, Feb. 2022. [17] Q. Zhang, X. Li, H. L. Pan, J. T. Wang and Z. J. Zhao, "Detection of vehicle tracks by a three-axis magnetometer", Sens. Actuators A: Phys., vol. 276, pp. 83-90, 2018. [18] S. Vançin and E. Erdem, "Implementation of the vehicle recognition systems using wireless magnetic sensors", Sādhanā, vol. 42, no. 6, pp. 841-854, 2017. [19] J. Lan, Y. Xiang, L. Wang and Y. Shi, "Vehicle detection and classification by measuring and processing magnetic signal", Measurement, vol. 44, pp. 174-180, 2011. [20] C. Xu, Y. Wang, X. Bao and F. Li, "Vehicle Classification Using an Imbalanced Dataset Based on a Single Magnetic Sensor", Sensors, vol. 18, no. 6, p. 1690, 2018. [21] W. Ma, D. Xing, A. McKee, R. Bajwa, C. Flores, B. Fuller and P. Varaiya, "A Wireless Accelerometer- Based Automatic Vehicle Classification Prototype System", IEEE Trans. Intell. Transp. Syst., vol. 15, no. 1, pp. 104-111, 2014. [22] H. Zhu and F. Yu, "A Vehicle Parking Detection Method Based on Correlation of Magnetic Signals", Int. J. Distrib. Sens. Netw., vol. 11. no. 7, 2015. [23] J. A. Vera-Gómez, A. Quesada-Arencibia, C. R. García, R. S. Moreno and F. G. Hernández, "An Intelligent Parking Management System for Urban Areas", Sensors, vol. 16, no. 6, p. 931, 2016. [24] P. Sarcevic, P. Szilveszter and O. Akos, "Real-Time Vehicle Classification System Using a Single Magnetometer", Sensors, vol. 22, no. 23, p. 9299, 2022. [25] M. Rujin, Z. Zhang, Y. Dong and Y. Pan, "Deep Learning Based Vehicle Detection and Classification Methodology Using Strain Sensors under Bridge Deck", Sensors, vol. 20, no. 18, p. 5051, 2020. [26] H. Zhu and F. Yu, "A Cross-Correlation Technique for Vehicle Detections in Wireless Magnetic Sensor Network", IEEE Sensors J., vol. 16, no. 11, pp. 4484-4494, 2016. [27] M. Stojanović, Lj. Vračar, D. Živaljevic and I. Neden Dimitriu, "The practical design of a vehicle magnetic signature generator", Transactions of the Institute of Measurement and Control, vol. 45, no. 8, pp. 1505-1514, 2023. [28] T. Ndjountche, Digital Electronics 3: Finite-state Machines, ISTE, Limited, 2016. [29] ROHM Semiconductor, "3-Axis Digital Magnetometer IC, BM1422AGMV", TSZ02201-0M2M0F916070-1- 2, 17. Oct. 2016. [30] M. Stojanović, J. Vračar and Lj. Vračar, "Magnetic Field Generator for Testing the Operation of Vehicle Detection Sensors", In Proceedings of the 33th IEEE International Conference on Microelectronics (MIEL), 2023, pp. 157-160.