🛠 Capteur de lumière avec seuil et hystérésis
Objectives
Make serious use of the onboard light sensor (an LDR read on channel 5 of the analog multiplexer): scaling, moving average filtering, and hysteresis to prevent flickering around the threshold. This is the basic pattern for any analog sensor alert system, and the Esplora handles it without a single wire.
Materials
- Arduino Esplora + micro-USB cable.
- Esplora library.
- Optional: a flashlight to test the threshold.
Complete code
/* Veilleur de lumiere : la LED RVB passe au vert le jour, au rouge la nuit.
- moyenne glissante sur 8 mesures (le mux impose des lectures sequentielles)
- hysteresis de 60 points pour ne pas osciller autour du seuil.
NB : le capteur est une LDR, 1023 = tres eclaire, 0 = obscurite. */
#include <Esplora.h>
const int SEUIL_ALLUMAGE = 350; // sous ce niveau -> mode nuit
const int SEUIL_EXTINCTION = 410; // au-dessus -> mode jour (hysteresis)
const int N = 8; // taille de la fenetre de moyenne
unsigned long mesures[N];
size_t idx = 0;
bool jour = true;
unsigned long moyenne {
unsigned long s = 0;
for (size_t i = 0; i < N; i++) s += mesures[i];
return s / N;
}
void setup {
Serial.begin(9600);
for (size_t i = 0; i < N; i++) mesures[i] = Esplora.readLightSensor;
}
void loop {
mesures[idx] = Esplora.readLightSensor;
idx = (idx + 1) % N;
unsigned long lum = moyenne;
if (jour && lum < SEUIL_ALLUMAGE) {
jour = false; // bascule en nuit
Esplora.writeRGB(255, 0, 0);
Esplora.tone(440, 120); // bip court sur le buzzer (D6)
} else if (!jour && lum > SEUIL_EXTINCTION) {
jour = true; // retour au jour
Esplora.writeRGB(0, 255, 0);
}
Serial.print("lum="); Serial.print(lum);
Serial.println(jour ? " JOUR" : " NUIT");
delay(100);
}
Expected verification
In daylight, the LED is green. Cover the sensor with your finger: below ~350, the LED turns red with a short beep. Release it halfway: the LED stays red until 410 (hysteresis) — without this gap, it would toggle frantically. The moving average smooths out spikes (a hand clap in front of the sensor no longer triggers a clear measurement error).
Common errors
- Identical threshold for rising and falling edges: permanent oscillation around the threshold; the hysteresis gap (here 60 points) is mandatory on a noisy sensor.
- Average recalculated on an unfilled window: initialize the buffer with real measurements in
setup, otherwise the first cycles are skewed by zeros. - Forgetting the LDR nature: the response is logarithmic with respect to actual illuminance; a threshold in ADC value is not linear in lux — readjust your thresholds for YOUR environment.
- Buzzer whistling continuously: call
Esplora.tone(f, duration)with a duration, or remember to usenoTone; without a duration, the sound remains stuck. - Reading other sensors in between without delay: each change of channel on the multiplexer has a settling time; the library handles this, but do not recreate the reading with a direct
analogRead(A4)without respecting this delay.
Other tutorials for this board
Code examples for this board
joystick-couleur.inoArduinojeu-boutons.inoArduinoslider-luminosite.inoArduinothermometre-couleur.inoArduinosonometre.inoArduinoaccelerometre-tilt.inoArduino