Zum Hauptinhalt springen

Arduino Integration

This page provides ready-to-use Arduino examples for integrating FG-3+ and FG-3C sensors with Arduino-compatible boards (Arduino Uno, Arduino Mega, ESP32, etc.).


FG-3+ — Frequency / Period Measurement

The FG-3+ outputs a 5 V rectangular pulse whose period is proportional to the magnetic field. The example below measures the pulse period using pulseIn(), which is suitable for low-update-rate applications. For higher accuracy and faster sampling, use hardware timer input-capture instead.

Level Shifting

The FG-3+ output is 5 V logic. Connect to a 3.3 V MCU (such as ESP32) only through a voltage divider or level shifter. Do not connect a 5 V signal directly to a 3.3 V GPIO pin.

// FG-3+ Frequency Measurement Example
// Reads the pulse period from an FG-3+ sensor and prints it to Serial.
//
// Wiring:
// FG-3+ VCC -> 5V
// FG-3+ GND -> GND
// FG-3+ OUT -> Pin 2 (via level shifter for 3.3V boards)
//
// Note: pulseIn() blocks during measurement. Use hardware timers
// for time-critical applications.

const int SENSOR_PIN = 2; // Digital input pin connected to FG-3+ OUT

void setup() {
Serial.begin(115200);
pinMode(SENSOR_PIN, INPUT);
Serial.println("FG-3+ Period Measurement");
Serial.println("Period_us");
}

void loop() {
// Measure the period of one complete pulse (HIGH + LOW duration)
// pulseIn returns duration in microseconds; 0 if timeout
unsigned long highTime = pulseIn(SENSOR_PIN, HIGH, 100000UL); // 100 ms timeout
unsigned long lowTime = pulseIn(SENSOR_PIN, LOW, 100000UL);

if (highTime == 0 || lowTime == 0) {
Serial.println("Timeout - no signal detected");
return;
}

unsigned long periodUs = highTime + lowTime;

// Print the period in microseconds
// To convert to field value (nT), apply your sensor's calibration factor
Serial.println(periodUs);

delay(100); // 10 Hz output rate
}
Calibration

The relationship between period (µs) and magnetic field (nT) depends on the specific sensor unit. Apply the calibration coefficients from your sensor's calibration certificate to convert period to field value.


FG-3+ — Higher Accuracy with Hardware Timer (ESP32)

For better accuracy on an ESP32, use the hardware timer input capture:

// FG-3+ High-Accuracy Period Measurement using ESP32 PCNT
// Uses the Pulse Counter peripheral for precise frequency counting.
//
// Wiring:
// FG-3+ OUT -> GPIO 4 (via level shifter)

#include "driver/pcnt.h"

#define PCNT_UNIT PCNT_UNIT_0
#define PCNT_INPUT GPIO_NUM_4
#define COUNT_TIME_MS 1000 // Count pulses for 1 second

void setup() {
Serial.begin(115200);

// Configure PCNT to count rising edges on the input pin
pcnt_config_t pcnt_config = {
.pulse_gpio_num = PCNT_INPUT,
.ctrl_gpio_num = PCNT_PIN_NOT_USED,
.lctrl_mode = PCNT_MODE_KEEP,
.hctrl_mode = PCNT_MODE_KEEP,
.pos_mode = PCNT_COUNT_INC, // Count rising edges
.neg_mode = PCNT_COUNT_DIS,
.counter_h_lim = 30000,
.counter_l_lim = 0,
.unit = PCNT_UNIT,
.channel = PCNT_CHANNEL_0,
};

pcnt_unit_config(&pcnt_config);
pcnt_counter_pause(PCNT_UNIT);
pcnt_counter_clear(PCNT_UNIT);
pcnt_counter_resume(PCNT_UNIT);

Serial.println("Frequency_Hz");
}

void loop() {
int16_t count = 0;
pcnt_counter_clear(PCNT_UNIT);
delay(COUNT_TIME_MS);
pcnt_get_counter_value(PCNT_UNIT, &count);

// count = pulses per second = frequency in Hz
Serial.println(count);
}

FG-3C — UART Reading

The FG-3C in UART mode transmits ASCII field values at 115200 baud on Pin 4.

// FG-3C UART Reading Example
// Reads magnetic field values from FG-3C sensor via hardware UART.
//
// Wiring (Arduino Mega or ESP32):
// FG-3C Pin 1 (VCC) -> 5V
// FG-3C Pin 2 (GND) -> GND
// FG-3C Pin 4 (TX) -> Serial1 RX (via level shifter for 3.3V boards)
//
// On Arduino Uno (no hardware Serial1): use SoftwareSerial on pins 10, 11.
// On ESP32: use Serial2 or any available UART.

// For ESP32: Serial2 RX = GPIO16, TX = GPIO17
// For Arduino Mega: Serial1 RX = Pin 19

void setup() {
Serial.begin(115200); // USB serial — for printing to PC
Serial1.begin(115200); // Hardware UART — connected to FG-3C Pin 4

Serial.println("FG-3C UART Reader");
Serial.println("Field_nT");
}

void loop() {
// Read one line from the FG-3C (terminated with newline)
if (Serial1.available()) {
String line = Serial1.readStringUntil('\n');
line.trim();

if (line.length() > 0) {
float fieldNT = line.toFloat();

// Print to USB serial for monitoring
Serial.println(fieldNT, 1); // 1 decimal place
}
}
}
hinweis

On Arduino Uno (single hardware UART), use the SoftwareSerial library on spare digital pins instead of Serial1. SoftwareSerial is limited to lower reliable baud rates; for 115200 on Uno, use a board with hardware UART (Mega, ESP32, Leonardo).


Reading Multiple FG-3+ Sensors (3-Axis or Gradiometer)

For a 3-axis system or gradiometer, measure each sensor on a separate pin:

// Multi-sensor FG-3+ Period Measurement
// Reads 3 sensors on pins 2, 3, 4 (e.g., for a 3-axis sensor assembly)
//
// Wiring:
// Sensor X OUT -> Pin 2
// Sensor Y OUT -> Pin 3
// Sensor Z OUT -> Pin 4
// All via level shifters for 3.3V boards

const int PINS[3] = {2, 3, 4};
const char* AXIS[3] = {"X", "Y", "Z"};

void setup() {
Serial.begin(115200);
for (int i = 0; i < 3; i++) {
pinMode(PINS[i], INPUT);
}
Serial.println("Axis,Period_us");
}

void loop() {
for (int i = 0; i < 3; i++) {
unsigned long highTime = pulseIn(PINS[i], HIGH, 100000UL);
unsigned long lowTime = pulseIn(PINS[i], LOW, 100000UL);

if (highTime > 0 && lowTime > 0) {
unsigned long period = highTime + lowTime;
Serial.print(AXIS[i]);
Serial.print(",");
Serial.println(period);
}
}

delay(200); // ~5 Hz per axis
}