In this Arduino sketch we'll read values from both digital and analog interfaces on the KY-025, you'll need a magnet to interact with the module.
The digital interface will send a HIGH signal when a magnetic field is detected, turning on the LED on the Arduino (pin 13).
On the other hand, the analog interface will return a HIGH numeric value when there's no magnetic field present and it'll drop to zero when a magnet is near.
int led = 13;int digitalPin = 3;int analogPin = A0;int digitalVal;int analogVal;
void setup()
{
pinMode(led, OUTPUT);
pinMode(digitalPin, INPUT);
Serial.begin(9600);
}
void loop()
{
digitalVal = digitalRead(digitalPin);
if(digitalVal == HIGH) {
digitalWrite(led, HIGH); }
else
{
digitalWrite(led, LOW); }
analogVal = analogRead(analogPin);
Serial.println(analogVal);
delay(100);
}