🛠 Lire un capteur de lumière sur A2

LilyPad Arduino · debutant · Schéma : IMG-lilypad-debutant-3-lilypad-lecture-lumiere.svg — Capteur de lumière LilyPad câblé sur A2, câble FTDI vers un ordinateur affichant les valeurs 0 à 1023.

Objectives

Read a LilyPad light sensor (photoresistor) on an analog input and display values from 0–1023 in the Serial Monitor. You will discover the 10-bit ADC, pads A2 to A5 (the only analog inputs available on standard LilyPad boards), and the use of the FTDI adapter as a serial port.

Hardware

Complete Code

// Lecture du capteur de lumière sur A2 — ADC 10 bits (0..1023)
const uint8_t CAPTEUR = A2; // LilyPad classiques : A2..A5 uniquement !

void setup {
  Serial.begin(9600); // le FTDI relie la carte au moniteur série
}

void loop {
  int brut = analogRead(CAPTEUR); // sombre -> bas, lumineux -> haut (selon câblage)
  Serial.print("Lumiere : ");
  Serial.print(brut);
  Serial.print(" / 1023  ->  ");
  Serial.print(map(brut, 0, 1023, 0, 100));
  Serial.println(" %");
  delay(100);
}

Expected Verification

The Serial Monitor displays approximately ten lines per second. Covering the photoresistor with your finger causes the value to drop towards 0; illuminating it with a flashlight causes it to rise towards 1023. The values vary progressively, without erratic jumps if the snap buttons are properly pressed.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials