Classroom Public page

Lab 11.1: Serial Debug

442 words

~90 min. Turn your week-9 photoresistor sketch into a properly-instrumented debug log. Add a calibration mode triggered by a button press.


Goal: build the Serial-debug habit; make every future sketch self-explaining via Serial output

Estimated time: 90 minutes

Prerequisites: lab 9.1 (photoresistor); lab 8.1 (button)

Steps

Step 1: Wire the circuit (15 min)

Combine lab 9.1's photoresistor on A0 + lab 8.1's button on pin 2 + an LED on pin 9. Same breadboard, all three components

Step 2: Baseline sketch with Serial output (15 min)

int darkThreshold = 200;
int brightThreshold = 800;

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(9, OUTPUT);
  Serial.begin(9600);
  Serial.println("=== HW-101 Lab 11.1 ===");
  Serial.print("Initial dark threshold: ");
  Serial.println(darkThreshold);
  Serial.print("Initial bright threshold: ");
  Serial.println(brightThreshold);
}

void loop() {
  int reading = analogRead(A0);
  int brightness = map(reading, darkThreshold, brightThreshold, 255, 0);
  brightness = constrain(brightness, 0, 255);
  analogWrite(9, brightness);

  Serial.print("Reading: ");
  Serial.print(reading);
  Serial.print("  Brightness: ");
  Serial.println(brightness);

  delay(250);
}

Upload. Open Serial Monitor at 9600 baud. You should see the startup messages, then a stream of readings

Step 3: Add a calibration trigger (30 min)

When the button is pressed (and held), capture the current reading as the new dark threshold:

void loop() {
  int reading = analogRead(A0);
  int buttonState = digitalRead(2);

  if (buttonState == LOW) {
    // button held: capture current reading as dark threshold
    darkThreshold = reading;
    Serial.print("CALIBRATE: dark threshold set to ");
    Serial.println(darkThreshold);
    delay(500);  // simple debouncing for hold; let go before holding again
  }

  int brightness = map(reading, darkThreshold, brightThreshold, 255, 0);
  brightness = constrain(brightness, 0, 255);
  analogWrite(9, brightness);

  Serial.print("Reading: ");
  Serial.print(reading);
  Serial.print("  Threshold dark: ");
  Serial.print(darkThreshold);
  Serial.print("  Brightness: ");
  Serial.println(brightness);

  delay(250);
}

Upload. Test: cover the photoresistor (simulate dark); hold the button; the threshold updates to that dark value. Now the LED behavior adapts to your local conditions

Step 4: Add a tabular debug format (15 min)

For data you might want to graph later, use comma-separated values:

Serial.print(millis());
Serial.print(",");
Serial.print(reading);
Serial.print(",");
Serial.print(darkThreshold);
Serial.print(",");
Serial.println(brightness);

Copy the Serial output to a text file; rename to .csv; open in a spreadsheet. Now you have a time-series of your sensor + control behavior

Step 5: Document (15 min)

In your lab notebook: paste a 30-second sample of your Serial output. Note the format. Describe what happens during a calibration press. Reflect on what other state you might want to log in future sketches

Expected output

  • Working photoresistor + button + LED sketch with debug logging
  • Calibration mode triggered by button
  • Optional CSV-formatted output for graphing

Common pitfalls

  • Forgetting Serial.begin() in setup: nothing prints until you call this. The baud rate must match what the Monitor uses (or what your Putty / minicom session uses)
  • Slow Serial output blocking loop responsiveness: at 9600 baud, each Serial.println(reading) takes ~5-10 ms. If you print every loop, that's significant time. Bump to 115200 baud for less overhead. Or print less often
  • Forgetting to constrain map() output: map() can return values outside the output range if the input is outside the input range. constrain() clamps to a safe range

Stretch (optional)

  • Add a "modes" pattern: short button-press = toggle a mode; long button-press = calibrate. State machine
  • Pipe the Serial output to a Python script that plots it in real time using matplotlib. Bridge the embedded world to data analysis
  • Add timestamps via millis() and have the Arduino "remember" the last 30 seconds of readings (store in an array, print on demand). Now you have a tiny embedded data logger

Lab 11.1 v0.1. Every future sketch from here has Serial output. Serial is your standard debugging fixture.