Arduino Nano Light Sensors

The Light Sensors project demonstrates how to use photoresistor (LDR) and digital light sensors with an Arduino Nano. The Nano reads the light level from the sensor and can trigger LEDs, relays, or messages based on whether it is bright or dark. This makes light‑sensing easy to integrate into automatic lights, security systems, and environmental‑monitoring setups.

Types of Light Sensors

Two common types of light sensors used with Arduino Nano are:

  • Analog light sensors (e.g. LDR modules): These output a continuously varying voltage that can be read with analogRead() and mapped to 0–1023.
  • Digital/I2C light sensors (e.g. BH1715, BH1750): These measure ambient light in lux and communicate over I2C, giving a calibrated value directly instead of a raw analog reading.

Components Needed

  • Arduino Nano
  • LDR or photoresistor module
  • 10K resistor (if using a bare LDR)
  • LED or relay module (for automatic light control)
  • Jumper Wires
  • Breadboard
  • Optional: Digital I2C light sensor (BH1715 or similar) for calibrated lux values

Circuit Setup (Analog LDR)

1. Connect LDR Module to Arduino Nano:

If using a pre‑built LDR module:

  • VCC → 5V on Arduino Nano.
  • GND → GND on Arduino Nano.
  • OUT (analog output) → A0 on Arduino Nano.

If using a bare photoresistor in a voltage divider:

  • Connect one leg of the LDR to 5V.
  • Connect the other leg to A0.
  • Connect a 10K resistor between A0 and GND.

How Analog Light Sensing Works

The LDR changes resistance with light: lower resistance in bright light and higher resistance in darkness. The Arduino reads the divided voltage at A0 (0–1023) and converts it into a relative light level. You can set thresholds to decide when it is dark or bright enough to trigger an action.

Program: Arduino Nano LDR Light Sensor (Automatic Light)
// Analog pin for LDR
const int ldrPin = A0;
// LED pin (or relay for lamp)
const int ledPin = 3;

// Threshold for darkness (adjust after testing)
const int darkThreshold = 300;

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int lightValue = analogRead(ldrPin);
  
  // Print raw value for calibration
  Serial.print("Light: ");
  Serial.println(lightValue);
  
  // Turn LED on when it's dark
  if (lightValue < darkThreshold) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
  
  delay(100);
}

Using Digital I2C Light Sensors (BH1715 / BH1750)

For more accurate ambient‑light measurements (in lux), you can use an I2C light sensor such as the BH1715 or BH1750. These sensors connect to the Arduino Nano’s SDA and SCL pins and provide a calibrated light‑level reading over the I2C bus.

Typical wiring for BH1750 (similar for BH1715):

  • VCC → 5V or 3.3V (check datasheet).
  • GND → GND.
  • SDA → A4 (or SDA) on the Arduino Nano.
  • SCL → A5 (or SCL) on the Arduino Nano.
Example: BH1750 I2C Light Sensor with Arduino Nano
#include <Wire.h>
#include <BH1750.h>

BH1750 lightMeter;

void setup() {
  Wire.begin();
  lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE);
  Serial.begin(9600);
}

void loop() {
  // Read light level in lux
  float lux = lightMeter.readLightLevel();
  Serial.print("Light level: ");
  Serial.print(lux);
  Serial.println(" lux");
  
  delay(1000);
}

// To install the libraries:
// 1) Sketch → Include Library → Manage Libraries
// 2) Install "BH1750" and "Wire" (Wire is built‑in, but you may need to ensure it is enabled).

Instructions (Analog LDR Setup)

1. Circuit Setup:

Wire the LDR (module or bare LDR with 10K resistor) to the Arduino Nano as described above, and connect the LED to the chosen digital pin through a 220 Ω resistor.

2. Code Upload:

Open the Arduino IDE, paste the LDR light‑sensor code, and upload it to the Arduino Nano.

3. Calibration:

Open the Serial Monitor (9600 baud) and observe the Light: values in full light and in darkness. Then adjust darkThreshold until the LED turns on only when it is actually dark.

Applications

Automatic Lighting: Use the Arduino Nano light sensor to turn on outdoor or indoor lights when ambient light falls below a set threshold.

Security Illumination: Combine light sensing with a PIR motion sensor so that security lights come on only at night when motion is detected.

Environmental Monitoring: Monitor light levels for greenhouses, plant‑growth chambers, or smart‑home dashboards, and log the data to a computer or SD card.

Troubleshooting

  • LED never turns on? Check the LDR wiring, threshold value, and LED polarity; also verify that the ledPin matches your circuit.
  • LED flickers or behaves erratically? Ensure the LDR is not exposed to quick light changes and use a stable power supply.
  • I2C sensor not detected? Confirm SDA/SCL wiring and that the correct I2C address and library (BH1750 / BH1715) are used; many modules default to 0x23 or 0x5C.

Best Practices and Notes

  • Always place the light sensor where it can see the ambient light directly, not blocked by enclosures or shadows.
  • Use a 220 Ω resistor in series with the LED to avoid over‑current, and a 10K resistor in the LDR voltage divider for stable readings.
  • For I2C sensors, keep SDA/SCL wires as short as possible and use pull‑up resistors if not built‑in on the module.

Extensions and Ideas

You can extend this project by combining an LDR with a motion sensor so that lights only turn on at night and only when movement is detected, or by adding a relay to control high‑power street or room lights, or by sending lux values from an I2C sensor to an LCD or TFT display for a compact light‑level dashboard.