Home Raspberry Pi Sensors Hall sensor module KY-003 & KY-024 for Raspberry Pi and Arduino

Hall sensor module KY-003 & KY-024 for Raspberry Pi and Arduino

Small magnetic sensor that can be used as a switch or a detector.

Tech details:

  • Operation voltage 3.3 V, 5 V 
  • Temperature range of -40 °C to 85 °C
  • Current input/output max  8/20mA
  • Response time: 3uS   (very fast)



4 pin connector, pins as follows:

  • AO: Analog Output
  • G: 0V Ground
  • +: 3.3V – 5V Supply
  • DO: Digital Output

3 pin connector, pins as follows:

  • GND (-): Ground
  • VCC (+): 3.3V – 5V Supply
  • S: Signal

Both versions of the sensor can be connected directly to GPIO pins.

What does it do?

Hall sensor module is a very responsive switch that is activated by a magnetic field. It can be triggered by a natural magnet or an electromagnet.  High response rates allow readouts to be taken even at frequencies of 100kHz. Most popular uses are in proximity sensing detecting speeds of spinning objects or as door sensors. Due to nature of the sensing (contactless)  hall sensor module is very durable, and there is no wear associated with the use. Sensors come usually as non-latching however the latching versions are possible (sensor keeps the state after magnet has been removed until opposite polarity is introduced).

How to use it?



This is an analog sensor module. The sensor goes HIGH on the  S pin when a magnetic field is detected.  The PCB contains a LED light that indicates when the sensor detects a field and a resistor to lower the current supplied to the board. It is possible to detect voltage changes, that occur when the magnetic field is applied.  The value of the voltage on S pin would depend on a strength of a magnet and the distance from the sensor.

Buy on Aliexpress.com


This is an analog/digital module with a built-in potentiometer to adjust the sensitivity of the sensor. Analog output (AO) works in the same way as on the KY – 003.  Digital output (DO) allows for a pure HIGH/LOW detection and adjusting the sensitivity of that detection with a potentiometer.

In both cases the GND(-) connects to any ground pin (black) on a Raspberry Pi, VCC(+) power supply (red) connects to the 3.3V on RPI and the signal (S or AO/DO) is linked to any GPIO pin (blue).

When using Hall sensor modules have to be mounted in a correct orientation. One side will respond to the N pole of the sensor while the other side will respond to the S pole.

Buy on Aliexpress.com

Sample code:

You can test the Hall sensor using a python2 code from here.

import RPi.GPIO as GPIO

print "KY-003 Module Test (CTRL-C = exit)"

GPIO.setup(GPIO_PIR, GPIO.IN, pull_up_down = GPIO.PUD_UP)
def printFunction(channel):
    GPIO.add_event_detect(GPIO_PIR, GPIO.RISING, callback=printFunction)
    while True :
        Current_State = GPIO.input(GPIO_PIR)
        except KeyboardInterrupt:
# Import required libraries
import RPi.GPIO as GPIO
import time
import datetime

# Tell GPIO library to use GPIO references

print "Setup GPIO pin as input"

# Set Switch GPIO as input
GPIO.setup(17 , GPIO.IN)

def sensorCallback1(channel):
# Called if sensor output goes LOW
    timestamp = time.time()
    stamp = datetime.datetime.fromtimestamp(timestamp).strftime('%H:%M:%S')
    print "Sensor LOW " + stamp

def sensorCallback2(channel):
# Called if sensor output goes HIGH
    timestamp = time.time()
    stamp = datetime.datetime.fromtimestamp(timestamp).strftime('%H:%M:%S')
    print "Sensor HIGH " + stamp

def main():
# Wrap main content in a try block so we can
# catch the user pressing CTRL-C and run the
# GPIO cleanup function. This will also prevent
# the user seeing lots of unnecessary error
# messages.

    GPIO.add_event_detect(17, GPIO.FALLING, callback=sensorCallback1)
    GPIO.add_event_detect(17, GPIO.RISING, callback=sensorCallback2)

# Loop until users quits with CTRL-C
while True :

    except KeyboardInterrupt:
# Reset GPIO settings


Nothing says "Thank you" better than keeping my coffee jar topped up!


Support me on Patreon and get an early access to tutorial files and videos.


Bitcoin (BTC)

Use this QR to keep me caffeinated in style with crypto-currency


Add an SSD to Raspberry Pi 4 and turn it into a super-cooled NAS drive.

More HATs

Argon One

Enclose Raspberry Pi 4 inside this great case with custom I/O, cooling and GPIO

More cases on

Best Raspberry Pi Projects

How to use Raspberry PI as WOL (wake on lan) server

While you could wake up your PC from a mobile directly, having a dedicated server capable of doing so is the best solution. The reason is simple. You can hook up as many devices as you wish with a single endpoint. This is why Raspberry Pi is perfect for this.

Free wireless doorbell (Amazon Dash button hack)

The most terrifying situation I can imagine* is missing a parcel

Raspberry Pi NAS: (NAS-pi) Modding like a boss #part2

Plex and RAID1 NAS drive build log

MagPi – auto-downloader

Grab each edition of Mag Pi as soon as possible!


How to boot Raspberry Pi 4 from USB

How to set up and boot Raspberry Pi 4 from USB drive - headless guide.

Raspberry Pi Zero, Raspberry Pi 3A+ WiFi without keyboard or cables

Getting Raspberry Pi boards connected in seconds

Everything you need to start using JSON

It's JSON not Jason, just saying!

How to use Join App in NodeRED

The day finally comes where Join app is almost everywhere!

Driving LCD with Arduino and Raspberry Pi

Displaying things on an LCD screen is as easy as going through a cup of jellybeans while writing this tutorial!