Skip to main content
ESPBoards

ESP32 KY-039 Heartbeat Sensor Module

The KY-039 is a heartbeat sensor module that uses an infrared LED and a phototransistor to detect pulse signals. It provides an analog output corresponding to the heartbeat, making it suitable for health monitoring and related applications.

⬇️ Jump to Code Examples

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

🔗 Quick Links

KY-039 Heartbeat Sensor Module Datasheet ButtonKY-039 Heartbeat Sensor Module Specs ButtonKY-039 Heartbeat Sensor Module Specs ButtonKY-039 Heartbeat Sensor Module Specs Button

🛒 KY-039 Price

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

Amazon com

Aliexpress logo

ℹ️ About KY-039 Heartbeat Sensor Module

The KY-039 Heartbeat Sensor Module is designed to detect heartbeats by measuring the variations in blood flow through a finger. It utilizes an infrared (IR) LED and a phototransistor to sense these changes. When a finger is placed between the IR LED and the phototransistor, the module outputs an analog voltage corresponding to the detected heartbeat. This sensor is commonly used in health monitoring projects, fitness applications, and biofeedback systems.

⚙️ KY-039 Sensor Technical Specifications

Below you can see the KY-039 Heartbeat 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
  • Operating Temperature Range: -40°C to 85°C
  • Dimensions: 19mm x 15mm

🔌 KY-039 Sensor Pinout

Below you can see the pinout for the KY-039 Heartbeat 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!

  • VCC: Connects to the power supply, typically 3.3V or 5V.
  • GND: Connects to the ground of the circuit.
  • S (Signal): Outputs an analog voltage corresponding to the heartbeat signal.

🛠️ KY-039 Heartbeat 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 Signal Detected

Issue: The sensor does not output any signal when a finger is placed between the IR LED and phototransistor.

Solutions:

  • Ensure the module is properly powered with the correct voltage (3.3V or 5V).
  • Verify all connections are secure and correctly wired.
  • Make sure the finger is positioned correctly between the IR LED and phototransistor.
  • Check for ambient light interference and consider shielding the sensor from external light sources.

⚠️ Unstable or Noisy Readings

Issue: The sensor outputs fluctuating or noisy signals.

Solutions:

  • Implement signal filtering in the software to smooth out the readings.
  • Ensure the environment is free from electrical noise or interference.
  • Check for any loose connections or faulty components on the module.

💻 Code Examples

Below you can find code examples of KY-039 Heartbeat Sensor Module with ESP32 in several frameworks:

If you encounter issues while using the KY-039 Heartbeat Sensor Module, check the Common Issues Troubleshooting Guide.

Arduino Core Image

ESP32 KY-039 Arduino IDE Code Example

Example in Arduino IDE

Fill in your main Arduino IDE sketch file with the following code to use the KY-039 Heartbeat Sensor Module:

#define SENSOR_PIN A0

void setup() {
Serial.begin(9600);
pinMode(SENSOR_PIN, INPUT);
Serial.println("KY-039 Heartbeat Sensor Test");
}

void loop() {
int sensorValue = analogRead(SENSOR_PIN);
Serial.println(sensorValue);
delay(10);
}

This Arduino code initializes the analog pin connected to the KY-039 sensor. It reads the analog value corresponding to the heartbeat signal and prints it to the serial monitor every 10 milliseconds. Users can observe the serial plotter to visualize the heartbeat waveform.

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-039 ESP-IDF Code Example
Example in Espressif IoT Framework (ESP-IDF)

If you're using ESP-IDF to work with the KY-039 Heartbeat 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-039 Heartbeat Sensor Test\n");
while (1) {
int raw = adc1_get_raw(SENSOR_CHANNEL);
float voltage = raw * (3.3 / 4095.0);
printf("Analog Voltage: %.2f V\n", voltage);
vTaskDelay(pdMS_TO_TICKS(10));
}
}

This ESP-IDF code configures GPIO34 as an analog input for the KY-039 heartbeat sensor. It reads the analog voltage corresponding to the heartbeat signal and prints it to the console every 10 milliseconds, allowing for real-time monitoring of the pulse waveform.

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-039 ESPHome Code Example

Example in ESPHome (Home Assistant)

Fill in this configuration in your ESPHome YAML configuration file (example.yml) to integrate the KY-039 Heartbeat Sensor Module

sensor:
- platform: adc
pin: GPIO34
name: "KY-039 Heartbeat Sensor"
update_interval: 10ms
filters:
- multiply: 3.3
- lambda: |-
return x * 1000; // Convert to millivolts

This ESPHome configuration sets up the KY-039 sensor connected to GPIO34. It reads the analog value every 10 milliseconds, converts it to millivolts, and makes it available as a sensor named "KY-039 Heartbeat Sensor" for further processing or visualization.

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

PlatformIO Image

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

Write this code in your PlatformIO project under the src/main.cpp file to use the KY-039 Heartbeat Sensor Module:

#include <Arduino.h>

#define SENSOR_PIN 34

void setup() {
Serial.begin(115200);
Serial.println("KY-039 Heartbeat Sensor Test");
}

void loop() {
int sensorValue = analogRead(SENSOR_PIN);
float voltage = sensorValue * (3.3 / 4095.0) * 1000; // Convert to millivolts
Serial.printf("Analog Voltage: %.2f mV\n", voltage);
delay(10);
}

This PlatformIO code sets up GPIO34 as an analog input for the KY-039 heartbeat sensor. It continuously reads the sensor value, converts it to millivolts, and prints it to the serial monitor every 10 milliseconds to capture the heartbeat waveform.

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-039 MicroPython Code Example

Example in Micro Python Framework

Fill in this script in your MicroPython main.py file (main.py) to integrate the KY-039 Heartbeat 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:
sensor_value = SENSOR_PIN.read()
voltage = (sensor_value / 4095) * 3300 # Convert to millivolts
print("Analog Voltage:", voltage, "mV")
time.sleep(0.01)

This MicroPython script configures GPIO34 as an ADC input for the KY-039 heartbeat sensor. It continuously reads the sensor value, converts it to millivolts, and prints the voltage every 10 milliseconds for real-time heartbeat monitoring.

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-039 Heartbeat Sensor Module, its pinout, connection with ESP32 and KY-039 Heartbeat Sensor Module code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.