Integrasi Arduino
Halaman ini menyediakan contoh Arduino siap pakai untuk mengintegrasikan sensor FG-3+ dan FG-3C dengan board yang kompatibel dengan Arduino (Arduino Uno, Arduino Mega, ESP32, dll.).
FG-3+ — Pengukuran Frekuensi / Periode
FG-3+ menghasilkan 5 V PWM yang periodenya proporsional dengan medan magnet. Contoh di bawah mengukur periode pulsa menggunakan pulseIn(), yang cocok untuk aplikasi dengan laju pembaruan rendah. Untuk akurasi lebih tinggi dan sampling lebih cepat, gunakan hardware timer input-capture sebagai gantinya.
Output FG-3+ adalah logika 5 V. Hubungkan ke MCU 3,3 V (seperti ESP32) hanya melalui pembagi tegangan atau level shifter. Jangan hubungkan sinyal 5 V langsung ke pin GPIO 3,3 V.
// 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
}
Hubungan antara periode (µs) dan medan magnet (nT) tergantung pada unit sensor spesifik. Terapkan koefisien kalibrasi dari sertifikat kalibrasi sensor Anda untuk mengkonversi periode ke nilai medan.
FG-3+ — Akurasi Lebih Tinggi dengan Hardware Timer (ESP32)
Untuk akurasi yang lebih baik pada ESP32, gunakan 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 — Pembacaan UART
FG-3C dalam mode UART mentransmisikan nilai medan ASCII pada 115200 baud pada 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
}
}
}
Pada Arduino Uno (UART hardware tunggal), gunakan pustaka SoftwareSerial pada pin digital cadangan sebagai pengganti Serial1. SoftwareSerial dibatasi pada baud rate yang lebih rendah; untuk 115200 pada Uno, gunakan board dengan UART hardware (Mega, ESP32, Leonardo).
Membaca Beberapa Sensor FG-3+ (3-Axis atau Gradiometer)
Untuk sistem 3-axis atau gradiometer, ukur setiap sensor pada pin terpisah:
// 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
}