🛠 Trace horodatée de capteurs sur le port série
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
- 1 LilyPad SimpleSnap + 3.7 V battery
- 1 LilyPad light sensor on A2
- 1 LilyPad temperature sensor (TMP36) on A3
- FTDI USB-to-serial adapter + serial monitor at 9600 baud
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
- Using delay between measurements: timestamps drift and measurements are lost when the serial monitor saturates; use millis as shown here.
- Forgetting F on strings: with 2 KB of SRAM, every literal stored in RAM counts.
- Baud rate too high (115200) with an old FTDI driver: characters are lost; 9600 is safe.
- >
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinopwm-respiration.inoArduinoluxmetre-couture.inoArduinointerrupteur-tilt.inoArduinoservo-balayage.inoArduino