ESP32 KY-018 Photoresistor Module
Overview
The KY-018 is a photoresistor module that detects ambient light levels. It outputs an analog signal corresponding to the light intensity, making it suitable for projects that require light sensing capabilities.
About KY-018 Photoresistor Module
The KY-018 Photoresistor Module is a light-sensitive sensor that varies its resistance based on the ambient light intensity. It contains a light-dependent resistor (LDR) and a fixed 10 kΩ resistor, forming a voltage divider circuit. As the surrounding light increases, the LDR’s resistance decreases, resulting in a higher output voltage. This module operates at a voltage range of 3.3V to 5V and provides an analog output signal. It’s commonly used in applications such as ambient light detection, automatic lighting control, and light intensity measurement.
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 (middle):
Connects to VCC (3.3V to 5V).Pin (S):
Outputs the analog signal; connect to an analog input on your microcontroller.
Troubleshooting Guide
Common Issues
❌ No Response from Sensor
Issue: The sensor does not output any signal regardless of light conditions.
Solutions:
- Verify that all connections are secure and correctly placed.
- Ensure the module is receiving the appropriate voltage (3.3V to 5V).
- Check if the microcontroller's analog input pin is correctly configured.
⚠️ Incorrect or Fluctuating Readings
Issue: The sensor outputs inconsistent or incorrect light intensity values.
Solutions:
- Ensure stable lighting conditions during measurements.
- Check for loose connections or interference from nearby electronic components.
- Implement software filtering to smooth out rapid fluctuations in readings.
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 sensorPin = A0; // Photoresistor connected to analog pin A0
int sensorValue = 0;
void setup() {
Serial.begin(9600);
Serial.println("KY-018 Photoresistor Test");
}
void loop() {
sensorValue = analogRead(sensorPin);
Serial.print("Light Intensity: ");
Serial.println(sensorValue);
delay(500);
}
This Arduino code sets up the KY-018 photoresistor on analog pin A0. It reads the analog value corresponding to the light intensity and prints it to the serial monitor every 500 milliseconds.
ESP-IDF Example
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/adc.h"
#define PHOTORESISTOR_CHANNEL ADC1_CHANNEL_0 // GPIO36
void app_main(void) {
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(PHOTORESISTOR_CHANNEL, ADC_ATTEN_DB_0);
printf("KY-018 Photoresistor Test\n");
while (1) {
int raw = adc1_get_raw(PHOTORESISTOR_CHANNEL);
printf("Light Intensity: %d\n", raw);
vTaskDelay(pdMS_TO_TICKS(500));
}
}
This ESP-IDF code configures GPIO36 (ADC1_CHANNEL_0) to read analog values from the KY-018 photoresistor. It prints the raw ADC value, corresponding to light intensity, to the console every 500 milliseconds.
ESPHome Example
sensor:
- platform: adc
pin: GPIO36
name: "KY-018 Light Intensity"
update_interval: 500ms
filters:
- multiply: 0.00322265625
This ESPHome configuration sets up the KY-018 photoresistor on GPIO36. It reads the analog value every 500 milliseconds and applies a multiplier to convert the raw ADC reading to a voltage value.
PlatformIO Example
platformio.ini
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino
PlatformIO Example Code
#include <Arduino.h>
#define PHOTORESISTOR_PIN 36
void setup() {
Serial.begin(115200);
Serial.println("KY-018 Photoresistor Test");
}
void loop() {
int sensorValue = analogRead(PHOTORESISTOR_PIN);
Serial.print("Light Intensity: ");
Serial.println(sensorValue);
delay(500);
}
This PlatformIO code configures GPIO36 as an analog input to read the KY-018 photoresistor sensor values. The light intensity readings are printed to the serial monitor every 500 milliseconds.
MicroPython Example
import machine
import time
PHOTORESISTOR_PIN = 36 # ADC input pin
adc = machine.ADC(machine.Pin(PHOTORESISTOR_PIN))
adc.atten(machine.ADC.ATTN_11DB) # Full range 0-3.3V
while True:
light_intensity = adc.read()
print("Light Intensity:", light_intensity)
time.sleep(0.5)
This MicroPython script configures GPIO36 as an ADC input for the KY-018 photoresistor sensor. The sensor value is read every 500 milliseconds and printed to the console.
Conclusion
The ESP32 KY-018 Photoresistor 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.