Arduino MKR WiFi 1010 - Light Sensor

Welcome to your complete guide on using a light sensor with the Arduino MKR WiFi 1010! Light sensing is one of the most fundamental and practical skills in electronics. In this comprehensive tutorial, you'll learn how to connect a photoresistor (LDR - light-dependent resistor) to your Arduino MKR WiFi 1010 board and read light intensity levels from your environment. The Arduino MKR WiFi 1010 is perfect for light sensing projects because of its accurate analog input capabilities, compact size, and built-in WiFi connectivity for IoT applications. Whether you're creating an automatic night light, smart blinds, solar panel tracker, or daylight monitor, the Arduino MKR WiFi 1010 with a light sensor provides simple and reliable light detection. This tutorial will guide you through wiring the photoresistor to your Arduino MKR WiFi 1010, understanding how light-dependent resistors work, programming your board step-by-step to read analog light values, and converting those values into meaningful brightness levels. By the end of this tutorial, you'll have a fully functional light sensing system using the Arduino MKR WiFi 1010 and LDR sensor, displaying real-time light levels on the Serial Monitor, with skills you can apply to countless light-activated projects.

What You'll Learn

Real-World Applications

Ready to start sensing light with your Arduino MKR WiFi 1010? Let's begin building your light detection project!

Arduino MKR WiFi 1010 Light Sensor

Hardware Preparation

1×Arduino MKR WiFi 1010
1×Micro USB Cable
1×Light Sensor
1×10 kΩ Resistor
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

The LDR light sensor is very affordable, but it requires a resistor for wiring, which can make the setup more complex. To simplify the wiring, you can use an LDR light sensor module as an alternative.

Overview of Light Sensor

A photoresistor (also called a photocell, LDR, or light-dependent resistor) is a simple sensor that changes its resistance based on the amount of light hitting it. The brighter the light, the lower the resistance. This makes it perfect for detecting light levels and determining whether it's dark, dim, or bright!

Light Sensor Pinout

A photoresistor has just two pins, and they're not polarized (meaning it doesn't matter which way you connect them - just like a regular resistor).

Light Sensor Pinout

How Light Sensor Works

Here's the key principle: the resistance of a photoresistor is inversely proportional to light intensity. This means:

  • More lightLower resistance → Easier for electricity to flow
  • Less lightHigher resistance → Harder for electricity to flow
How Light Sensor Works

Important Note: Photoresistors provide relative light measurements, not absolute lux values. They're great for detecting light changes and brightness levels, but they're not suitable for applications requiring precise, calibrated light measurements.

Arduino MKR WiFi 1010 - Light Sensor

The Arduino MKR WiFi 1010 has analog input pins that can read voltages from 0V to 3.3V and convert them to digital values from 0 to 4095. By connecting a photoresistor in a voltage divider circuit, you can use the analogRead() function to measure light intensity as a number that changes with brightness!

Wiring Diagram

Connect your light sensor (photoresistor) to the Arduino MKR WiFi 1010 as shown below. The photoresistor is wired with a 10kΩ resistor to create a voltage divider circuit:

The wiring diagram between Arduino MKR WiFi 1010 Light Sensor

This image is created using Fritzing. Click to enlarge image

Arduino MKR WiFi 1010 Code

Below is Arduino MKR WiFi 1010 code that gets a reading from a light sensor and figures out how bright it is.

/* * This Arduino MKR WiFi 1010 code was developed by newbiely.com * * This Arduino MKR WiFi 1010 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-light-sensor */ #define LIGHT_SENSOR_PIN A0 // The Arduino Nano 33 IoT pin connect to the light sensor void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); } void loop() { // reads the input on analog pin (value between 0 and 4095) int analog_value = analogRead(LIGHT_SENSOR_PIN); Serial.print("Analog Value = "); Serial.print(analog_value); // The raw analog reading // We'll have a few threshholds, qualitatively determined if (analog_value < 40) { Serial.println(" => Dark"); } else if (analog_value < 800) { Serial.println(" => Dim"); } else if (analog_value < 2000) { Serial.println(" => Light"); } else if (analog_value < 3200) { Serial.println(" => Bright"); } else { Serial.println(" => Very bright"); } delay(500); }

Detailed Instructions

New to Arduino MKR WiFi 1010? Complete our Getting Started with Arduino MKR WiFi 1010 tutorial first to set up your development environment.

  • Wire the components according to the diagram above
  • Connect the Arduino MKR WiFi 1010 to your computer via USB cable
  • Open Arduino IDE and select the correct board and COM port
  • Copy the code and upload it to your board
  • Open the Serial Monitor
How to open serial monitor on Arduino IDE
  • Shine light on the sensor or cover it with your hand
  • View the results on the Serial Monitor:
COM6
Send
Analog Value = 406 => Dim Analog Value = 412 => Dim Analog Value = 465 => Dim Analog Value = 471 => Dim Analog Value = 3511 => Very bright Analog Value = 3521 => Very bright Analog Value = 3618 => Very bright
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Light Sensor and LED

Wiring Diagram

The wiring diagram between Arduino MKR WiFi 1010 Light Sensor LED

This image is created using Fritzing. Click to enlarge image

Arduino MKR WiFi 1010 Code

The code below will switch the LED on when it is dark and off when it is light.

/* * This Arduino MKR WiFi 1010 code was developed by newbiely.com * * This Arduino MKR WiFi 1010 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-light-sensor */ // constants won't change const int LIGHT_SENSOR_PIN = A0; // Arduino MKR WiFi 1010 pin connected to light sensor's pin const int LED_PIN = 6; // Arduino MKR WiFi 1010 pin connected to LED's pin const int ANALOG_THRESHOLD = 500; // variables will change: int analogValue; void setup() { pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode } void loop() { analogValue = analogRead(LIGHT_SENSOR_PIN); // read the input on analog pin if(analogValue < ANALOG_THRESHOLD) digitalWrite(LED_PIN, HIGH); // turn on LED else digitalWrite(LED_PIN, LOW); // turn off LED }

Challenge Yourself

Ready to take your light sensing skills further? Try these exciting projects:

  • Automatic LED Control: Make LEDs turn on automatically when it gets dark and turn off when it's bright
  • Sunrise Alarm: Create an alarm system that triggers based on sunrise detection (when light levels increase)
  • Light-Controlled Fan: Build a system where a fan speed changes based on light intensity (useful for temperature control)
  • Multi-Sensor Light Tracker: Use multiple photoresistors to track the direction of the brightest light source
  • WiFi Light Logger: Send light level data wirelessly to your computer or cloud service using the MKR WiFi 1010's connectivity
  • RGB Light Feedback: Control an RGB LED color based on light levels (blue for dark, yellow for dim, red for bright)
  • Smart Window Shades: Combine a servo motor with the light sensor to automatically open/close blinds based on sunlight

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!