Skip to main content
ESPBoards

ESP32 KY-025 Reed Switch Module

The KY-025 is a reed switch module that provides both analog and digital outputs. It is equipped with a potentiometer to adjust sensitivity and is suitable for detecting magnetic fields in various applications.

⬇️ Jump to Code Examples

Arduino Core Image
ESP-IDF Image
ESPHome Image
PlatformIO Image
MicroPython Image

🔗 Quick Links

KY-025 Reed Switch Module Datasheet ButtonKY-025 Reed Switch Module Specs ButtonKY-025 Reed Switch Module Specs ButtonKY-025 Reed Switch Module Specs Button

🛒 KY-025 Price

KY-025 Reed Switch Module
Normally, the KY-025 Reed Switch Module costs around 2$ per Psc.
The prices are subject to change. Check current price:

Amazon com

Amazon de logo

Aliexpress logo

ℹ️ About KY-025 Reed Switch Module

The KY-025 Reed Switch Module is a magnetic sensor that detects the presence of a magnetic field. It features both analog and digital outputs: the analog output provides a voltage corresponding to the magnetic field strength, while the digital output activates when the magnetic field exceeds a user-defined threshold, adjustable via an onboard potentiometer. This module is commonly used in applications such as proximity sensing, position detection, and security systems.

⚙️ KY-025 Sensor Technical Specifications

Below you can see the KY-025 Reed Switch Module Technical Specifications. The sensor is compatible with the ESP32, operating within a voltage range suitable for microcontrollers. For precise details about its features, specifications, and usage, refer to the sensor’s datasheet.

  • Type: module
  • Protocol: Analog/Digital
  • Operating Voltage: 3.3V to 5V
  • Dimensions: 36 x 15 x 14 mm
  • Weight: 2 g

🔌 KY-025 Sensor Pinout

Below you can see the pinout for the KY-025 Reed Switch Module. The VCC pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND pin is the ground connection and must be connected to the ground of your ESP32!

  • GND: Connects to ground.
  • +V: Connects to 3.3V or 5V power supply.
  • D0: Digital output; goes high when the magnetic field exceeds the set threshold.
  • A0: Analog output; provides a voltage proportional to the magnetic field strength.

🧵 KY-025 Wiring with ESP32

Below you can see the wiring for the KY-025 Reed Switch Module with the ESP32. Connect the VCC pin of the sensor to the 3.3V pin on the ESP32 or external power supply for power and the GND pin of the sensor to the GND pin of the ESP32. Depending on the communication protocol of the sensor (e.g., I2C, SPI, UART, or analog), connect the appropriate data and clock or signal pins to compatible GPIO pins on the ESP32, as shown below in the wiring diagram.

  • GND: Connect to ESP32 GND.
  • +V: Connect to ESP32 3.3V or 5V.
  • D0: Connect to a digital input pin on ESP32 (e.g., GPIO17).
  • A0: Connect to an analog input pin on ESP32 (e.g., GPIO36).

🛠️ KY-025 Reed Switch Module Troubleshooting

This guide outlines a systematic approach to troubleshoot and resolve common problems with the . Start by confirming that the hardware connections are correct, as wiring mistakes are the most frequent cause of issues. If you are sure the connections are correct, follow the below steps to debug common issues.

❌ No Response from Sensor

Issue: The sensor does not provide any output when exposed to a magnetic field.

Solutions:

  • Verify all connections are secure and correctly placed.
  • Ensure the module is receiving the appropriate voltage (3.3V or 5V).
  • Adjust the potentiometer to set the correct sensitivity threshold.
  • Test the sensor with a known magnetic source to confirm functionality.

⚠️ Unstable Analog Readings

Issue: The analog output fluctuates without any change in the magnetic field.

Solutions:

  • Check for electromagnetic interference from nearby devices.
  • Ensure that the power supply is stable and free from noise.
  • Use shielded cables for analog signal transmission if necessary.

💻 Code Examples

Below you can find code examples of KY-025 Reed Switch Module with ESP32 in several frameworks:

If you encounter issues while using the KY-025 Reed Switch Module, check the Common Issues Troubleshooting Guide.

Arduino Core Image

ESP32 KY-025 Arduino IDE Code Example

Example in Arduino IDE

Fill in your main Arduino IDE sketch file with the following code to use the KY-025 Reed Switch Module:

// Declaration and initialization of the input pins
int analog_input = A0; // Analog output of the sensor
int digital_input = 3; // Digital output of the sensor

void setup() {
pinMode(analog_input, INPUT);
pinMode(digital_input, INPUT);
Serial.begin(9600); // Serial output with 9600 bps
Serial.println("KY-025 Magnetic field detection");
}

void loop() {
float analog_value;
int digital_value;

// Current values are read out, converted to the voltage value...
analog_value = analogRead(analog_input) * (5.0 / 1023.0);
digital_value = digitalRead(digital_input);

//... and printed at this point
Serial.print("Analog voltage value: ");
Serial.print(analog_value, 4);
Serial.print(" V, Threshold value: ");

if (digital_value == 1) {
Serial.println("reached");
} else {
Serial.println("not reached");
}
Serial.println("-------------------------------");
delay(1000);
}

This Arduino code reads the analog voltage from the KY-025 sensor's analog output and checks the digital output to determine if the magnetic field strength has surpassed the set threshold. The results are printed to the serial monitor every second.

Connect your ESP32 to your computer via a USB cable, Ensure the correct Board and Port are selected under Tools, Click the "Upload" button in the Arduino IDE to compile and upload the code to your ESP32.

ESP-IDF Image

ESP32 KY-025 ESP-IDF Code Example
Example in Espressif IoT Framework (ESP-IDF)

If you're using ESP-IDF to work with the KY-025 Reed Switch Module, here's how you can set it up and read data from the sensor. Fill in this code in the main ESP-IDF file:

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/adc.h"
#include "driver/gpio.h"

#define REED_SENSOR_ANALOG_PIN ADC1_CHANNEL_0 // GPIO36
#define REED_SENSOR_DIGITAL_PIN GPIO_NUM_17

void app_main(void) {
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(REED_SENSOR_ANALOG_PIN, ADC_ATTEN_DB_11);
gpio_set_direction(REED_SENSOR_DIGITAL_PIN, GPIO_MODE_INPUT);

printf("KY-025 Magnetic Field Detection\n");
while (1) {
int analog_value = adc1_get_raw(REED_SENSOR_ANALOG_PIN);
int digital_value = gpio_get_level(REED_SENSOR_DIGITAL_PIN);
printf("Analog Value: %d, Digital Threshold: %s\n", analog_value, digital_value ? "Reached" : "Not Reached");
vTaskDelay(pdMS_TO_TICKS(1000));
}
}

This ESP-IDF code reads the analog value from the KY-025 sensor using ADC on GPIO36 and monitors the digital output on GPIO17 to determine if the magnetic field threshold is exceeded. The results are printed every second.

Update the I2C pins (I2C_MASTER_SDA_IO and I2C_MASTER_SCL_IO) to match your ESP32 hardware setup, Use idf.py build to compile the project, Use idf.py flash to upload the code to your ESP32.

ESPHome Image

ESP32 KY-025 ESPHome Code Example

Example in ESPHome (Home Assistant)

Fill in this configuration in your ESPHome YAML configuration file (example.yml) to integrate the KY-025 Reed Switch Module

sensor:
- platform: adc
pin: GPIO36
name: "KY-025 Magnetic Field Strength"
update_interval: 1s
binary_sensor:
- platform: gpio
pin:
number: GPIO17
mode: INPUT_PULLUP
name: "KY-025 Magnetic Threshold Trigger"

This ESPHome configuration reads the KY-025 sensor's analog output on GPIO36 and detects the threshold trigger using GPIO17 as a binary sensor. Updates are logged every second.

Upload this code to your ESP32 using the ESPHome dashboard or the esphome run command.

PlatformIO Image

ESP32 KY-025 PlatformIO Code Example

Example in PlatformIO Framework

For PlatformIO, make sure to configure the platformio.ini file with the appropriate environment and libraries, and then proceed with the code.

Configure platformio.ini

First, your platformio.ini should look like below. You might need to include some libraries as shown. Make sure to change the board to your ESP32:

[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino

ESP32 KY-025 PlatformIO Example Code

Write this code in your PlatformIO project under the src/main.cpp file to use the KY-025 Reed Switch Module:

#include <Arduino.h>

#define REED_SENSOR_ANALOG_PIN A0
#define REED_SENSOR_DIGITAL_PIN 17

void setup() {
pinMode(REED_SENSOR_DIGITAL_PIN, INPUT);
Serial.begin(115200);
Serial.println("KY-025 Magnetic Sensor Test");
}

void loop() {
int analog_value = analogRead(REED_SENSOR_ANALOG_PIN);
int digital_value = digitalRead(REED_SENSOR_DIGITAL_PIN);
Serial.printf("Analog: %d, Digital: %s\n", analog_value, digital_value ? "Threshold Reached" : "Not Reached");
delay(1000);
}

This PlatformIO code reads the KY-025 sensor's analog output and monitors the digital threshold trigger. The values are printed every second.

Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload command.

MicroPython Image

ESP32 KY-025 MicroPython Code Example

Example in Micro Python Framework

Fill in this script in your MicroPython main.py file (main.py) to integrate the KY-025 Reed Switch Module with your ESP32.

import machine
import time

REED_SENSOR_ANALOG_PIN = machine.ADC(machine.Pin(36))
REED_SENSOR_ANALOG_PIN.atten(machine.ADC.ATTN_11DB)
REED_SENSOR_DIGITAL_PIN = machine.Pin(17, machine.Pin.IN, machine.Pin.PULL_UP)

while True:
analog_value = REED_SENSOR_ANALOG_PIN.read()
digital_value = REED_SENSOR_DIGITAL_PIN.value()
print("Analog:", analog_value, "Digital:", "Threshold Reached" if digital_value else "Not Reached")
time.sleep(1)

This MicroPython script reads the KY-025 sensor's analog signal using ADC on GPIO36 and monitors the digital output on GPIO17. The readings are printed every second.

Upload this code to your ESP32 using a MicroPython-compatible IDE, such as Thonny, uPyCraft, or tools like ampy.

Conclusion

We went through technical specifications of KY-025 Reed Switch Module, its pinout, connection with ESP32 and KY-025 Reed Switch Module code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.