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)

Connectivity:

KY-024

4 pin connector, pins as follows:

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

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?

KY-003

KY-003

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

KY-024

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.

AnalogDigital
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO_PIR = 24
print "KY-003 Module Test (CTRL-C = exit)"

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

# Tell GPIO library to use GPIO references
GPIO.setmode(GPIO.BCM)

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)

try:
# Loop until users quits with CTRL-C
while True :
    time.sleep(0.1)

    except KeyboardInterrupt:
# Reset GPIO settings
GPIO.cleanup()

PayPal

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

Patreon

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

image/svg+xml

Bitcoin (BTC)

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

SATA HAT

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

0
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)

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

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

0
Plex and RAID1 NAS drive build log

MagPi – auto-downloader

0
Grab each edition of Mag Pi as soon as possible!

HOW TO...

How to boot Raspberry Pi 4 from USB

0
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

0
Getting Raspberry Pi boards connected in seconds

Everything you need to start using JSON

0
It's JSON not Jason, just saying!

How to use Join App in NodeRED

0
The day finally comes where Join app is almost everywhere!

Driving LCD with Arduino and Raspberry Pi

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