Arduino: How to use KY-024 Linear Magnetic Hall Module

Arduino Tutorial: KY-024 Linear Magnetic Hall Module

Abstract

Learn how to use the KY-024 Linear Magnetic Hall Module with Arduino. This versatile module is similar to the KY-025 but explicitly uses a Linear Hall Effect Sensor, providing both a raw analog voltage (A0) and a digital ON/OFF signal (D0) based on the strength and polarity of a magnetic field. This tutorial focuses on reading both outputs and calibrating the digital trigger using the onboard potentiometer.

1. Introduction

The KY-024 employs a ratiometric linear Hall sensor, meaning the output voltage is a continuous function of the magnetic field strength passing through it.

  1. Analog Output (A0): Provides a voltage proportional to the magnetic field strength. The voltage is typically near VCC/2 (e.g., 2.5V, or ≈512 raw ADC) when no magnet is present, increasing or decreasing as the North or South pole is brought near.
  2. Digital Output (D0): An onboard comparator uses the analog signal to provide a simple, adjustable HIGH/LOW switch. The threshold for this switch is set by the potentiometer.

In this episode, you’ll learn:

  • The principle of linear Hall sensing (voltage proportional to field).
  • How to use the analog output (A0) to measure proximity and polarity.
  • How to use the digital output (D0) for a simple magnetic switch.
  • How to use the onboard potentiometer to set the digital magnetic threshold.

This project uses magnetic fields to demonstrate both continuous (analog) and binary (digital) sensing.

2. Prerequisites

Make sure you have:

  • An Arduino Uno or compatible board.
  • One KY-024 Linear Magnetic Hall Module.
  • One small magnet (e.g., neodymium magnet).
  • Jumper Wires.
  • Arduino IDE

3. Wiring and Setup for Arduino

The KY-024 module has four pins and requires both digital and analog input pins.

Step 1 – Identify Pins

The module typically has four pins: A0 (Analog Output), D0 (Digital Output), VCC (+), and GND (− or GND).

Step 2 – Connect the Module

Wire the module to the Arduino as follows:

  • Connect the GND pin of the KY-024 to the GND pin on the Arduino.
  • Connect the VCC pin of the KY-024 to the 5V pin on the Arduino.
  • Connect the D0 pin of the KY-024 to Arduino Digital Pin 7 (Digital Input).
  • Connect the A0 pin of the KY-024 to Arduino Analog Pin A0 (Analog Input).

This image was created with Fritzing

Step 3 – Initialize Pin Modes

pinMode(7, INPUT); // D0 pin for digital ON/OFF detection

We will use the on-board LED (Pin 13) to reflect the digital trigger state.

4. Writing Dual-Mode Magnetic Sensing Code

We will continuously monitor the analog value to determine the field strength and polarity, and use the digital output for a simple trigger.

Open main.ino and implement the following code.

				
					const int DIGITAL_PIN = 7; // D0 Output
const int ANALOG_PIN = A0; // A0 Output
const int LED_PIN = 13;    // Built-in LED
const int CENTER_VALUE = 512; // Baseline ADC value (no field)

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(DIGITAL_PIN, INPUT);
  Serial.begin(9600);
  Serial.println("KY-024 Linear Hall Sensor Ready! Calibrate Digital Threshold.");
}

void loop() {
  // 1. Read Analog State (A0)
  int analogValue = analogRead(ANALOG_PIN);
  int deviation = analogValue - CENTER_VALUE; // Deviation from the center (512)

  // 2. Read Digital State (D0)
  // Digital state is set by the potentiometer threshold.
  // We assume LOW means the magnetic field has exceeded the set threshold.
  int digitalState = digitalRead(DIGITAL_PIN);

  // Control LED based on Digital Output (Action Trigger)
  if (digitalState == LOW) {
    digitalWrite(LED_PIN, HIGH); // Turn LED ON (Magnetic Field Exceeded Threshold)
  } else {
    digitalWrite(LED_PIN, LOW);  // Turn LED OFF
  }

  // Print both outputs to the Serial Monitor
  Serial.print("Analog (A0): ");
  Serial.print(analogValue);
  Serial.print(" | Deviation: ");
  Serial.print(deviation);
  Serial.print(" | Digital State (D0): ");
 
  if (digitalState == LOW) {
    Serial.println("TRIGGERED (LOW)");
  } else {
    Serial.println("CLEAR (HIGH)");
  }
 
  delay(100);
}

				
			

Code Explanation

  • Analog Reading: The analogValue should hover around 512 with no magnet. A positive deviation (e.g., 700) indicates one pole, and a negative deviation (e.g., 300) indicates the opposite pole.
  • D0 Output: The digital pin switches state when the A0 reading crosses the threshold set by the potentiometer. This threshold can be set for a specific magnetic strength regardless of polarity (if the threshold is set close to 512) or for a specific pole (if the threshold is set far from 512).

5. Adjusting Sensitivity (Potentiometer)

The potentiometer controls the digital switching point for the D0 pin:

  1. Upload the code and open the Serial Monitor.
  2. Note the Analog (A0) value (center baseline) with no magnet.
  3. Slowly turn the potentiometer and watch the Digital State (D0).
  • If the D0 state switches when the magnet is far away, the pot is set near the 512 center point, making it highly sensitive.
  • If the D0 state switches when the magnet is very close, the pot is set far from the 512 center point, making it less sensitive.
  1. Calibrate: Set the pot so the D0 pin only switches when a magnet is within your desired distance.

6. Uploading and Running the Project

Step 1 – Build & Upload

Complete the standard build and upload process.

Step 2 – Test

  1. Calibrate the digital threshold using the potentiometer (Section 5).
  2. No Magnet: The Arduino LED (Pin 13) should be OFF.
  3. Bring Magnet Closer: As the magnet approaches, the A0 reading will deviate (e.g., to 300 or 700).
  4. Once the magnetic field reaches the strength set by the potentiometer, the LED should turn ON and the Serial Monitor should show TRIGGERED.

7. Hands-On Lab Recap

You’ve learned:

  • The linear output behavior of a Hall sensor (proportional voltage).
  • How to use the analog deviation from the center point (512) to detect polarity.
  • The crucial skill of calibrating the digital threshold with the onboard potentiometer.

This concludes the comprehensive series on fundamental KY-series modules.

8. Common Issues & Fixes

Issue Cause Solution
Analog reading is fixed at 512 and never moves. Magnet is too weak or too far away. Use a stronger magnet (neodymium is best) and place it within 1cm of the sensor.
Digital LED is always ON or never turns ON. Potentiometer is mis-calibrated. Calibrate the sensor carefully (Section 5). The pot must be tuned to separate ambient readings from the magnetic field readings.
Analog reading fluctuates randomly near 512. Noise in the power supply or ambient magnetic noise. This is normal; the sensor is highly sensitive. Focus on the sharp change caused by the external magnet.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top