Skip to main content
ESPBoards

ESP32 KY-035 Analog Hall Magnetic Sensor Module

The KY-035 is an analog Hall magnetic sensor module based on the AH49E linear Hall-effect sensor. It provides an analog output voltage proportional to the detected magnetic field strength and polarity, making it ideal for applications in motor control, position sensing, and magnetic field detection.

⬇️ Jump to Code Examples

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

🔗 Quick Links

KY-035 Analog Hall Magnetic Sensor Module Datasheet ButtonKY-035 Analog Hall Magnetic Sensor Module Specs ButtonKY-035 Analog Hall Magnetic Sensor Module Specs ButtonKY-035 Analog Hall Magnetic Sensor Module Specs Button

🛒 KY-035 Price

KY-035 Analog Hall Magnetic Sensor Module
Normally, the KY-035 Analog Hall Magnetic Sensor Module costs around 2$ per Psc.
The prices are subject to change. Check current price:

Amazon com

Amazon de logo

Aliexpress logo

ℹ️ About KY-035 Analog Hall Magnetic Sensor Module

The KY-035 Analog Hall Magnetic Sensor Module utilizes the AH49E linear Hall-effect sensor to detect magnetic fields. It outputs an analog voltage proportional to the magnetic field strength, allowing for the detection of both the presence and polarity of a magnetic field. This module is suitable for applications such as motor control, position sensing, and magnetic field detection.

⚙️ KY-035 Sensor Technical Specifications

Below you can see the KY-035 Analog Hall Magnetic Sensor 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
  • Operating Voltage: 3.3V to 5V
  • Current Consumption: 3.5 mA at 5V
  • Output Type: Analog voltage
  • Operating Temperature Range: -40°C to 85°C
  • Dimensions: 19 x 15 x 3 mm

🔌 KY-035 Sensor Pinout

Below you can see the pinout for the KY-035 Analog Hall Magnetic Sensor 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!

  • S (Signal): Outputs an analog voltage corresponding to the magnetic field strength.
  • Middle Pin (VCC): Supplies power to the module, typically 3.3V to 5V.
  • G (Ground): Connects to the ground of the circuit.

🧵 KY-035 Wiring with ESP32

Below you can see the wiring for the KY-035 Analog Hall Magnetic Sensor 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.

  • S (Signal): Connect to an analog input pin on the ESP32 (e.g., GPIO34).
  • Middle Pin (VCC): Connect to ESP32 3.3V.
  • G (Ground): Connect to ESP32 GND.

🛠️ KY-035 Analog Hall Magnetic Sensor 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 Output Signal

Issue: The sensor does not provide any output signal.

Solutions:

  • Verify that the module is properly powered with the correct voltage (3.3V to 5V).
  • Ensure all connections are secure and correctly wired.
  • Check for any damage to the sensor or module components.

⚠️ Incorrect or Fluctuating Readings

Issue: The sensor outputs incorrect or unstable readings.

Solutions:

  • Ensure that there are no external magnetic fields or electrical noise affecting the sensor.
  • Use proper shielding and grounding techniques to minimize interference.
  • Calibrate the sensor to account for any environmental factors.

💻 Code Examples

Below you can find code examples of KY-035 Analog Hall Magnetic Sensor Module with ESP32 in several frameworks:

If you encounter issues while using the KY-035 Analog Hall Magnetic Sensor Module, check the Common Issues Troubleshooting Guide.

Arduino Core Image

ESP32 KY-035 Arduino IDE Code Example

Example in Arduino IDE

Fill in your main Arduino IDE sketch file with the following code to use the KY-035 Analog Hall Magnetic Sensor Module:

int sensorPin = A0; // The input pin for the sensor

void setup() {
Serial.begin(9600);
Serial.println("KY-035 Magnetic Field Detection");
}

void loop() {
int rawValue = analogRead(sensorPin);
float voltage = rawValue * (5.0 / 1023.0) * 1000; // Convert to millivolts
Serial.print("Voltage: ");
Serial.print(voltage);
Serial.println(" mV");
delay(1000);
}

This Arduino code initializes the serial communication and reads the analog value from the KY-035 sensor connected to pin A0. It converts the raw analog value to voltage in millivolts and prints it 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-035 ESP-IDF Code Example
Example in Espressif IoT Framework (ESP-IDF)

If you're using ESP-IDF to work with the KY-035 Analog Hall Magnetic Sensor 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"

#define SENSOR_CHANNEL ADC1_CHANNEL_6 // GPIO34

void app_main(void) {
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(SENSOR_CHANNEL, ADC_ATTEN_DB_11);

printf("KY-035 Magnetic Field Detection\n");
while (1) {
int raw = adc1_get_raw(SENSOR_CHANNEL);
float voltage = raw * (3.3 / 4095.0) * 1000; // Convert to millivolts
printf("Voltage: %.2f mV\n", voltage);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}

This ESP-IDF code configures ADC1 channel 6 (GPIO34) to read analog values from the KY-035 sensor. It converts the raw ADC value to voltage in millivolts and prints it to the console 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-035 ESPHome Code Example

Example in ESPHome (Home Assistant)

Fill in this configuration in your ESPHome YAML configuration file (example.yml) to integrate the KY-035 Analog Hall Magnetic Sensor Module

sensor:
- platform: adc
pin: GPIO34
name: "KY-035 Magnetic Field Sensor"
update_interval: 1s
filters:
- multiply: 3.3
- lambda: |-
return x * 1000; // Convert to millivolts

This ESPHome configuration sets up the KY-035 sensor connected to GPIO34 as an ADC sensor. It reads the analog voltage every second, converts it to millivolts, and logs the value. The lambda function ensures the output is in millivolts for easier interpretation.

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

PlatformIO Image

ESP32 KY-035 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-035 PlatformIO Example Code

Write this code in your PlatformIO project under the src/main.cpp file to use the KY-035 Analog Hall Magnetic Sensor Module:

#include <Arduino.h>

#define SENSOR_PIN 34

void setup() {
Serial.begin(115200);
Serial.println("KY-035 Magnetic Field Sensor Test");
}

void loop() {
int raw_value = analogRead(SENSOR_PIN);
float voltage = raw_value * (3.3 / 4095.0) * 1000; // Convert to millivolts
Serial.print("Magnetic Field Voltage: ");
Serial.print(voltage);
Serial.println(" mV");
delay(1000);
}

This PlatformIO code sets up GPIO34 as an analog input for the KY-035 sensor. It reads the raw ADC value, converts it to millivolts, and logs it to the serial monitor 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-035 MicroPython Code Example

Example in Micro Python Framework

Fill in this script in your MicroPython main.py file (main.py) to integrate the KY-035 Analog Hall Magnetic Sensor Module with your ESP32.

import machine
import time

SENSOR_PIN = machine.ADC(machine.Pin(34))
SENSOR_PIN.atten(machine.ADC.ATTN_11DB)

while True:
raw_value = SENSOR_PIN.read()
voltage = (raw_value / 4095) * 3300 # Convert to millivolts
print("Magnetic Field Voltage:", voltage, "mV")
time.sleep(1)

This MicroPython script configures GPIO34 as an ADC input for the KY-035 sensor. It reads the raw ADC value, converts it to millivolts, and prints it to the console 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-035 Analog Hall Magnetic Sensor Module, its pinout, connection with ESP32 and KY-035 Analog Hall Magnetic Sensor Module code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.