If a magnetic field is detected, the two input pins are short-circuited by pulling them towards each other.

Technical dara

Operating voltage 3,3 V - 5 V
Dimensions 19 x 18,5 x 9 mm

Pin assignment

Code example Arduino

Pin assignment Arduino

Arduino Sensor
Pin 10 signal
5 V +V
ground GND
Arduino Sensor
Pin 13 LED+

This is an example program which lights up a LED when a signal is detected at the sensor. The modules KY-011, KY-016 or KY-029 can also be used as LEDs, for example.

int Led = 13 ;// declaration of the LED output pin
int Sensor = 10 ;// Declaration of the sensor input pin
int val; // Temporary variable
void setup ()
  pinMode (Led, OUTPUT) ; // Initialize output pin
  pinMode (Sensor, INPUT) ; // Initialize sensor pin
void loop ()
  val = digitalRead (Sensor) ; // The current signal at the sensor is read out
  if (val == HIGH) // If a signal could be detected, the LED is switched on.
    digitalWrite (Led, LOW);
    digitalWrite (Led, HIGH);

Example program download


Code example Raspberry Pi

Pin assignment Raspberry Pi

Raspberry Pi Sensor
GPIO 24 [Pin 18] Signal
3.3 V [Pin 1] +V
Ground [Pin 6] GND

This is an example program where a console output is generated as soon as a signal is detected.

# Required modules are imported and set up
import RPi.GPIO as GPIO
import time
# Here the input pin is declared, to which the sensor is connected. Additionally the PullUP resistor at the input will be activated
print("Sensor test [press CTRL+C to end test]")
# This outputFunction will be executed on signal detection
def outputFunction(null):
        print("Signal detected")
# When a signal is detected (falling signal edge) the output function is executed
GPIO.add_event_detect(GPIO_PIN, GPIO.FALLING, callback=outputFunction, bouncetime=100) 
# main program loop
    while True:
# clean up after the program is finished
except KeyboardInterrupt:

Sample program download


To start with the command:

sudo python3 KY021-RPi.py

Code example Micro:Bit

Pinout Micro:Bit:

Micro:Bit Sensor
Pin 1 Signal
3 V +V
Ground GND

Sample program download