IoTSimulator

Photoresistor Sensor: How It Works, Wiring, and Example Code

An LDR light sensor module used for reading brightness levels with analog and digital outputs in Arduino projects.
M
Muhammad Ichsan
Photoresistor Sensor

This article explains the Photoresistor Sensor in a practical way: what it does, how it reacts to light, and how to wire it to Arduino.

It is a good fit when a project needs to react to room brightness, from simple light meters to automatic lamps and night-light builds.

Before the circuit, it helps to see the module first so the body, board layout, and pin side are easier to picture.

Description

The photoresistor sensor module uses a light-dependent resistor, or LDR, to measure brightness. When light hits the sensor, its resistance changes, and the module turns that change into a voltage that Arduino can read.

The same part also appears as a bare single photoresistor, and many module boards are sold as KY-018 style boards. The bare LDR is the raw sensor, while the module adds the resistors and output pins that make Arduino wiring easier.

Photoresistor sensor product view
Photoresistor sensor product view

That makes it a good beginner part for night lights, brightness meters, automatic lamps, and other small projects that react to room light.

Many modules give you both an analog output and a digital output. The analog output gives you the raw light reading, while the digital output acts like a threshold switch.

Photoresistors are better for coarse light sensing than precise lux measurement. They are not polarized, so the two sensor legs can connect either way.

Their response is slower than phototransistors or photodiodes, which is fine for a night light or room brightness monitor, but not ideal for fast light changes.

Different parts can behave a little differently, so the readings often need a quick calibration step. The module is useful because it shows a clear trend, not because every unit reads exactly the same.

Why KY-018 Exist

Single photoresistor and KY-018 module comparison
Single photoresistor and KY-018 module comparison

The KY-018 module exists to make the bare photoresistor easier to use with Arduino. The raw LDR only gives resistance change, so you still need a resistor divider and a clean way to read the signal.

The module adds that supporting circuit for you. That gives you a ready-to-use analog output, a digital threshold output, and pins that plug into a breadboard without extra guesswork.

The bare photoresistor still makes sense when you want to learn the circuit or build your own divider from scratch. The module makes more sense when you want faster setup, cleaner wiring, and less trial and error.

Features

Here are the main things to know about the Photoresistor Sensor:

FeatureWhat it means
Analog outputLets the Arduino watch brightness as a changing value.
Digital threshold outputCan trigger when the light crosses a set level.
Adjustable sensitivityThe onboard potentiometer changes the trigger point.
Simple wiringUsually only needs power, ground, and one or two signal pins.
Beginner friendlyGood for light meters and automatic lamp demos.
Voltage divider behaviorWorks naturally with analogRead().
Coarse sensingBest for light and dark changes, not precise lux values.
Non-polarized sensorThe bare LDR legs can be connected either way.
Slower responseBetter for room sensing than rapid light changes.

The most useful thing about the module is that it gives you both a live reading and a simple on or off light condition. That makes it flexible enough for beginner demos and small control projects.

If you need a more exact light measurement, a phototransistor or photodiode is usually the better choice. For automatic lamps, room sensing, and simple threshold logic, the photoresistor is still practical.

How Does It Work?

The LDR changes resistance depending on how much light reaches it. In bright conditions the resistance drops, and in darker conditions it rises. The module turns that change into an analog voltage on AO.

The Arduino reads that voltage with analogRead(). If the module also uses a comparator, the DO pin can switch based on a threshold set by the onboard potentiometer or simulator control.

How a photoresistor sensor works
How a photoresistor sensor works

Analog Reading

The analog output is the best choice when you want to see how the brightness changes in real time or map the reading to another output like an LED or servo. It gives you a smooth value, so the sketch can respond gradually instead of only jumping between two states.

Digital Trigger

The digital output is better when you only need a clean yes or no answer, such as turning on a lamp when it gets dark. It behaves more like a light switch, which is useful when the project only cares about crossing a threshold.

Photoresistor Sensor Pinout

The module exposes four pins: VCC, GND, DO, and AO. The analog pin is the one you use when you want the full light reading, while the digital pin is there for threshold-based behavior.

PinMeaningBeginner note
VCCPower supplyUsually connect to 5V.
GNDGroundMust share ground with the Arduino.
AOAnalog outputUse this for the raw brightness reading.
DODigital threshold outputUse this for an on/off light trigger.

Arduino With Photoresistor Sensor

This circuit preview shows a basic Arduino Uno connection with the photoresistor module. It is a good starting point because the analog output can be tested immediately and the threshold output can be added later if needed.

SCL
SDA
AREF
GND.1
D13
D12
D11
D10
D9
D8
D7
D6
D5
D4
D3
D2
D1
D0
IOREF
RESET
3V3
5V
GND.2
GND.3
VIN
A0
A1
A2
A3
A4
A5
VCC
GND
DO
AO

Pin Connection

The connection table below follows the same wiring shown in the circuit preview. The analog pin is the main one to remember, and the digital pin is optional if you want threshold behavior.

Pin Connection Map
Sensor Pin
VCC
Arduino Pin
5V
Explanation

Supplies power to the sensor module.

Sensor Pin
GND
Arduino Pin
GND
Explanation

Shared ground reference for stable readings.

Sensor Pin
AO
Arduino Pin
A0
Explanation

Carries the analog light reading into Arduino.

Sensor Pin
DO
Arduino Pin
D2
Explanation

Optional digital trigger output for threshold use.

If the reading looks inverted, that is usually not a wiring failure. It means the module is changing in the opposite direction from what you expected, so the sketch may need a small adjustment to the threshold or mapping logic.

Arduino Code

This example reads the analog light value and prints it to Serial Monitor. It is the easiest first test for a photoresistor sensor because you can see the number change as the light changes.

C++ Source
1
2
3
4
5
6
7
8
9
10
11
12

About analogRead()

The analogRead() function turns the changing voltage into a number the sketch can use. On an Arduino Uno, the result usually ranges from 0 to 1023, which makes it easy to compare against a threshold or remap into another output.

How The Code Works, Part By Part

Let's break the sketch into smaller pieces so the light-reading flow is easier to understand and modify later.

Setup

The setup block prepares Serial Monitor so you can watch the sensor values while you test the module.

C++ Source
1
2
3
4
5

Read Signal

The Arduino reads the analog output from the sensor. The returned value changes with the light level around the module.

C++ Source
1

Print Value

Printing the value helps you understand the normal range first. After that, it is easier to choose a threshold or convert the reading into another behavior.

C++ Source
1
2

Repeat

The short delay keeps the serial stream readable while the sketch continues to monitor the sensor.

C++ Source
1

Wrapping Up

The Photoresistor Sensor is useful when you want Arduino to react to light in the room.

The analog pin gives you the raw brightness trend, while the digital pin gives you a quick threshold signal. That means you can start with a basic light meter and later reuse the same module for an automatic lamp trigger or a dark-room alert.

Once you understand the LDR response, the voltage divider, and the threshold setting, the module becomes easy to reuse in small projects. It teaches a clear idea and gives you a visible result right away.

New

Wait! We're building more...

Our laboratory is currently preparing a lot of exciting new projects using Photoresistor Sensor. Stay tuned for the upcoming massive update!

Projects Using Photoresistor Sensor

Explore practical implementations

No official projects found for this component yet.

Muhammad Ichsanul Fadhil
About Writer

Muhammad Ichsanul Fadhil

"I'm a developer and hardware enthusiast with a passion for IoT. I love experimenting with new components and writing down everything I learn to help others build their own projects."

Share this article

Share it with your favorite channel.