A photoresistor's resistance drops as light gets brighter, and a microcontroller measures voltage, not resistance, so you pair it with a fixed 10 kΩ resistor as a voltage divider: 5V to one leg, the other leg to A0 and to the 10 kΩ resistor, and the resistor's far end to GND. analogRead(A0) then rises with the light, from near 0 in the dark to 900 or more in bright light.
That is the circuit the 30 Days Lost in Space kit builds on Day 6, and everything below uses it. If analog pins are new to you, the analog input guide and what a microcontroller is are good first reads.
What is a Photoresistor?
A photoresistor, also called an LDR (Light Dependent Resistor), is a disc of cadmium-sulfide semiconductor whose resistance depends on light. Photons striking the material knock electrons loose, and more free electrons make it a better conductor. The squiggly line on its face is the light-sensitive track.
- Bright light: low resistance (a few hundred Ω to a few kΩ)
- Darkness: very high resistance (up to about 1 MΩ)
Your kit's sensor is a GL5528-type LDR: roughly 10 to 20 kΩ in dim room light, over 1 MΩ in the dark, and most sensitive to green light (about 540 nm), close to the peak sensitivity of the human eye.
Wiring It to the HERO Board
analogRead() measures voltage, so the photoresistor becomes the top half of a voltage divider:
5V ---[Photoresistor]--- A0 ---[10kΩ]--- GND
| From | To | |------|----| | HERO Board 5V | Photoresistor, either leg | | Photoresistor, other leg | A0 and one end of the 10 kΩ resistor | | 10 kΩ resistor, other end | HERO Board GND |
On a breadboard, put the photoresistor's second leg, the resistor's first leg and the wire to A0 in the same 5-hole column so they share one connection. The photoresistor has no polarity: either leg can face 5V.
The Divider Math
The voltage at A0 depends on the ratio of the two resistances:
V(A0) = 5V × 10k / (R_LDR + 10k)
Worked through with real numbers:
| Light level | R_LDR | V(A0) | analogRead | |-------------|-------|-------|------------| | Bright | about 1 kΩ | 4.5V | about 930 | | Room light | about 10 kΩ | 2.5V | about 512 | | Dark | about 1 MΩ | 0.05V | about 10 |
More light, lower LDR resistance, A0 pulled toward 5V, higher reading.
Example 1: Read the Light Level
Upload this, open the Serial Monitor at 9600 baud, and cover the sensor with your hand:
// Read the light sensor and print it, 10 times a second.
const int SENSOR_PIN = A0; // the photoresistor and the 10k resistor meet here
void setup() {
Serial.begin(9600);
}
void loop() {
int light = analogRead(SENSOR_PIN); // 0 (dark) to 1023 (bright)
Serial.println(light);
delay(100);
}
The number drops as your hand blocks the light. Tools > Serial Plotter in the Arduino IDE draws the same numbers as a live graph, which makes slow changes easy to see.
Typical Readings
| Condition | analogRead value | |-----------|-----------------| | Direct sunlight | 900 to 1023 | | Indoor light | 400 to 700 | | Dim room | 100 to 300 | | Covered or dark | 0 to 50 |
Treat these as ballpark: every LDR is a little different, and so is every room.
Calibrate, Don't Hardcode
Because no two sensors or rooms match, the course never relies on one fixed number for "bright". The Day 6 lesson records the darkest and brightest readings the sensor has actually seen, then uses map() to turn each new reading into a blink speed inside that range. Move the lander to a new room and it recalibrates instead of needing new code. The full sketch is in the map() guide.
Example 2: A Night Light That Doesn't Flicker
The obvious night light, "LED on below 200", flickers at dusk: the reading wobbles around 200 and the LED chatters on and off. Use two thresholds instead, a gap called hysteresis. Keep the divider on A0 and add an LED with a 220 Ω resistor on pin 9:
// Night light that doesn't flicker at dusk: on below 200, off only above 260.
const int SENSOR_PIN = A0; // photoresistor on the 5V side, 10k to GND
const int LED_PIN = 9;
const int DARK = 200; // turn the lamp on below this
const int LIGHT = 260; // turn it off only above this
bool lampOn = false;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
int light = analogRead(SENSOR_PIN);
if (!lampOn && light < DARK) {
lampOn = true;
Serial.println("Lamp on");
} else if (lampOn && light > LIGHT) {
lampOn = false;
Serial.println("Lamp off");
}
digitalWrite(LED_PIN, lampOn ? HIGH : LOW);
delay(100);
}
Once on, the lamp waits for the reading to climb past 260 before it turns off, so small wobbles do nothing. We tested it in our simulator: dark turned it on, a middle light level kept it on, bright light turned it off, and the same middle level then kept it off. Adjust 200 and 260 to your room using Example 1.
If you build a night light, point the LED away from the sensor. Otherwise the lamp lights up the sensor, the sketch decides it's daytime, the lamp turns off, and it starts over.
Example 3: Work Out the Resistance
Run the divider formula backwards and the sketch can tell you the photoresistor's resistance at any moment:
// What is the photoresistor's resistance right now? Work back from the divider.
const int SENSOR_PIN = A0;
const float FIXED_OHMS = 10000.0; // the 10k resistor between A0 and GND
void setup() {
Serial.begin(9600);
}
void loop() {
int reading = analogRead(SENSOR_PIN);
if (reading > 0) {
float ldrOhms = FIXED_OHMS * (1023 - reading) / reading;
Serial.print(reading);
Serial.print(" = ");
Serial.print(ldrOhms / 1000.0, 1);
Serial.println(" kOhm");
} else {
Serial.println("0 = darker than this divider can measure");
}
delay(500);
}
R_LDR = 10k × (1023 - reading) / reading comes straight from the formula above. It's handy for checking a sensor and for choosing the fixed resistor.
Choosing the Fixed Resistor
A divider is most sensitive when the fixed resistor equals the photoresistor's resistance at the light level you care about. With a GL5528-type sensor, 10 kΩ puts that sweet spot at ordinary room light, which is why the course uses it. For a sensor that works in bright daylight, a smaller resistor (around 1 kΩ) spreads the bright end of the scale; for a very dark room, a larger one (100 kΩ) spreads the dark end. Use Example 3 to measure your sensor in the light you care about.
Smoother Readings
Readings jitter by a few counts. Average a handful:
int readLight() {
long total = 0;
for (int i = 0; i < 10; i++) {
total += analogRead(A0);
delay(2);
}
return total / 10; // the average of 10 readings
}
Good to Know
- Not polarized: either leg can face 5V.
- It's slow: a cadmium-sulfide cell takes tens of milliseconds to react. Perfect for light levels, useless for detecting fast flashes.
- Modules: the KY-018 photoresistor module in the 37-in-1 Sensor Kit has the fixed resistor built onto its little board, so it has three pins (S, + and -) and its S pin goes straight to an analog pin.
Where You'll Use It in 30 Days Lost in Space
| Mission | What it does | |---------|--------------| | Day 6 | The lander's "solar panel": your first analog sensor, with live calibration and map() | | Day 7 | Charges a simulated battery by adding up light readings | | Day 9 | Shows the charge level as a color on the RGB LED |
Common Mistakes
- Reading stuck near 0 or 1023: the 10 kΩ half of the divider is missing or miswired. With only the photoresistor connected there's no divider, and A0 floats or sits at 5V.
- Readings go down when the light goes up: the photoresistor and the fixed resistor are swapped. With the photoresistor on the GND side the math flips, and dark reads high. Either swap them back or flip the scale with
map(x, 0, 1023, 1023, 0). - Using a digital pin:
digitalRead()only reports HIGH or LOW. Light levels need A0 to A5 andanalogRead(). - Hard-coded thresholds: a value that works at your desk fails by the window. Measure first with Example 1, or calibrate as Day 6 does.
- Covering it doesn't reach 0: normal. Room light leaks in, and even "dark" leaves a few counts.
Frequently Asked Questions
What resistor do I use with a photoresistor?
10 kΩ is the standard choice, and it's what the 30 Days kit uses. It gives the widest swing of readings in ordinary indoor light. Choose smaller for bright daylight and larger for dim rooms (see Choosing the Fixed Resistor).
Does a photoresistor have polarity?
No. It's a resistor, so either leg can go to 5V. Only the order in the divider matters: photoresistor on the 5V side means brighter light reads higher.
Why does my reading drop when the light gets brighter?
The photoresistor is on the GND side of the divider and the 10 kΩ resistor on the 5V side. That works too, it just reads backwards. Swap the two parts, or flip the number in code.
Can a photoresistor measure lux?
Not accurately. Its response varies from part to part and changes with temperature, so treat its readings as "darker or brighter", not as a light meter. To get lux, calibrate it against a real light meter, or use a digital light-sensor chip made for measuring.
Analog or digital pin for a photoresistor?
Analog: A0 to A5 with analogRead(), which returns 0 to 1023. A digital pin only sees HIGH or LOW.