ESP32 KY-019 5V Relay Module
Overview
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.
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.
Where to Buy
Prices are subject to change. We earn from qualifying purchases as an Amazon Associate.
Technical Specifications
Pinout Configuration
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.
Troubleshooting Guide
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.
Debugging Tips
🔍 Serial Monitor
Use the Serial Monitor to check for error messages and verify the sensor's output. Add debug prints in your code to track the sensor's state.
⚡ Voltage Checks
Use a multimeter to verify voltage levels and check for continuity in your connections. Ensure the power supply is stable and within the sensor's requirements.
Additional Resources
Code Examples
Arduino Example
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.
ESP-IDF Example
#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.
ESPHome Example
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.
PlatformIO Example
platformio.ini
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino
PlatformIO Example Code
#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.
MicroPython Example
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.
Conclusion
The ESP32 KY-019 5V Relay Module is a powerful KY-0xx module sensor that offers excellent performance and reliability. With support for multiple development platforms including Arduino, ESP-IDF, ESPHome, PlatformIO, and MicroPython, it's a versatile choice for your IoT projects.
For optimal performance, ensure proper wiring and follow the recommended configuration for your chosen development platform.
Always verify power supply requirements and pin connections before powering up your project to avoid potential damage.