Arduino Nano ESP32 - Light Sensor

This tutorial provides instructions on how to use Arduino Nano ESP32 with the light sensor. In detail, we will learn:

If you're in search of a module-based light sensor, I recommend taking a look at the Arduino Nano ESP32 - LDR Light Sensor Module tutorial.

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×Light Sensor
1×10 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Optional) DC Power Jack
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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. We appreciate your support.

Overview of Light Sensor

The most wide-used light sensor is a photoresistor (also known as photocell, or light-dependent resistor, LDR).

It can be used to detect the presence ofthe light. It can also be used to measure the illuminance/brightness level of the light.

Light Sensor Pinout

A light sensor has two pins. Just like a normal resistor, We do NOT need to distinguish these pins.

Light Sensor Pinout

How Light Sensor Works

The photoresistor's resistance is in inverse proportion to the intensity of the light. The less light the photoresistor's face is exposed, the more the photoresistor's resistance is. Therefore, we can infer how bright the ambient light is by measuring the photoresistor's resistance.

How Light Sensor Works

WARNING

The value measured by photoresistor reflects the approximated tendency of the light's intensity, it does NOT represent exactly the luminous flux. Therefore, the photoresistor should not be used in an application that requires high accuracy. calibration is also required for some kind application.

Arduino Nano ESP32 - Light Sensor

The ESP32's analog input pin converts the voltage (between 0v and ADC_VREF - default is 3.3V) into integer values (between 0 and 4095), called analog value or ADC value. By connecting an analog input pin of Arduino Nano ESP32 to the photoresistor, we can read the analog value by using analogRead() function.

Wiring Diagram between Light Sensor and Arduino Nano ESP32

The wiring diagram between Arduino Nano ESP32 and Light Sensor

This image is created using Fritzing. Click to enlarge image

Arduino Nano ESP32 Code

The below Arduino Nano ESP32 code reads the value from a light sensor and infers the light level

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-light-sensor */ #define LIGHT_SENSOR_PIN A0 // The Arduino Nano ESP32 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

How to open serial monitor on Arduino IDE
  • Radiates light to sensor
  • Check out the result on the Serial Monitor. It looks like the below::
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 ESP32 and Light Sensor LED

This image is created using Fritzing. Click to enlarge image

Arduino Nano ESP32 Code

The below code turns ON the LED if it is dark, otherwise turns OFF the LED

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-light-sensor */ #define LIGHT_SENSOR_PIN A0 // The Arduino Nano ESP32 pin connect to the light sensor #define LED_PIN D2 // The Arduino NanO ESP32 pin connected to LED #define ANALOG_THRESHOLD 500 void setup() { pinMode(LED_PIN, OUTPUT); // set ESP32 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 }

Video Tutorial