Project 02 · Photoresistor

Light as a voltage.

Analog input, a voltage divider, and a screen that brightens with the room. Cover the LDR with a notebook; the bar should collapse.

2 × 45 minA010kΩ dividermap() showcase
LDR voltage divider on a breadboard
LDR between 5V and A0; 10kΩ from A0 to GND. The junction is the signal.

Bright light → LDR resistance falls → analog value climbs toward 1023. Darkness does the opposite. Students should predict the number before covering the cell.

ldr_read.ino
const int sensorPin = A0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  int lightValue = analogRead(sensorPin);
  Serial.println(lightValue);
  delay(200);
}
ldr_bar.pde
import processing.serial.*;

Serial myPort;
int lightLevel = 0;

void setup() {
  size(420, 220);
  myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
  float shade = map(lightLevel, 0, 1023, 20, 240);
  background(shade);
  fill(lightLevel > 500 ? 20 : 240);
  textSize(20);
  text("Light intensity: " + lightLevel, 40, 48);
  fill(0, 100, 255);
  rect(40, 90, map(lightLevel, 0, 1023, 0, 320), 32);
}

void serialEvent(Serial p) {
  String val = p.readStringUntil('\n');
  if (val != null) {
    val = trim(val);
    if (val.matches("\\d+")) lightLevel = int(val);
  }
}
Basic Print the number.
Intermediate Proportional bar.
Advanced Background color tracks light.
Challenge Night-light LED on the Uno, or a timestamped log.
Stuck atCheck
0 foreverOpen circuit / LDR not in the divider
1023 foreverMissing 10kΩ or missing GND
JitterLoose breadboard; average a few samples if needed
Next: Sonar