HomeRaspberry Pi SensorsHall 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 with BTC: 1FwFqqh71mUTENcRe9q4s9AWFgoc8BA9ZU

M5Paper

Programable, ESP32 based awesome dev platform with 4.7 e-ink display by M5Stack

More HATs

client-image
client-image

Argon One M.2

Enclose Raspberry Pi 4 inside this great case with custom I/O, cooling and GPIO and M.2 SSD support

More cases on

client-image
client-image

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.

Slow Internet Warning

0
From time to time my Internet grinds to a stop. Since Raspberry Pi 4 comes with a 1Gbps Ethernet, I decided to take advantage of it and create a reporting system in NodeRED that will monitor and report when the ISP is not keeping the contractual agreements. Works with Alexa, Google Home, Android and Windows 10.

How fast Raspberry Pi NAS is?

0
Let's see how fast Raspberry Pi NAS really is?

Argon18: Argon ONE SSD modification

0
Argon One case just got better - now you can boot it from USB without ruining the design thanks to Argon 18: Argon One SSD modification

HOW TO...

How to boot Raspberry Pi 5 from NVMe M.2 SSD

0
This is how you can boot your Raspberry PI 5 from NVMe in 10 min! Check out these easy instructions

It took me 2 months to boot CM4 from NVMe

0
Complete beginners guide to Compute Module 4 boot from NVMe.

Raspberry Pi Zero 2 W vs other Zero boards

0
It's time to test the Raspberry Pi Zero 2 W against other Raspberry Pi boards from Zero series: power, WiFi, temperature and core performance

C/C++ and MicroPython SDK for Raspberry Pi Pico on Windows

0
A guide to SDK toolchain for Raspberry Pi Pico and C/C++ , Micropython on Windows.

A comprehensive guide to Grafana & InfluxDB

0
How to use Grafana and InfluxDB on Raspberry Pi for IoT sensors in home automation