Projects / MKR WiFi 1010 light page

MKR WiFi 1010 light page

Intermediate2.2 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. ARDUINO

    Wire the sensor

    VCC and GND, SDA to pin 11, SCL to pin 12.

  2. ARDUINOBH1750690 lux

    Install the libraries

    WiFiNINA and BH1750.

  3. ARDUINO

    Edit the sketch

    Put your Wi-Fi name and password in the placeholders.

  4. ARDUINO

    Upload it

    Open the Serial Monitor at 115200 for the address.

  5. ARDUINO

    Open the address

    On a phone or computer on the same network.

  6. ARDUINO

    Cover the sensor with a hand

    After the next refresh the reading drops.

  7. ARDUINO

    Shine a torch on it

    The brightest value goes up.

In short

How do you build an MKR WiFi 1010 light page?

An Arduino MKR WiFi 1010 reads a BH1750 light sensor and serves the light level and the brightest value since power-up on a local page. You need Arduino MKR WiFi 1010, BH1750 light sensor and Breadboard 830 Tie Points. It takes 7 steps and about 130 minutes.

Level
Intermediate
Time
About 130 min
Parts
3

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
BH1750 VCCMKR VCC pinPower at 3.3 V
BH1750 GNDMKR GND pinCommon ground
BH1750 SDAMKR pin 11 (SDA)I2C data
BH1750 SCLMKR pin 12 (SCL)I2C clock

Code

The BH1750 measures light in lux, the unit used for room lighting, and sends the answer over I2C, so only two signal wires are needed. A normal lit room is a few hundred lux, a dim evening room is tens, and a bright day outdoors is tens of thousands. The sensor is meant for 3.3 volts, which suits the MKR WiFi 1010, whose logic is 3.3 volts. The board has a separate Wi-Fi chip, and the WiFiNINA library talks to it. After joining your own network, the board prints its address in the Serial Monitor and then runs a small web server. Each time the page is opened it shows the current reading, a word for it and the brightest value since the board was switched on. The page reloads itself every ten seconds. The sensor's reading depends on its angle and on shadows, and it does not see colour, so it is useful for comparing places, not for exact measurements. The page is for your home network only, so do not forward a router port to it. The name and password are placeholders; use your own and never share the sketch with the real ones in it.

Runs on a virtual Arduino MKR WiFi 1010 in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <SPI.h>
#include <WiFiNINA.h>
#include <Wire.h>
#include <BH1750.h>

const char WIFI_NAME[] = "YOUR_WIFI_NAME";         // placeholders: use your own network
const char WIFI_PASSWORD[] = "YOUR_WIFI_PASSWORD";

BH1750 lightMeter;
WiFiServer server(80);
float brightest = 0;

const char* describe(float lux) {
  if (lux < 10) return "Dark";
  if (lux < 100) return "Dim";
  if (lux < 500) return "Normal indoor light";
  if (lux < 5000) return "Bright";
  return "Very bright, near a window or outdoors";
}

void setup() {
  Serial.begin(115200);
  Wire.begin();                                    // pins 11 and 12 on the MKR
  lightMeter.begin();
  while (WiFi.begin(WIFI_NAME, WIFI_PASSWORD) != WL_CONNECTED) {
    delay(3000);
  }
  server.begin();
  IPAddress ip = WiFi.localIP();
  Serial.print("Open http://");
  Serial.println(ip);
}

void loop() {
  float lux = lightMeter.readLightLevel();
  if (lux > brightest) brightest = lux;

  WiFiClient client = server.available();
  if (client) {
    bool blankLine = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (c == '\n' && blankLine) {              // end of the request: send the page
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println();
          client.println("<!DOCTYPE html><html><head><meta charset='utf-8'>");
          client.println("<meta name='viewport' content='width=device-width, initial-scale=1'>");
          client.println("<meta http-equiv='refresh' content='10'><title>Room light</title></head>");
          client.println("<body style='font-family:sans-serif;text-align:center;margin-top:3em'>");
          client.print("<h1>"); client.print(describe(lux)); client.println("</h1>");
          client.print("<p style='font-size:3em'>"); client.print(lux, 0); client.println(" lux</p>");
          client.print("<p>Brightest since power-up: "); client.print(brightest, 0); client.println(" lux</p>");
          client.println("</body></html>");
          break;
        }
        if (c == '\n') blankLine = true;
        else if (c != '\r') blankLine = false;
      }
    }
    delay(1);
    client.stop();
  }
  delay(200);
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Open the pageA word and a lux number
Cover the sensorDark and a low number
Shine a torchA much higher number
Brightest valueKeeps the highest seen
Wait ten secondsThe page reloads by itself

If it does not work

Stays silent

Check the name and password, and that the network is 2.4 GHz.

Reads zero or 65535

Check SDA and SCL on pins 11 and 12 and the 3.3 V power.

Page does not open

Use the exact address from the Serial Monitor and the same Wi-Fi.

Does not compile

Install the WiFiNINA and BH1750 libraries and the SAMD board package.

Number jumps

Light flickers from mains lamps and screens, this is normal.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the MKR WiFi 1010 light page?

Arduino MKR WiFi 1010, BH1750 light sensor and Breadboard 830 Tie Points.

How long does the MKR WiFi 1010 light page take to build?

About 130 minutes for a intermediate maker, in 7 steps.

How do I know the MKR WiFi 1010 light page works?

Open the page: A word and a lux number. Cover the sensor: Dark and a low number. Shine a torch: A much higher number.

Stays silent: what should I check?

Check the name and password, and that the network is 2.4 GHz.

Reads zero or 65535: what should I check?

Check SDA and SCL on pins 11 and 12 and the 3.3 V power.

Page does not open: what should I check?

Use the exact address from the Serial Monitor and the same Wi-Fi.

Does not compile: what should I check?

Install the WiFiNINA and BH1750 libraries and the SAMD board package.