The following Arduino sketch will read values from both digital and analog interfaces on the KY-024. The digital interface will turn on the Arduino's LED when a magnetic field is detected.
The analog interface starts at an initial value determined by the input voltage and the potentiometer, this value will increase or decrease depending on the intensity and polarity of the magnetic field.
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);
}