🛠 Thermomètre de lumière : LDR, seuils et hystérésis

Arduino UNO R3 · intermediaire · Schéma : IMG-uno-r3-intermediaire-1-capteur-lumiere-ldr.svg — pont diviseur LDR + résistance 10 kΩ entre 5 V et GND, point milieu vers A1, LED témoin sur pin 12.

Objectives

Set up a real analog sensor (LDR photoresistor in a voltage divider), calibrate a threshold in real-world conditions using the Serial Monitor, and avoid oscillation around the threshold thanks to hysteresis. Intermediate level: moving from "reading" to "interpreting".

Hardware

Complete code

// LDR sur A1 (pont diviseur avec 10 k), LED témoin sur pin 12.
const uint8_t LDR = A1;
const uint8_t TEMOIN = 12;

// Hystérésis : deux seuils distincts pour éviter le clignotement.
const int SEUIL_OBSCURITE = 300;   // en dessous : nuit
const int SEUIL_JOUR      = 400;   // au-dessus : jour
bool nuit = false;

void setup {
  pinMode(TEMOIN, OUTPUT);
  Serial.begin(115200);
}

void loop {
  int v = analogRead(LDR);          // 0 = noir, 1023 = pleine lumière (selon montage)

  if (!nuit && v < SEUIL_OBSCURITE) {
    nuit = true;
    Serial.print(F("Nuit -> ")); Serial.println(v);
  } else if (nuit && v > SEUIL_JOUR) {
    nuit = false;
    Serial.print(F("Jour -> ")); Serial.println(v);
  }

  digitalWrite(TEMOIN, nuit ? HIGH : LOW);
  Serial.print(F("ldr=")); Serial.println(v);
  delay(100);
}

Expected verification

The Serial Monitor (115200) displays a stable value that increases when you bring the lamp closer and decreases when you cover the LDR with your hand. The indicator LED turns on below ~300 and only turns off above ~400: between the two, no state change — hysteresis is working.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials