Arduino Nano - Light Sensor

This tutorial instructs you how to use the light sensor with Arduino Nano. In detail:

If you're looking for a light sensor in module form, I recommend checking out the tutorial for the Arduino Nano - LDR Light Sensor Module.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Light Sensor
1×10 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board 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 light sensor used in this tutorial is a photoresistor, also known as a photocell, light-dependent resistor, or LDR.

It is not only used to detect light, but also to measure the amount of brightness or illumination of the surrounding light.

The Light Sensor Pinout

A photoresistor has two pins which do not need to be distinguished as it is a type of resistor and they are symmetrical.

Light Sensor pinout

How It Works

The amount of light that the photoresistor's face is exposed to will determine its resistance. By measuring this resistance, we can measure the brightness of the ambient light.

How Light Sensor Works

WARNING

The value of the light sensor only gives an indication of the intensity of light, and is not an exact measure of luminous flux. Therefore, it should only be used in applications that do not need a high degree of precision.

Arduino Nano - Light Sensor

Arduino Nano's pins A0 through A7 can be used as analog inputs. These analog input pins convert the voltage (ranging from 0 volts to VCC) into integer values (from 0 to 1023), referred to as ADC value or analog value.

Connect one pin of the photoresistor to an analog input pin. Using analogRead() function, read the analog value from the pin. This will allow us to determine the light levels relatively.

Wiring Diagram

The wiring diagram between Arduino Nano and Light Sensor

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code

The code below reads the value from a photocell and determines the light level qualitatively.

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

Detailed Instructions

  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button on the IDE to transfer the code to the Arduino Nano.
  • Open the Serial Monitor.
  • Shine light onto the sensor.
  • Check out the result on the Serial Monitor.
COM6
Send
Analog reading: 163 - Dim Analog reading: 152 - Dim Analog reading: 187 - Dim Analog reading: 188 - Dim Analog reading: 957 - Very bright Analog reading: 972 - Very bright Analog reading: 981 - Very bright
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Light Sensor and LED

  • When it is dark, the code below will turn the LED ON. Otherwise, the LED will be switched OFF.
/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-light-sensor */ const int LIGHT_SENSOR_PIN = A0; // The Arduino Nano pin connected to light sensor's pin const int LED_PIN = 2; // The Arduino Nano pin connected to LED's pin const int ANALOG_THRESHOLD = 500; int analog_value; void setup() { pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode } void loop() { analog_value = analogRead(LIGHT_SENSOR_PIN); // read the input on analog pin if(analog_value < ANALOG_THRESHOLD) digitalWrite(LED_PIN, HIGH); // turn on LED else digitalWrite(LED_PIN, LOW); // turn off LED }
  • The wiring diagram for the above code:
The wiring diagram between Arduino Nano and Light Sensor LED

This image is created using Fritzing. Click to enlarge image

Video Tutorial

Challenge Yourself

  • When the light in your room is dim, activate it automatically.
  • Refer to Arduino Nano - Relay for more information.

※ 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!