luxmetre.ino

ESP8266 (ESP-12E module) · File : luxmetre.ino · Language : Arduino · Use case : Mesurer la luminosité avec l'ADC 1 V de l'ESP8266

The ESP8266 ADC is atypical: full scale 1 V (0.1 V without the NodeMCU divider). The sketch averages 16 reads, converts to volts then rough lux, and categorizes the ambience.

Hardware required

An ESP-12E module (ESP8266EX, 2.4 GHz Wi-Fi) mounted on a NodeMCU / Wemos D1 mini board, powered over micro-USB. 1 photoresistor (LDR) + 10 kΩ resistor as a divider, or a 3-pin light sensor module.

Wiring

LDR/10 kΩ divider between 3.3 V and GND, wiper to A0. On a bare NodeMCU (no divider), add a 10 kΩ/20 kΩ divider stage.

How it works

analogRead returns 0-1023 scaled to the converter full scale; software averaging smooths noise. The lux conversion is linear and strictly false — it serves as a relative level. In the Arduino IDE, install the « esp8266 » package, select « NodeMCU 1.0 (ESP-12E Module) » then Upload (hold GPIO0 LOW if flashing fails). The A0 ADC reads 0-1 V (NodeMCU divider: 0-3.3 V).

Going further

Trigger an LED below a threshold, or send the timestamped level to a server every minute.

luxmetre.ino

// Photorésistance sur A0 — l'ADC de l'ESP8266 lit 0 a 1 V seulement !
// Sur NodeMCU, le diviseur embarque ramene la pleine echelle a ~3,3 V.
const unsigned long PERIODE = 500;
unsigned long precedent = 0;

void setup() {
  Serial.begin(115200);
  analogReadResolution(10);      // 0..1023 par defaut
}

void loop() {
  if (millis() - precedent < PERIODE) return;
  precedent = millis();

  long brut = 0;
  for (int i = 0; i < 16; i++) brut += analogRead(A0);   // moyenne 16
  brut /= 16;

  float volts = brut * (3.3 / 1023.0);     // apres diviseur NodeMCU
  float lux = volts * 500.0;               // grossier : a calibrer au LUXmetre
  Serial.printf("brut=%ld  %.2f V  ~%.0f lux  %s\n", brut, volts, lux,
                lux < 30 ? "NOIR" : lux < 300 ? "interieur" : "lumineux");
}

← Back to the board