All courses

Arduino C++ · Lesson 34 of 40

Show and record meaningful data

Measurement and Interfaces

Keep display updates separate from sensing

A display can be slower than a sensor. Sample at one interval and update the screen at another. Start with a fixed line such as Temp: --.- C, then overwrite changed fields. Repeatedly clearing a character LCD can cause flicker. A common 128 × 64 monochrome framebuffer uses 1024 bytes before library overhead, which is significant on a 2 KB SRAM Uno R3.

For an SSD1306 OLED or an I2C LCD backpack, install the library that matches the controller and address. Run that library's example before combining it with your application. Screens that look alike may use different controllers or backpack pin mappings.

Use serial CSV before adding storage

Begin logging to Serial Monitor or a computer capture tool. This complete sketch prints time and A0 raw readings every 200 ms; use the potentiometer circuit from calibration.

Arduino C++
unsigned long lastLog = 0;

void setup() {
  Serial.begin(9600);
  Serial.println(F("time_ms,raw_adc"));
}

void loop() {
  const unsigned long now = millis();
  if (now - lastLog < 200) return;
  lastLog = now;
  Serial.print(now);
  Serial.print(',');
  Serial.println(analogRead(A0));
}

Keep every row in the same column order. Include units in headers, and record a validity field when a real sensor can fail. A timestamp from millis() is time since reset, not calendar time. For calendar dates use a configured real-time clock or a trustworthy external time source.

SD storage adds SPI wiring, chip-select management, memory overhead, and potentially long writes. Use a level-compatible module, check open/write results, and document how data is closed or flushed. Disconnecting power during a write can corrupt a file. Do not write every sample to EEPROM as a substitute for a logging medium.

Exercise: Capture one minute of output. Confirm the expected approximate row count, monotonic timestamps until a reset/wrap, and consistent column count. Document what should happen if a display or storage device is absent.

Official references for further study

A useful reminder

Clean Code: Use descriptive constants instead of magic numbers

Instead of calling digitalWrite(13, HIGH) directly in your code, define descriptive constants like const int LED_PIN = 13; at the top of your file. This makes your code self-documenting and extremely easy to update if you change your wiring.