KY-021 Mini magnetic Reed module

From SensorKit X40 Wiki

Jump to: navigation, search

Picture

ky-021.jpg

Technical data / Short description

If the sensor is close to a magnetic field, the input pins are connected.

Pinout

3 G V S.png

Code example Arduino

This example will activate a LED if the sensor is close to a magnetic field.

The modules KY-011, KY-016 or KY-029 can be used as a LED.

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);
  }
  else
  {
    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 

SensorTest_Arduino_withoutPullUP

Code example Raspberry Pi

# Needed modules will be imported and configured
import RPi.GPIO as GPIO
import time
   
GPIO.setmode(GPIO.BCM)
   
# Declaration of the input pin which is connected with the sensor. Additional to that, a pullup resistor will be activated.
GPIO_PIN = 24
GPIO.setup(GPIO_PIN, GPIO.IN)
   
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=outFunction, bouncetime=100) 
   
# main program loop
try:
        while True:
                time.sleep(1)
   
# Scavenging work after the end of the program
except KeyboardInterrupt:
        GPIO.cleanup()

Connections Raspberry Pi:

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

Example program download

KY-021_mini-magnetic-reed_RPI

To start, enter the command:

sudo python SensorTest_RPi_withoutPullUP.py