🛠 Trace horodatée de capteurs sur le port série

LilyPad Arduino · intermediaire · Schéma : IMG-lilypad-intermediaire-3-lilypad-trace-capteurs-serie.svg — Capteurs lumière (A2) et température (A3) reliés par fil conducteur, câble FTDI vers un portable affichant une trace CSV horodatée.

Objectives

Turn the LilyPad into a mini-logger: read two sensors (light on A2, temperature on A3), timestamp each measurement with millis, and print a CSV trace that can be copied into a spreadsheet. This introduces sampling without delay, serial throughput via FTDI, and memory optimization using F.

Hardware

Complete code

// Trace capteurs horodatée en CSV — lumière A2, température A3
const uint8_t LUM = A2;
const uint8_t TMP = A3;
const unsigned long PERIODE = 1000; // 1 mesure par seconde
unsigned long dernierTour = 0;

void setup {
  Serial.begin(9600);                      // via FTDI ; 9600 suffit pour 2 capteurs
  Serial.println(F("millis;lumiere;temp_C")); // en-tête CSV, F économise la SRAM
}

void loop {
  unsigned long maintenant = millis;
  if (maintenant - dernierTour < PERIODE) return; // sans delay : pas de dérive
  dernierTour = maintenant;

  int lum = analogRead(LUM);                 // 0..1023
  int mv  = analogRead(TMP) * 5000L / 1023;  // millivolts (AVcc ≈ 5 V)
  int dixC = mv - 500;                       // TMP36 : 10 mV/°C, 500 mV à 0 °C

  Serial.print(maintenant);  Serial.print(';');
  Serial.print(lum);         Serial.print(';');
  Serial.print(dixC / 10);   Serial.print('.');
  Serial.println(dixC % 10);
}

Expected verification

One line per second in the format “milliseconds;light;temperature”, for example 12000;642;24.8. Pinching the temperature sensor between your fingers causes the value to rise by a few degrees within seconds. The CSV header appears only once at startup; the trace can be copied from the serial monitor to a spreadsheet.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials