The Modulo Mini Interruptor Magnetico (KY21), manufactured by Keys, is a compact magnetic switch module designed to detect the presence of a magnetic field. This module is widely used in security systems, such as door and window sensors, to detect when a door or window is opened or closed. It is also suitable for other applications requiring magnetic field detection, such as proximity sensing and automation systems.
The following table outlines the key technical details of the KY21 module:
Parameter | Value |
---|---|
Operating Voltage | 3.3V to 5V |
Operating Current | < 5mA |
Output Type | Digital (High/Low) |
Detection Range | 10mm to 20mm (depending on magnet strength) |
Dimensions | 32mm x 14mm x 10mm |
Operating Temperature | -20°C to 70°C |
The KY21 module has three pins, as described in the table below:
Pin | Name | Description |
---|---|---|
1 | VCC | Power supply input (3.3V to 5V) |
2 | GND | Ground connection |
3 | OUT | Digital output pin (HIGH when no magnet is detected, LOW when a magnet is detected) |
VCC
pin to a 3.3V or 5V power source and the GND
pin to the ground of your circuit.OUT
pin to a digital input pin of your microcontroller or to another circuit that requires the magnetic field detection signal.OUT
pin will go LOW. When the magnet is removed, the OUT
pin will return to HIGH.Below is an example of how to connect and use the KY21 module with an Arduino UNO:
VCC
pin of the KY21 to the 5V pin of the Arduino.GND
pin of the KY21 to the GND pin of the Arduino.OUT
pin of the KY21 to digital pin 2 of the Arduino.// KY21 Magnetic Switch Module Example
// This code reads the output of the KY21 module and prints the status to the Serial Monitor.
const int sensorPin = 2; // KY21 OUT pin connected to digital pin 2
int sensorState = 0; // Variable to store the sensor state
void setup() {
pinMode(sensorPin, INPUT); // Set the sensor pin as input
Serial.begin(9600); // Initialize serial communication at 9600 baud
}
void loop() {
sensorState = digitalRead(sensorPin); // Read the state of the sensor
if (sensorState == LOW) {
// Magnet is detected
Serial.println("Magnet detected!");
} else {
// Magnet is not detected
Serial.println("No magnet detected.");
}
delay(500); // Wait for 500ms before reading again
}
The module does not detect the magnet:
The output signal is unstable:
The module outputs a constant HIGH signal:
OUT
pin is properly connected to the microcontroller or circuit.Q: Can the KY21 module detect any type of magnet?
A: Yes, the KY21 can detect most types of magnets, but the detection range depends on the magnet's strength and size.
Q: Can I use the KY21 with a 3.3V microcontroller?
A: Yes, the KY21 operates within a voltage range of 3.3V to 5V, making it compatible with 3.3V microcontrollers like the ESP32.
Q: Is the KY21 suitable for outdoor use?
A: The KY21 is not waterproof or weatherproof. If used outdoors, it should be enclosed in a protective casing to prevent damage from moisture or dust.