LM393 Light Sensor with Arduino: Complete Wiring & Code Guide
Learn how to connect an LM393-based light sensor module to an Arduino Uno, read both analog and digital outputs, and use it to build 3 practical projects. Compatible with ESP32, ESP8266, and Raspberry Pi Pico.
What You Need
- ✓ Arduino Uno (or Nano, Mega)
- ✓ LM393 Light Sensor Module(~$0.85)
- ✓ Jumper wires (3 pieces)
- ✓ USB cable to program the Arduino
All components are available on AliExpress, LCSC, and Amazon for under $5 total.
How the LM393 Light Sensor Works
The module has two main parts:
- Photoresistor (LDR): A light-dependent resistor. Its resistance decreases as light intensity increases (typically 1kΩ in bright light, 1MΩ in darkness).
- LM393 Comparator IC: Compares the LDR voltage against a threshold set by the on-board potentiometer, then outputs a clean digital HIGH/LOW signal.
The module provides two outputs:
- Analog (AO): Direct voltage from the voltage divider, proportional to light intensity. Connect to Arduino's ADC pin (e.g., A0).
- Digital (DO): Goes HIGH when light is above the threshold (or below, depending on jumper). Connect to any digital pin (e.g., D2).
Wiring Diagram
Connect the module to your Arduino using 3 jumper wires:
LM393 Module Arduino Uno ───────────── ────────── VCC ──────────────── 5V GND ──────────────── GND AO ──────────────── A0 DO ──────────────── D2 Optional: LED on D13 for visual feedback
Pin Configuration
Most LM393 light sensor modules have 3 or 4 pins:
- VCC: Power input (3.3V or 5V)
- GND: Ground
- DO: Digital output (threshold-based)
- AO: Analog output (continuous)
Arduino Code
Copy and paste this code into the Arduino IDE. It reads both the analog and digital outputs and prints them to the Serial Monitor.
// LM393 Light Sensor with Arduino
// Reads both analog (AO) and digital (DO) outputs
// More tutorials: https://comparator.online
const int analogPin = A0; // AO connected to A0
const int digitalPin = 2; // DO connected to D2
const int ledPin = 13; // Built-in LED
void setup() {
pinMode(digitalPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("LM393 Light Sensor Ready");
}
void loop() {
// Read analog value (0-1023)
int analogValue = analogRead(analogPin);
// Read digital value (HIGH or LOW)
int digitalValue = digitalRead(digitalPin);
// Convert to voltage (0-5V)
float voltage = analogValue * (5.0 / 1023.0);
// Convert to light percentage (inverted: lower value = brighter)
int lightPercent = map(analogValue, 0, 1023, 100, 0);
// Print to Serial Monitor
Serial.print("Light: ");
Serial.print(lightPercent);
Serial.print("% | Voltage: ");
Serial.print(voltage, 2);
Serial.print("V | Digital: ");
Serial.println(digitalValue == HIGH ? "BRIGHT" : "DARK");
// Visual feedback via built-in LED
digitalWrite(ledPin, digitalValue);
delay(200);
}Calibrating the Threshold
The on-board potentiometer sets the digital output threshold. To calibrate:
- Power up the Arduino with the code running.
- Open the Serial Monitor to see live values.
- Place the sensor in the lighting condition you want to detect (e.g., shade for "dark" detection).
- Turn the potentiometer with a small screwdriver until the digital output toggles.
- The threshold is now set for that specific light level.
3 Project Ideas
Automatic Street Light
Turn on an LED (or relay controlling a real light) when ambient light drops below threshold. Great for Arduino beginners.
Plant Light Monitor
Log light levels over time to optimize plant placement. Send data to a serial plotter or save to SD card for analysis.
Smart Home Trigger
Trigger Home Assistant or MQTT events when sunlight enters a room. Combine with ESP32 for WiFi-enabled projects.
ESP32 / ESP8266 Compatibility
The LM393 light sensor works identically with ESP32 and ESP8266. Just change the pin numbers:
- ESP32: AO → GPIO 34 (ADC1_CH6), DO → GPIO 4. Use
analogRead()returns 0-4095 (12-bit ADC). - ESP8266 (NodeMCU): AO → A0, DO → D1. Note: ESP8266 ADC is only 0-1V, use voltage divider.
- Raspberry Pi Pico: AO → GP26 (ADC0), DO → GP15. Use MicroPython or CircuitPython.
Frequently Asked Questions
What's the difference between the analog and digital outputs?
The analog output (AO) gives you a continuous voltage reading proportional to light intensity—useful for measuring exact light levels. The digital output (DO) is a simple HIGH/LOW signal that triggers when light crosses a threshold you set with the potentiometer—useful for "is it dark or bright" decisions.
Why is my digital output stuck HIGH or LOW?
The threshold potentiometer may need adjustment. Turn it slowly with a small screwdriver while watching the Serial Monitor. The digital output should toggle when light crosses your set threshold.
Can I use 3.3V instead of 5V for VCC?
Yes! The LM393 light sensor module works with both 3.3V and 5V. Use 3.3V when connecting to ESP32 or Raspberry Pi to protect the ADC pins.
My LM393 is out of stock. What are alternatives?
The new LM393B from TI is a drop-in replacement with better specs (38V max, 2kV ESD, 0.37mV offset). See our IC Selection Tool for more alternatives.
Design Smarter Circuits
Now that you know how to use the LM393 light sensor, you might want to design the comparator circuit itself. Our free Hysteresis Calculator helps you calculate threshold voltages, hysteresis width, and resistor values for LM393-based designs.
Try the Calculator →