Skip to main content
ESPBoards

ESP32 KY-054 Phototransistor Module

The KY-054 is a phototransistor module that detects ambient light levels and provides an analog voltage output proportional to the light intensity. It is commonly used in projects that require light sensing capabilities, such as automatic lighting systems and brightness detection.

⬇️ Jump to Code Examples

Arduino Core Image
ESPHome Image
PlatformIO Image
MicroPython Image

🔗 Quick Links

KY-054 Phototransistor Module Datasheet ButtonKY-054 Phototransistor Module Specs ButtonKY-054 Phototransistor Module Specs ButtonKY-054 Phototransistor Module Specs Button

ℹ️ About KY-054 Phototransistor Module

The KY-054 Phototransistor Module is designed to detect light intensity and convert it into an electrical signal. It operates similarly to a light-dependent resistor (LDR) but offers faster response times and greater sensitivity. This module is ideal for applications such as ambient light monitoring, automatic brightness adjustment, and light-based control systems.

⚙️ KY-054 Sensor Technical Specifications

Below you can see the KY-054 Phototransistor 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 - 5V
  • Fixed Resistance: 10 kΩ
  • Dimensions: 28 x 15 x 7 mm
  • Operating Temperature: -25°C to 70°C
  • Output: Analog voltage proportional to light intensity

🔌 KY-054 Sensor Pinout

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

  • GND: Connects to the ground of the circuit.
  • +V: Supplies power to the module, typically 3.3V or 5V.
  • Signal: Outputs an analog voltage corresponding to the detected light intensity.

🛠️ KY-054 Phototransistor 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 Response from the Sensor

Issue: The sensor does not provide any output or the readings remain constant.

Solutions:

  • Ensure that the module is properly powered with the correct voltage (3.3V or 5V).
  • Verify that all connections are secure and correctly oriented.
  • Check if the analog input pin on the microcontroller is functioning correctly by testing with another analog sensor.

⚠️ Inaccurate or Fluctuating Readings

Issue: The sensor provides erratic or incorrect light intensity values.

Solutions:

  • Avoid exposing the sensor to direct light sources that may cause saturation.
  • Implement software filtering techniques, such as averaging multiple readings, to smooth out fluctuations.
  • Ensure that there are no loose connections or interference from nearby electronic components.

💻 Code Examples

Below you can find code examples of KY-054 Phototransistor Module with ESP32 in several frameworks:

If you encounter issues while using the KY-054 Phototransistor Module, check the Common Issues Troubleshooting Guide.

Arduino Core Image

ESP32 KY-054 Arduino IDE Code Example

Example in Arduino IDE

Fill in your main Arduino IDE sketch file with the following code to use the KY-054 Phototransistor Module:

// Define pin for phototransistor
int light_sensor = A5;
// Definition of the parameters required for the calculation
const double U1 = 5.0;
const double R2 = 10000.0;
double U2;
double I;
double R1;
double lux;
int rawValue;

void setup() {
// Define the pin mode
pinMode(light_sensor, INPUT);
// Set up serial communication for serial monitor
Serial.begin(9600);
Serial.println("KY-054 Brightness test");
}

void loop() {
// Reading the voltage of the light sensor
rawValue = analogRead(light_sensor);
U2 = rawValue * (5.0/1023) ;

// Check U2 for the division
if (U2 != 0) {
// Calculate the resistance of the sensor
R1 = (U1 * R2) / U2;
// Calculate current
I = (U1 / R1) * 1000000.0;
// Calculate lux
lux = log(I)/0.06;
}
else lux = 0;

// Output the result on the serial monitor
Serial.print("Lux:\t");
Serial.println(lux);

// wait for one second
delay(1000);
}

This Arduino code sets up the KY-054 phototransistor module to measure ambient light intensity. It reads the analog voltage from the sensor, calculates the corresponding lux value using a logarithmic formula, and outputs the result 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-054 ESP-IDF Code Example
Example in Espressif IoT Framework (ESP-IDF)

This ESP-IDF code configures GPIO36 as an analog input to read the output of the KY-054 phototransistor module. It calculates the voltage value and estimates the light intensity in lux using a logarithmic formula. The readings are displayed on the console every second.

ESPHome Image

ESP32 KY-054 ESPHome Code Example

Example in ESPHome (Home Assistant)

Fill in this configuration in your ESPHome YAML configuration file (example.yml) to integrate the KY-054 Phototransistor Module

sensor:
- platform: adc
pin: GPIO36
name: "KY-054 Light Sensor"
update_interval: 1s
filters:
- multiply: 3.3
- lambda: |-
float resistance = (3.3 * 10000.0) / (x == 0 ? 1 : x);
float current = (3.3 / resistance) * 1000000.0;
return log(current) / 0.06;

This ESPHome configuration sets up the KY-054 phototransistor module on GPIO36 as an analog input. It reads the voltage level, applies a mathematical conversion to estimate light intensity in lux, and updates the value every second.

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

PlatformIO Image

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

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

#include <Arduino.h>

#define LIGHT_SENSOR_PIN 36

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

void loop() {
int raw_value = analogRead(LIGHT_SENSOR_PIN);
float voltage = raw_value * (3.3 / 4095.0);
Serial.printf("Light Sensor Voltage: %.2fV\n", voltage);
delay(1000);
}

This PlatformIO code configures GPIO36 as an analog input for the KY-054 sensor. It reads the analog voltage, converts it to a readable format, and prints 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-054 MicroPython Code Example

Example in Micro Python Framework

Fill in this script in your MicroPython main.py file (main.py) to integrate the KY-054 Phototransistor Module with your ESP32.

import machine
import time
import math

LIGHT_SENSOR_PIN = machine.ADC(machine.Pin(36))
LIGHT_SENSOR_PIN.atten(machine.ADC.ATTN_11DB)

while True:
raw_value = LIGHT_SENSOR_PIN.read()
voltage = (raw_value / 4095) * 3.3
resistance = (3.3 * 10000.0) / (voltage if voltage > 0 else 1)
current = (3.3 / resistance) * 1000000.0
lux = math.log(current) / 0.06
print("Light Intensity:", lux, "lux")
time.sleep(1)

This MicroPython script sets up the KY-054 phototransistor module on GPIO36 as an analog input. It reads the sensor voltage, calculates the resistance, estimates the light intensity in lux, and prints the value 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-054 Phototransistor Module, its pinout, connection with ESP32 and KY-054 Phototransistor Module code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.