~90 min. Wire a photoresistor in a voltage divider. Read with analogRead. Print to Serial Monitor. Use the reading to set LED brightness via PWM.
Goal: read a continuous analog value; understand the relationship between physical phenomenon (light) and the number analogRead returns
Estimated time: 90 minutes
Prerequisites: lab 7.2 (external LED); lab 8.1 (digital input)
Steps
Step 1: Wire the photoresistor in a voltage divider (15 min)
Photoresistor is in your kit. Resistance varies from ~1 kΩ (bright light) to ~100 kΩ (dark). Pair it with a 10 kΩ fixed resistor:
- +5V → photoresistor → tie-strip (this is Vout; goes to A0) → 10 kΩ → GND
- A0 (Arduino analog input pin 0) reads Vout
When bright: photoresistor is low resistance → Vout is high (most of 5 V drops across the 10 kΩ) When dark: photoresistor is high resistance → Vout is low
Step 2: Read with analogRead (15 min)
Write a sketch:
void setup() {
Serial.begin(9600);
}
void loop() {
int reading = analogRead(A0);
Serial.println(reading);
delay(200);
}
Upload. Open Serial Monitor at 9600 baud. You should see a number in the range 0-1023 update every 200 ms. Cover the photoresistor with your hand: number drops. Hold it near a lamp: number rises
Step 3: Calibrate (15 min)
Cover the photoresistor completely; record the "dark" reading (typically ~100-300) Hold it under bright room light; record the "bright" reading (typically ~700-900)
These are your calibration endpoints. Note that "completely dark" is hard to achieve in a normally-lit room; the calibration is approximate
Step 4: Drive an LED based on light (30 min)
Wire an LED + resistor on pin 9 (PWM-capable). Modify the sketch to set LED brightness inversely to light:
void setup() {
pinMode(9, OUTPUT);
Serial.begin(9600);
}
void loop() {
int reading = analogRead(A0);
int brightness = map(reading, 200, 800, 255, 0);
// dark (low reading) → bright LED (255); bright (high reading) → off LED (0)
brightness = constrain(brightness, 0, 255);
analogWrite(9, brightness);
Serial.print("reading: ");
Serial.print(reading);
Serial.print(" brightness: ");
Serial.println(brightness);
delay(100);
}
Upload. Move your hand over the photoresistor; the LED brightens. Move away; LED dims. This is a basic feedback control loop
Step 5: Test the bounds (15 min)
Try in a dark room: the LED should be fully on. Try outside or under bright sunlight: the LED should be off. Mid-room: somewhere in between
If the bounds don't match your calibration, adjust the map() arguments. The first two arguments to map() are the input range; the second two are the output range
Expected output
- Working light-following LED
- Serial Monitor showing reading and brightness in real time
- Notebook entry with calibration values
Common pitfalls
- Reading does not change with light: check wiring. The photoresistor must be in series with the fixed resistor; A0 must be between them
- Reading saturates at 0 or 1023: the photoresistor might be wrong-value for your fixed resistor. Try a different fixed-resistor value (e.g., 4.7 kΩ or 22 kΩ) to shift the divider's operating point
- LED behavior is "all or nothing": verify the LED is on a PWM-capable pin (3, 5, 6, 9, 10, 11 on R4 Minima). If you used pin 4 or 7 (non-PWM), analogWrite produces digitalWrite-like behavior
Stretch (optional)
- Plot the reading over a day (every 5 minutes; manually or with a recording sketch). Watch the natural light cycle
- Add a button that lets you re-calibrate the dark threshold on demand. The button captures the current reading as the new dark endpoint
- Use the light reading to trigger an action: when the room gets dark, turn on a "night light" LED at moderate brightness. When the room is bright, turn it off. This is a simple smart-light prototype
Lab 9.1 v0.1. First analog input. The pattern (read analog; map to control; act) recurs for the rest of the course.