🛠 Capteur de lumière avec seuil et hystérésis

Arduino Esplora — Board Fiche · intermediaire · Schéma : IMG-esplora-intermediaire-1-esplora-capteur-lumiere-hysteresis.svg — Courbe d'hystérésis superposée à l'Esplora, avec la LED RVB verte en mode jour et rouge en mode nuit et les deux seuils annotés.

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

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

  1. Identical threshold for rising and falling edges: permanent oscillation around the threshold; the hysteresis gap (here 60 points) is mandatory on a noisy sensor.
  2. Average recalculated on an unfilled window: initialize the buffer with real measurements in setup, otherwise the first cycles are skewed by zeros.
  3. 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.
  4. Buzzer whistling continuously: call Esplora.tone(f, duration) with a duration, or remember to use noTone; without a duration, the sound remains stuck.
  5. 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

← Back to the board 🛠 All tutorials