Build a pocket Wi-Fi lux meter with a Wemos D1 Mini, a BH1750 light sensor and a 0.96 inch OLED that shows light level, min/max and a room rating, plus a live phone web page.
Need it ready-made? Buy Ready Project
Is your study desk bright enough? Does the plant corner get enough light? Is the workshop bench lit well for soldering? Our eyes adapt so well that we are bad at judging light. This small Wemos D1 Mini lux meter measures light in lux with a BH1750 sensor, shows the reading on a 0.96 inch OLED with a simple rating (Dark, Dim, Living room, Reading, Office / desk, Bright, Direct sun) and keeps the minimum and maximum values. It also hosts a live web page, so you can place the sensor in one spot and read it on your phone from across the room.
It is an easy one-evening build: only three modules on one I2C bus and one push button. The BH1750 is read directly with the Wire library using the commands from its datasheet, so you only need the Adafruit SSD1306 and GFX libraries for the display.

How It Works
- BH1750 light sensor: a digital sensor with a photodiode, amplifier and 16-bit ADC inside. Its spectral response is close to the human eye, so the reading is real lux, from 1 to about 65,000 lx. The sketch powers it on and puts it in continuous high-resolution mode (1 lx steps, about 120 ms per reading).
- Reading the value: every 500 ms the D1 Mini asks the sensor for two bytes over I2C. The datasheet formula is
lux = raw count / 1.2. If the sensor does not answer, the code shows an error and tries to wake it up again. - Rating and min/max: the lux value is compared with common indoor light levels (for example about 300–500 lx for reading and 500–1000 lx for desk work) to show a word rating. The lowest and highest readings since the last reset are stored as min and max.
- Button: a short press toggles HOLD, which freezes the reading so you can move the meter and still read it. A long press (more than 1 second) resets min and max.
- Wi-Fi web page: the D1 Mini joins your Wi-Fi and serves a page at
http://luxmeter.local(or its IP address). The page asks/dataonce per second for a small JSON reply and shows the lux value, rating, min/max and a log-scale bar, with HOLD and RESET buttons.
Components Required
- Wemos D1 Mini (ESP8266) board – 1
- BH1750 (GY-302) digital light sensor module – 1
- 0.96 inch SSD1306 I2C OLED display, 128×64, 4-pin – 1
- 6 mm tactile push button – 1
- Breadboard (400 points) and male-to-male jumper wires – 1 set
- Micro-USB cable and 5 V USB supply or power bank – 1
Wiring Table
| Module pin | Wemos D1 Mini pin | Notes |
|---|---|---|
| BH1750 VCC | 3V3 | The module also works on 5 V, but 3.3 V keeps the I2C lines at ESP8266 level |
| BH1750 GND | GND | Common ground |
| BH1750 SCL | D1 (GPIO5) | I2C clock, shared with the OLED |
| BH1750 SDA | D2 (GPIO4) | I2C data, shared with the OLED |
| BH1750 ADDR | GND | Address 0x23 (tie to 3V3 for 0x5C and change BH1750_ADDR in the code) |
| OLED VCC | 3V3 | Check the pin order on your module: some have GND first, some VCC first |
| OLED GND | GND | Common ground |
| OLED SCL | D1 (GPIO5) | Same I2C bus |
| OLED SDA | D2 (GPIO4) | Same I2C bus, address 0x3C |
| Push button leg 1 | D5 (GPIO14) | Internal pull-up is enabled in the code |
| Push button leg 2 | GND | Pressed = LOW |
| Micro-USB | 5 V USB supply | Powers the board and both modules through the onboard 3.3 V regulator |
Connection Diagram
The diagram near the top of this post shows the full layout. The drawing below lists every connection pin by pin and matches the wiring table and the code.
Code
Install the ESP8266 boards package in the Arduino IDE, select LOLIN(WEMOS) D1 R2 & mini, and install Adafruit SSD1306 and Adafruit GFX from the Library Manager. Replace YOUR_WIFI_SSID and YOUR_WIFI_PASSWORD with your own network details before uploading. The meter still works on the OLED without Wi-Fi.
/*
Wemos D1 Mini Wi-Fi Lux Meter with BH1750 and OLED
Arduino Project Hub - https://ardiunoprojecthub.online/
What it does:
- Reads light level in lux from a BH1750 sensor (I2C, no extra library).
- Shows lux, a light-level rating, min and max on a 0.96" SSD1306 OLED.
- A push button on D5: short press = HOLD/RUN, long press (1 s) = reset min/max.
- Serves a small web page at http://luxmeter.local (or the IP address)
that refreshes every second from /data (JSON).
Wiring (see the wiring table in the post):
BH1750 VCC -> 3V3, GND -> GND, SCL -> D1, SDA -> D2, ADDR -> GND (0x23)
OLED VCC -> 3V3, GND -> GND, SCL -> D1, SDA -> D2 (address 0x3C)
Button one leg -> D5, other leg -> GND (internal pull-up used)
Libraries: Adafruit SSD1306, Adafruit GFX (Library Manager).
Board: "LOLIN(WEMOS) D1 R2 & mini" from the ESP8266 boards package.
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// ---------- Wi-Fi settings: replace with your own ----------
const char* WIFI_SSID = "YOUR_WIFI_SSID";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";
// ---------- Pins and addresses ----------
const uint8_t BUTTON_PIN = D5; // GPIO14, button to GND
const uint8_t BH1750_ADDR = 0x23; // ADDR pin to GND (0x5C if ADDR to 3V3)
const uint8_t OLED_ADDR = 0x3C;
// BH1750 commands (from the datasheet)
const uint8_t BH_POWER_ON = 0x01;
const uint8_t BH_RESET = 0x07;
const uint8_t BH_CONT_HRES = 0x10; // continuous, 1 lx resolution, ~120 ms
Adafruit_SSD1306 display(128, 64, &Wire, -1);
ESP8266WebServer server(80);
float luxNow = 0, luxMin = 0, luxMax = 0;
bool holdOn = false;
bool sensorOk = false;
bool firstReading = true;
unsigned long lastRead = 0;
// ---------- BH1750 helpers ----------
bool bhCommand(uint8_t cmd) {
Wire.beginTransmission(BH1750_ADDR);
Wire.write(cmd);
return Wire.endTransmission() == 0; // 0 = sensor answered
}
bool bhBegin() {
if (!bhCommand(BH_POWER_ON)) return false;
bhCommand(BH_RESET);
return bhCommand(BH_CONT_HRES);
}
// Returns lux, or -1 if the sensor did not answer
float bhReadLux() {
if (Wire.requestFrom(BH1750_ADDR, (uint8_t)2) != 2) return -1;
uint16_t raw = ((uint16_t)Wire.read() << 8) | Wire.read();
return raw / 1.2f; // datasheet: lux = count / 1.2
}
// ---------- Light-level rating ----------
const char* rating(float lx) {
if (lx < 10) return "Dark";
if (lx < 100) return "Dim";
if (lx < 300) return "Living room";
if (lx < 500) return "Reading";
if (lx < 1000) return "Office / desk";
if (lx < 10000) return "Bright / shade";
return "Direct sun";
}
// ---------- OLED ----------
void drawScreen() {
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print(F("LUX METER"));
if (holdOn) { display.setCursor(98, 0); display.print(F("HOLD")); }
display.setCursor(0, 14);
if (!sensorOk) {
display.setTextSize(1);
display.print(F("BH1750 not found"));
} else {
display.setTextSize(2);
display.print(luxNow, luxNow < 100 ? 1 : 0);
display.setTextSize(1);
display.print(F(" lx"));
display.setCursor(0, 34);
display.print(rating(luxNow));
display.setCursor(0, 46);
display.print(F("min ")); display.print(luxMin, 0);
display.print(F(" max ")); display.print(luxMax, 0);
}
display.setCursor(0, 56);
if (WiFi.status() == WL_CONNECTED) display.print(WiFi.localIP());
else display.print(F("Wi-Fi offline"));
display.display();
}
// ---------- Web page ----------
const char PAGE[] PROGMEM = R"HTML(<!DOCTYPE html><html><head>
<meta name="viewport" content="width=device-width,initial-scale=1"><title>Lux Meter</title>
<style>body{font-family:Arial,sans-serif;text-align:center;background:#111;color:#eee}
#lux{font-size:64px;margin:10px}#bar{height:18px;background:#333;border-radius:9px;margin:10px 20px}
#fill{height:18px;width:0;background:#fbc02d;border-radius:9px}button{font-size:18px;margin:6px;padding:8px 16px}</style>
</head><body><h2>Wemos D1 Mini Lux Meter</h2>
<div id="lux">--</div><div id="rate"></div><div id="bar"><div id="fill"></div></div>
<p>Min <b id="mn">-</b> lx Max <b id="mx">-</b> lx</p><p id="hold"></p>
<button onclick="fetch('/hold')">Hold / Run</button><button onclick="fetch('/reset')">Reset min/max</button>
<script>
function upd(){fetch('/data').then(r=>r.json()).then(d=>{
if(!d.ok){lux.textContent='Sensor error';return;}
lux.textContent=d.lux+' lx';rate.textContent=d.rating;mn.textContent=d.min;mx.textContent=d.max;
hold.textContent=d.hold?'HOLD':'';
// log scale bar: 1 lx = 0%, 100000 lx = 100%
fill.style.width=Math.min(100,Math.max(0,Math.log10(Math.max(d.lux,1))*20))+'%';});}
setInterval(upd,1000);upd();
</script></body></html>)HTML";
void handleRoot() { server.send_P(200, "text/html", PAGE); }
void handleData() {
String j = "{\"ok\":";
j += sensorOk ? "true" : "false";
j += ",\"lux\":" + String(luxNow, 1);
j += ",\"min\":" + String(luxMin, 0);
j += ",\"max\":" + String(luxMax, 0);
j += ",\"hold\":"; j += holdOn ? "true" : "false";
j += ",\"rating\":\""; j += rating(luxNow); j += "\"}";
server.send(200, "application/json", j);
}
void resetMinMax() { luxMin = luxNow; luxMax = luxNow; }
// ---------- Button: short press = hold, long press = reset ----------
void checkButton() {
static bool lastState = HIGH;
static unsigned long pressedAt = 0;
bool s = digitalRead(BUTTON_PIN);
if (s == LOW && lastState == HIGH) pressedAt = millis(); // pressed
if (s == HIGH && lastState == LOW) { // released
unsigned long t = millis() - pressedAt;
if (t > 1000) resetMinMax();
else if (t > 40) holdOn = !holdOn; // 40 ms debounce
}
lastState = s;
}
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
Wire.begin(D2, D1); // SDA, SCL
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
Serial.println(F("OLED not found - check wiring and address"));
}
display.clearDisplay(); display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0); display.print(F("Connecting Wi-Fi...")); display.display();
sensorOk = bhBegin();
Serial.println(sensorOk ? F("BH1750 ready") : F("BH1750 not found"));
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
unsigned long start = millis();
while (WiFi.status() != WL_CONNECTED && millis() - start < 15000) delay(250);
if (WiFi.status() == WL_CONNECTED) {
Serial.print(F("IP: ")); Serial.println(WiFi.localIP());
MDNS.begin("luxmeter"); // http://luxmeter.local
} else {
Serial.println(F("Wi-Fi not connected, OLED only"));
}
server.on("/", handleRoot);
server.on("/data", handleData);
server.on("/hold", []() { holdOn = !holdOn; server.send(200, "text/plain", "ok"); });
server.on("/reset", []() { resetMinMax(); server.send(200, "text/plain", "ok"); });
server.begin();
}
void loop() {
server.handleClient();
MDNS.update();
checkButton();
if (millis() - lastRead >= 500) { // 2 readings per second
lastRead = millis();
if (!holdOn) {
float lx = bhReadLux();
if (lx < 0) {
sensorOk = bhBegin(); // try to wake the sensor again
} else {
sensorOk = true;
luxNow = lx;
if (firstReading) { resetMinMax(); firstReading = false; }
if (luxNow < luxMin) luxMin = luxNow;
if (luxNow > luxMax) luxMax = luxNow;
}
}
drawScreen();
}
}
Testing and Using the Meter
- Open the Serial Monitor at 115200 baud. You should see
BH1750 readyand the IP address. - Cover the sensor with your finger: the reading should drop to near 0 lx and show Dark. Shine a phone torch on it: it should jump to several thousand lux.
- Open
http://luxmeter.local(or the IP address) on a phone on the same Wi-Fi. The value updates every second. - Hold the sensor flat at desk height, facing up, to measure work-surface light. Keep your body and shadow away from it.
- Press the button briefly to HOLD a reading, and hold it for more than 1 second to reset min/max before a new survey.
Troubleshooting
| Problem | Fix |
|---|---|
| OLED shows “BH1750 not found” | Check SCL to D1 and SDA to D2 (they are easy to swap), 3V3 and GND. Check the ADDR pin: to GND for 0x23, or change BH1750_ADDR to 0x5C if it is tied high. |
| OLED stays blank | Most 0.96 inch OLEDs use address 0x3C; some use 0x3D. Run an I2C scanner sketch and update OLED_ADDR. Check the VCC/GND order printed on the module. |
| Reading stuck at 0 or 54612 | 0 means the sensor is covered or in reset; 54612 (65535 / 1.2) is the top of the range in direct sunlight. Shade the sensor or move indoors. |
| Readings jump around | Fluorescent and cheap LED lamps flicker. Hold the meter still; for a smoother value, average several readings in the code. |
| Web page does not open | Check the IP on the OLED bottom line and keep the phone on the same 2.4 GHz Wi-Fi. Use the IP address if luxmeter.local does not work on Android. |
| “Wi-Fi offline” on the OLED | Check the SSID and password (case-sensitive). The ESP8266 only supports 2.4 GHz networks. |
| Button does nothing | The button must connect D5 to GND. Use the two legs on opposite sides of a 4-leg tactile button. |
Ideas to Extend
- Add a relay on D6 and switch a lamp on automatically when the desk falls below 300 lx.
- Log the readings every minute to a Google Sheet or an MQTT broker to see how daylight changes in a room.
- Run it from a small Li-ion cell with a TP4056 charger board for a truly portable meter.