Arduino Nano 33 IoT - Light Sensor

This guide shows you how to use the Arduino Nano 33 IoT with a light sensor. We will learn the following steps:

If you need a simple light sensor module, check out the Arduino Nano 33 IoT - LDR Light Sensor Module guide.

Arduino Nano 33 IoT Light Sensor

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×Light Sensor
1×10 kΩ resistor
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

Or you can buy the following sensor 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 .

Overview of Light Sensor

The most popular sensor for measuring light is the photoresistor (also called a photocell or light-dependent resistor, LDR).

It can be used to check if there is light. It can also measure how bright the light is.

Light Sensor Pinout

A light sensor has two pins. Like a normal resistor, you don't need to tell these pins apart.

Light Sensor Pinout

How Light Sensor Works

A photoresistor's resistance works in the opposite way to the light's brightness. When there is less light on the photoresistor, its resistance becomes higher. So, by checking the resistance of the photoresistor, we can tell how bright the surrounding light is.

How Light Sensor Works

WARNING

The value read by the photoresistor gives a rough idea of how strong the light is, but it does not show the exact brightness. This means the photoresistor is not a good choice for tasks that need very precise measurements. Also, some uses need calibration.

Arduino Nano 33 IoT - Light Sensor

The Arduino Nano 33 IoT board's analog input pin changes a voltage (from 0 volts to a reference voltage, usually 3.3V) into a whole number between 0 and 4095. This number is known as the analog value or ADC value. By connecting an analog input pin to a photoresistor, you can use the analogRead() function to read this number.

Wiring Diagram between Light Sensor and Arduino Nano 33 IoT

The wiring diagram between Arduino Nano and 33 IoT Light Sensor

This image is created using Fritzing. Click to enlarge image

Arduino Nano 33 IoT Code

Below is Arduino Nano 33 IoT code that gets a reading from a light sensor and figures out how bright it is.

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-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

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Copy the code above and paste it into the Arduino IDE.
  • Click the Upload button on the Arduino IDE to compile and send the code to the Arduino Nano 33 IoT board.
  • Open the Serial Monitor in the Arduino IDE.
How to open serial monitor on Arduino IDE
  • Sends light to the sensor
  • Look at the output on the Serial Monitor. It appears like this:
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 Nano and 33 IoT Light Sensor LED

This image is created using Fritzing. Click to enlarge image

Arduino Nano 33 IoT Code

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

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-light-sensor */ #define LIGHT_SENSOR_PIN A0 // The Arduino Nano 33 IoT pin connect to the light sensor #define LED_PIN 2 // The Arduino NanO Arduino Nano 33 IoT pin connected to LED #define ANALOG_THRESHOLD 500 void setup() { pinMode(LED_PIN, OUTPUT); // set Arduino Nano 33 IoT pin to output mode } void loop() { int analog_value = analogRead(LIGHT_SENSOR_PIN); // read the value on analog pin if (analog_value < ANALOG_THRESHOLD) digitalWrite(LED_PIN, HIGH); // turn on LED else digitalWrite(LED_PIN, LOW); // turn off LED }

※ NOTE THAT:

Please note that the Arduino Nano 33 IoT pins A4 and A5 have built-in pull-up resistors for I2C communication. This can affect analog readings, so it is recommended to avoid using these pins with any devices/sensors that relies on ADC.

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!