KY-021 Mini magnetic Reed module

From SensorKit X40 Wiki

Revision as of 19:06, 20 February 2017 by Webmaster Wiki (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search



Technical data / Short description

If a magnetic field will be detected, the input pins will be shorted.



Code example Arduino

This example will light up a LED after a sensor detected a signal.

The modules KY-011, KY-016 or KY-029 could be used as LED too 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) ; // Initialization output pin
  pinMode (Sensor, INPUT) ; // Initialization sensor pin
void loop ()
  val = digitalRead (Sensor) ; // The current signal at the sensor will be read
  if (val == HIGH) // If a signal will be detected, the LED will light up.
    digitalWrite (Led, LOW);
    digitalWrite (Led, HIGH);

Connections Arduino:

LED + = [Pin 13]
LED - = [Pin GND]
Sensor Signal = [Pin 10]
Sensor +V = [Pin 5V]
Sensor - = [Pin GND]

Example program download

Code example Raspberry Pi

Program example in the language python.

# Needed modules will be imported and configured
import RPi.GPIO as GPIO
import time
# Declaration of the input pin which is connected with the sensor. Additional to that, a pullup resistor will be activated.
print "Sensor-test [press ctrl+c to end]"
# This outFunction will be started after a signal was detected.
def outFunction(null):
        print("Signal detected")
# The outFunction will be started after a signal (falling signal edge) was detected.
GPIO.add_event_detect(GPIO_PIN, GPIO.FALLING, callback=ausgabeFunktion, bouncetime=100) 
# main program loop
        while True:
# Scavenging work after the end of the program
except KeyboardInterrupt:

Connections Raspberry Pi:

Signal = GPIO24 [Pin 18]
+V = 3,3V [Pin 1]
GND = GND [Pin 6]

Example program download

To start use the command:

sudo python