Вернуться на страницу библиотеки модулей

int led = 13;          // on-board LED
int digitalPin = 3;    // KY-025 D0 (digital output)
int analogPin = A0;    // KY-025 A0 (analog output)

const unsigned long debounceMs = 30;  // ignore contact bounce shorter than this
int reedState   = LOW;                // current debounced state
int lastReading = LOW;
unsigned long lastChange = 0;

void setup()
{
  pinMode(led, OUTPUT);
  pinMode(digitalPin, INPUT);
  Serial.begin(9600);
}

void loop()
{
  // ---- Digital output D0 (debounced) ----
  int reading = digitalRead(digitalPin);  // HIGH when the reed switch closes
  if (reading != lastReading) {
    lastReading = reading;
    lastChange = millis();
  }
  if (millis() - lastChange > debounceMs && reading != reedState) {
    reedState = reading;
    digitalWrite(led, reedState);         // on-board LED mirrors the stable reed state
  }

  // ---- Analog output A0 ----
  // The reed is on/off, so A0 swings between two levels (inverted: lower value = magnet near).
  Serial.println(analogRead(analogPin));  // Tools > Serial Plotter to visualise

  delay(50);
}

Рекомендуемые ссылки:
Arduino KY-025 Reed Switch Module (Analog + Digital)
KY-025 Analog, magnetic reed sensor – SensorKit
Using the Reed Switch Module KY-025 with Arduino – Phipps Electronics
What is a reed switch and how do the A3144 and KY-025 work?
ESP32 KY-025 Reed Switch Module Pinout & Datasheet
Датчик магнитного поля (геркон) KY-025 и Arduno Uno: Схема подключения и пример кода – блог

Вернуться на страницу библиотеки модулей