Fluxgate Magnetometers are highly sensitive sensors designed to detect minute magnetic field variations, commonly used in geophysical surveying and magnetic anomaly detection. They can detect magnetic fields generated by vehicles, making them ideal for traffic detection and counting applications.
FG-3+ Sensor by FG Sensors offers exceptional sensitivity and low noise performance. They are perfect for detecting magnetic anomalies from hidden objects and vehicles, the FG-3+ provides reliable and accurate magnetic field measurements for traffic monitoring.
More details on FG sensors and fluxgate technology can be found at fgsensors.com.
Goal: Detect vehicles by sensing their magnetic disturbances using the FG-3+ near roadways.
Platform: Arduino UNO R4 WiFi for sensor interfacing and optional cloud connectivity.
Functionality: Detect vehicle, with possibility to upgrade system to log detection events with timestamps and send data wirelessly to Arduino Cloud.
Hardware SetupFG-3+ magnetometer positioned close to the road to effectively sense magnetic disturbances from passing cars, trucks, or motorcycles.
Connect the FG-3+ sensor to Arduino UNO R4 WiFi.
- FG-3+ VCC - 5V
- FG-3+ GND – GND
- FG-3+ OUT – connected to DIGITAL pin 2
- Sensor Reading: Measures FG-3+ sensor output frequency, which varies proportionally with the magnetic field from 40 kHz to 120 kHz, using a frequency counter.
- Signal Filtering: Applies a smoothing algorithm to eliminate false readings and reduce EMI-induced disturbances.
- Vehicle Detection: Identifies sudden, significant changes in magnetic field strength corresponding to a passing vehicle.
- Visual Feedback: Triggers a car animation on the matrix display each time a vehicle is detected.
1. Sensor Signal Counting
The digital sensor output (frequency signal proportional to magnetic field) is connected to an interrupt pin. Rising edges trigger the ISR sensorDigHandler(), which increments a counter sensorDigCnt when counting is enabled (intEnable flag). This counter accumulates sensor pulses during a fixed measurement window (measureTime ms).
2. Baseline Establishment and Moving Average
The baseline frequency count (representing no vehicle traffic) is initialized at startup by measuring sensor pulses without disturbance. During normal operation, a moving average baseline is continuously updated with factor alpha smoothing to adapt slowly to slow environmental changes.
3. Peak Detection
The main logic checks if the instantaneous sensor counts deviates from the baseline by a threshold (base_line_change). If yes, it enters a peak event state, tracking how long the deviation lasts (peakSamplesCount) and its maximum magnitude (peakValue). When the deviation drops below the threshold or exceeds sample count (N), the peak event ends.
4. Vehicle Detection and Animation Trigger
When a peak event ends, it is treated as a detected vehicle event. The car animation on the matrix display is triggered to visually signal the vehicle passage.
5. Matrix Animation
The animation function playCarAnimation() simulates a car bitmap moving across the LED matrix from right to left with a blocking delay controlling frame speed.
Parameter Tuning(Parameter / Purpose / How to Tune)
(Parameter / Purpose / How to Tune)updateRate / How often sensor data is processed (ms) / Lower for faster response but higher CPU usage (100ms is good starting point)
measureTime / Duration during which pulses are counted (ms) / Should be less than updateRate; affects resolution of frequency measurement
alpha / Smoothing factor for moving average baseline / Lower (0.05-0.1) = slower baseline adjustment, more stable; higher = faster adaptation
base_line_change / Threshold to detect significant deviation (counts) / Tune empirically based on noise and vehicle signal amplitude, start with 10
N / Max duration of peak event in samples / Set to cover expected vehicle presence duration; too small might cut event early
- To tune, start by observing the sensor values and noise under normal conditions.
- Adjust base_line_change so false positives are minimized.
- Adjust alpha so baseline smoothly tracks slow changes but ignores brief spikes.
- Increase N if vehicle signal is longer or noisier.
To test in the lab, simply move your keys or a magnet near the sensor—this creates a large enough magnetic disturbance to simulate a vehicle passing by, allowing quick verification of detection functionality.
Notice: Fluxgate magnetometers are extremely sensitive sensors that can detect very small magnetic field changes and are also affected by electromagnetic interference (EMI) in the lab – this can introduce falls triggers.
Optional EnhancementsAdding Non-Blocking Animation (Improvement Suggestion)
To avoid blocking sensor processing during animation, change the animation to run incrementally within the loop() using a state machine and millis() for timing:
- Store animation state variables: current x-position and last frame time.
- On peak detection, set a flag to start animation.
- Each loop() iteration, check if animation flag is set and enough time elapsed since last frame.
- Update car position, redraw frame, and clear when animation completes.
- Meanwhile, sensor counting and peak detection continue running concurrently.
This approach allows vehicle detection to keep running without missing interrupts or updates due to delay() blocking.
Improving detection and logging algorithm- Optimize detection parameters: prevent false readings
- Counting & Logging: Increment vehicle counts per detection event. Timestamp each event for historical data.
- Use AI signal processing to identify different vehicle types
- Use timestamps combined with geographic data (GPS) for spatial traffic analytics.
- Cloud Upload: Send real-time counts, logs, and sensor data to Arduino Cloud for remote access and visualization.
- Alerts & Analytics: Set thresholds for abnormal traffic flow or unusual magnetic signatures for notifications.
- Analyse long-term traffic patterns remotely through Arduino Cloud dashboards.
- Low-Cost & Easy to Deploy: Uses affordable, off-the-shelf components.
- Highly Sensitive Detection: FG-3+ fluxgate magnetometer’s precision detects subtle magnetic changes from vehicles.
- Scalable: Multiple units can create a wide-area traffic monitoring network.
- Wireless & Cloud-Connected: No need for wired infrastructure, with real-time data available anywhere .
This project shows the real-world applicability of fluxgate magnetometer technology, especially the FG-3+ sensor from FG Sensors, in modern smart city solutions like traffic monitoring using Arduino’s versatile and connected platform.
Comments