ESP32 KY-051 Voltage Translator / Level Shifter
Overview
The KY-051 is a versatile voltage level shifter module that enables safe communication between devices operating at different voltage levels. With four bi-directional channels, it ensures compatibility and protects components from potential damage due to voltage mismatches.
About KY-051 Voltage Translator / Level Shifter
The KY-051 Voltage Translator, also known as a Level Shifter, is designed to safely interface devices operating at different voltage levels. It features four channels that can convert digital signals either up or down, facilitating communication between components like microcontrollers and sensors with varying voltage requirements.
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.
VCCa:
Connects to the lower voltage reference (e.g., 3.3V).VCCb:
Connects to the higher voltage reference (e.g., 5V).GND:
Common ground connection for both voltage levels.A1-A4:
Input/Output pins corresponding to VCCa voltage level.B1-B4:
Input/Output pins corresponding to VCCb voltage level.
Wiring with ESP32
VCCa:
Connect to the lower voltage device's power supply (e.g., 3.3V from an ESP32).VCCb:
Connect to the higher voltage device's power supply (e.g., 5V from an Arduino).GND:
Connect to the common ground shared by both devices.A1-A4:
Connect to the lower voltage device's I/O pins.B1-B4:
Connect to the higher voltage device's I/O pins.
Troubleshooting Guide
Common Issues
❌ No Signal Translation
Issue: Signals are not being translated between voltage levels.
Solutions:
- Verify that
VCCb
is greater than or equal toVCCa
as required. - Ensure all connections are secure and correctly wired according to the pinout diagram.
- Confirm that both devices share a common ground connection.
⚠️ Unstable Signal Transmission
Issue: Signal transmission is erratic or unreliable.
Solutions:
- Check for loose or faulty wiring connections.
- Ensure that the voltage levels supplied to
VCCa
andVCCb
are within the specified ranges. - Verify that the connected devices are functioning properly and are compatible with the voltage levels being used.
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
#define LOW_VOLT_PIN 2 // Pin connected to low voltage side (e.g., 3.3V logic)
#define HIGH_VOLT_PIN 3 // Pin connected to high voltage side (e.g., 5V logic)
void setup() {
pinMode(LOW_VOLT_PIN, INPUT);
pinMode(HIGH_VOLT_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("KY-051 Voltage Level Shifter Test");
}
void loop() {
int low_signal = digitalRead(LOW_VOLT_PIN);
digitalWrite(HIGH_VOLT_PIN, low_signal);
Serial.print("Low Voltage Signal: ");
Serial.print(low_signal);
Serial.print(" -> High Voltage Output: ");
Serial.println(digitalRead(HIGH_VOLT_PIN));
delay(500);
}
This Arduino code tests the KY-051 voltage level shifter by reading a digital signal from a low voltage pin (3.3V logic) and shifting it to a high voltage pin (5V logic). It continuously reads the signal, shifts it, and prints both the input and output values to the serial monitor every 500ms.
ESP-IDF Example
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#define LOW_VOLT_PIN GPIO_NUM_2 // Low voltage side (3.3V logic)
#define HIGH_VOLT_PIN GPIO_NUM_3 // High voltage side (5V logic)
void app_main() {
gpio_pad_select_gpio(LOW_VOLT_PIN);
gpio_pad_select_gpio(HIGH_VOLT_PIN);
gpio_set_direction(LOW_VOLT_PIN, GPIO_MODE_INPUT);
gpio_set_direction(HIGH_VOLT_PIN, GPIO_MODE_OUTPUT);
printf("KY-051 Voltage Level Shifter Test\n");
while (1) {
int low_signal = gpio_get_level(LOW_VOLT_PIN);
gpio_set_level(HIGH_VOLT_PIN, low_signal);
printf("Low Voltage Signal: %d -> High Voltage Output: %d\n", low_signal, gpio_get_level(HIGH_VOLT_PIN));
vTaskDelay(pdMS_TO_TICKS(500));
}
}
This ESP-IDF code configures GPIO2 as an input (low voltage side) and GPIO3 as an output (high voltage side). It continuously reads the input signal, shifts it to the high voltage side, and prints the values to the console every 500ms.
ESPHome Example
sensor:
- platform: gpio
pin: GPIO2
name: "KY-051 Low Voltage Signal"
update_interval: 0.5s
binary_sensor:
- platform: gpio
pin: GPIO3
name: "KY-051 High Voltage Output"
This ESPHome configuration sets up the KY-051 module to monitor a low voltage signal on GPIO2 and shift it to GPIO3. The signals are updated every 500ms.
PlatformIO Example
platformio.ini
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino
PlatformIO Example Code
#include <Arduino.h>
#define LOW_VOLT_PIN 2
#define HIGH_VOLT_PIN 3
void setup() {
pinMode(LOW_VOLT_PIN, INPUT);
pinMode(HIGH_VOLT_PIN, OUTPUT);
Serial.begin(115200);
Serial.println("KY-051 Voltage Level Shifter Test");
}
void loop() {
int low_signal = digitalRead(LOW_VOLT_PIN);
digitalWrite(HIGH_VOLT_PIN, low_signal);
Serial.printf("Low Voltage Signal: %d -> High Voltage Output: %d\n", low_signal, digitalRead(HIGH_VOLT_PIN));
delay(500);
}
This PlatformIO code configures GPIO2 as an input and GPIO3 as an output to test the KY-051 voltage level shifter. It reads the signal from the low voltage side and shifts it to the high voltage side, printing the results to the serial monitor every 500ms.
MicroPython Example
import machine
import time
LOW_VOLT_PIN = machine.Pin(2, machine.Pin.IN)
HIGH_VOLT_PIN = machine.Pin(3, machine.Pin.OUT)
while True:
low_signal = LOW_VOLT_PIN.value()
HIGH_VOLT_PIN.value(low_signal)
print("Low Voltage Signal:", low_signal, "-> High Voltage Output:", HIGH_VOLT_PIN.value())
time.sleep(0.5)
This MicroPython script configures GPIO2 as an input and GPIO3 as an output for the KY-051 voltage level shifter. It continuously reads the low voltage signal, shifts it to the high voltage side, and prints the values every 500ms.
Conclusion
The ESP32 KY-051 Voltage Translator / Level Shifter 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.