ESP32 KY-019 5V Relay Module
The KY-019 is a 5V relay module that enables microcontrollers to control high-voltage devices. It supports both AC and DC loads, making it suitable for various automation and control applications.
🔗 Quick Links
🛒 KY-019 Price
ℹ️ About KY-019 5V Relay Module
The KY-019 5V Relay Module allows microcontrollers like Arduino and ESP32 to control high-voltage devices. It operates on a 5V signal and can switch AC loads up to 250V at 10A or DC loads up to 30V at 10A. The module features a single-channel relay with both normally open (NO) and normally closed (NC) contacts, providing flexibility in controlling connected devices. An onboard LED indicates the relay's status, lighting up when the relay is active.⚙️ KY-019 Sensor Technical Specifications
Below you can see the KY-019 5V Relay 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: Digital
- Operating Voltage: 5V DC
- Control Signal Voltage: 5V DC
- Maximum AC Load: 250V AC at 10A
- Maximum DC Load: 30V DC at 10A
- Dimensions: 53 x 18 x 20 mm
- Weight: 16 g
🔌 KY-019 Sensor Pinout
Below you can see the pinout for the KY-019 5V Relay 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!
Pin (-):
Connects to ground (GND).Pin (+):
Connects to VCC (5V).Pin (S):
Connects to the control signal from the microcontroller.
🧵 KY-019 Wiring with ESP32
Below you can see the wiring for the KY-019 5V Relay 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.
KY-019 Pin (-):
Connect to ESP32GND
.KY-019 Pin (+):
Connect to ESP325V
.KY-019 Pin (S):
Connect to a digital GPIO pin on ESP32 (e.g.,GPIO5
).
🛠️ KY-019 5V Relay 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.
❌ Relay Not Activating
Issue: The relay does not switch when the control signal is applied.
Solutions:
- Ensure the control signal voltage is 5V; the relay may not activate with lower voltages.
- Verify that the
Pin (+)
is connected to a stable 5V power source. - Check for proper grounding between the module and the microcontroller.
⚠️ Unstable Relay Operation
Issue: The relay switches intermittently or unpredictably.
Solutions:
- Ensure that the power supply can provide sufficient current for the relay.
- Check for loose connections or faulty wiring.
- Confirm that the control signal is not experiencing noise or interference.
💻 Code Examples
Below you can find code examples of KY-019 5V Relay Module with ESP32 in several frameworks:
If you encounter issues while using the KY-019 5V Relay Module, check the Common Issues Troubleshooting Guide.

ESP32 KY-019 Arduino IDE Code Example
Fill in your main
Arduino IDE sketch file with the following code to use the KY-019 5V Relay Module:
int relayPin = 10; // Relay connected to digital pin 10
void setup() {
pinMode(relayPin, OUTPUT);
Serial.begin(9600);
Serial.println("KY-019 Relay Module Test");
}
void loop() {
digitalWrite(relayPin, HIGH); // Turn relay on
Serial.println("Relay is ON");
delay(1000); // Wait for a second
digitalWrite(relayPin, LOW); // Turn relay off
Serial.println("Relay is OFF");
delay(1000); // Wait for a second
}
This Arduino code sets up the KY-019 relay module on digital pin 10. It alternates the relay state every second, turning it on and off, while printing the status to the serial monitor.
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.

ESP32 KY-019 ESP-IDF Code ExampleExample in Espressif IoT Framework (ESP-IDF)
If you're using ESP-IDF to work with the KY-019 5V Relay 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/gpio.h"
#define RELAY_PIN GPIO_NUM_5
void app_main(void) {
gpio_config_t io_conf = {
.pin_bit_mask = (1ULL << RELAY_PIN),
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE
};
gpio_config(&io_conf);
printf("KY-019 Relay Module Test\n");
while (1) {
gpio_set_level(RELAY_PIN, 1); // Turn relay on
printf("Relay is ON\n");
vTaskDelay(pdMS_TO_TICKS(1000)); // Wait for a second
gpio_set_level(RELAY_PIN, 0); // Turn relay off
printf("Relay is OFF\n");
vTaskDelay(pdMS_TO_TICKS(1000)); // Wait for a second
}
}
This ESP-IDF code configures GPIO5 as an output to control the KY-019 relay module. It toggles the relay state every second, turning it on and off, while printing the status to the console.
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.

ESP32 KY-019 ESPHome Code Example
Fill in this configuration in your ESPHome YAML configuration file (example.yml
) to integrate the KY-019 5V Relay Module
switch:
- platform: gpio
pin: GPIO5
name: "KY-019 Relay"
icon: "mdi:relay"
restore_mode: ALWAYS_OFF
This ESPHome configuration sets up the KY-019 relay module on GPIO5 as a switch. The relay can be controlled from Home Assistant and will always start in the OFF state when powered up.
Upload this code to your ESP32 using the ESPHome dashboard or the esphome run
command.

ESP32 KY-019 PlatformIO Code Example
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-019 PlatformIO Example Code
Write this code in your PlatformIO project under the src/main.cpp
file to use the KY-019 5V Relay Module:
#include <Arduino.h>
#define RELAY_PIN 5
void setup() {
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(115200);
Serial.println("KY-019 Relay Module Test");
}
void loop() {
digitalWrite(RELAY_PIN, HIGH);
Serial.println("Relay is ON");
delay(1000);
digitalWrite(RELAY_PIN, LOW);
Serial.println("Relay is OFF");
delay(1000);
}
This PlatformIO code configures GPIO5 as an output for the KY-019 relay module. The relay toggles ON and OFF every second, with the status printed to the serial monitor.
Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload
command.

ESP32 KY-019 MicroPython Code Example
Fill in this script in your MicroPython main.py file (main.py
) to integrate the KY-019 5V Relay Module with your ESP32.
import machine
import time
RELAY_PIN = machine.Pin(5, machine.Pin.OUT)
while True:
RELAY_PIN.value(1)
print("Relay is ON")
time.sleep(1)
RELAY_PIN.value(0)
print("Relay is OFF")
time.sleep(1)
This MicroPython script configures GPIO5 as an output to control the KY-019 relay module. The relay turns ON and OFF every second, with messages printed to the console.
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-019 5V Relay Module, its pinout, connection with ESP32 and KY-019 5V Relay Module code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.