🛠 Lire un capteur de lumière sur A2
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
- 1 LilyPad SimpleSnap + 3.7 V battery
- 1 LilyPad Light Sensor
- Conductive thread or snap buttons
- FTDI adapter + Arduino IDE (Serial Monitor set to 9600 baud)
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
- Using A0 or A1: Standard LilyPad boards (Main/Simple/SimpleSnap) only expose A2 to A5; nothing happens on these pins.
- Serial Monitor set to a baud rate other than 9600: Unreadable characters.
- Misunderstanding the dark/light direction: Depending on the sensor wiring (voltage divider towards + or towards −), the value increases or decreases with light.
- >
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinopwm-respiration.inoArduinoluxmetre-couture.inoArduinointerrupteur-tilt.inoArduinoservo-balayage.inoArduino