// ───────────────────────────────────────────────────────────────────────────── // A3144 Hall Effect Sensor — Serial Monitor Test // XIAO ESP32-S3 // // A3144 VCC → 5V // A3144 GND → GND // A3144 OUT → GPIO8 (D9) + 10kΩ pullup to 3.3V // ───────────────────────────────────────────────────────────────────────────── #define HALL_PIN 8 // GPIO8 — digital input #define DEBOUNCE_MS 5 // ignore re-triggers within 5ms volatile bool pulseFlag = false; volatile uint32_t lastPulseUs = 0; unsigned long lastPrintTime = 0; unsigned long lastPulseTime = 0; int pulseCount = 0; float rpm = 0.0; // ── Interrupt — fires on each magnet passing ────────────────────────────────── void IRAM_ATTR onPulse() { uint32_t now = micros(); if (now - lastPulseUs > (DEBOUNCE_MS * 1000)) { lastPulseUs = now; pulseFlag = true; } } // ───────────────────────────────────────────────────────────────────────────── void setup() { Serial.begin(115200); pinMode(HALL_PIN, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(HALL_PIN), onPulse, FALLING); delay(500); Serial.println("A3144 Hall Effect Sensor Test"); Serial.println("─────────────────────────────"); Serial.println("Pulses/sec | RPM (10 magnets) | Status"); } void loop() { unsigned long now = millis(); // Handle pulse flag from ISR if (pulseFlag) { pulseFlag = false; pulseCount++; lastPulseTime = now; } // Print every 500ms if (now - lastPrintTime >= 500) { lastPrintTime = now; // RPM = (pulses per second / magnets) * 60 float pulsesPerSec = pulseCount * 2.0; // 500ms window × 2 rpm = (pulsesPerSec / 10.0) * 60.0; // 10 magnets per revolution // Dial status bool dialMoving = (now - lastPulseTime) < 300; // 300ms timeout String status = dialMoving ? "MOVING" : "STILL"; Serial.print("Pulses/s: "); Serial.print(pulsesPerSec, 1); Serial.print(" \tRPM: "); Serial.print(rpm, 1); Serial.print(" \tStatus: "); Serial.println(status); pulseCount = 0; // reset count for next window } }