กลับหน้าหลัก
views
How to Use NodeMCU ESP8266 Analog Input with LDR to Control LED
Last updated on

How to Use NodeMCU ESP8266 Analog Input with LDR to Control LED


How to Use NodeMCU ESP8266 Analog Input with LDR to Control LED

This tutorial covers using NodeMCU ESP8266 to read analog signals from a Light Dependent Resistor (LDR) and control an LED based on ambient light levels. Perfect for beginners learning Analog Input on ESP8266.

Understanding LDR Operation

LDR (Light Dependent Resistor) is a component whose resistance changes based on light intensity:

  • Bright light → Lower resistance
  • Dark conditions → Higher resistance

When connected in a voltage divider circuit and read through the Analog pin, values range from 0 to 1023.

Required Components

  • NodeMCU ESP8266 V2 (CP2102)
  • Breadboard MB-102
  • LDR (Photoresistor) 5mm, 10mm, or 20mm
  • LED (red or green)
  • 10k Ohm resistor (for voltage divider)
  • 220 Ohm resistor (for LED)
  • Jumper wires (M-M, M-F)
  • Micro USB cable
  • Power Adapter 5V 2A (for standalone operation)

Circuit Diagram and Wiring

NodeMCU ESP8266 connected to LDR and LED on breadboard showing A0 pin connected to LDR voltage divider and D0 pin connected to LED through 220 Ohm resistor

Wiring connections:

ComponentNodeMCU ESP8266 Pin
LED (anode +)D0
LDR (pin 1)A0
10k Resistor (pin 1)3.3V
10k Resistor (pin 2)A0 and LDR (pin 1)
LDR (pin 2)GND

Connect LED’s other leg through 220 Ohm resistor to GND.

Example Code

// Define used pins
const int ldrPin = A0;     // Analog pin for LDR reading
const int ledPin = D0;      // Digital pin for LED control

// Variables
int sensorValue = 0;         // Value read from LDR (0-1023)
int threshold = 500;        // Threshold for LED on/off

void setup() {
  // Initialize Serial Monitor
  Serial.begin(9600);
  
  // Set LED pin as Output
  pinMode(ledPin, OUTPUT);
  
  Serial.println("Start reading LDR value...");
}

void loop() {
  // Read analog value from LDR
  sensorValue = analogRead(ldrPin);
  
  // Display read value on Serial Monitor
  Serial.print("LDR Value: ");
  Serial.println(sensorValue);
  
  // Check value and control LED
  if (sensorValue > threshold) {
    // Bright light -> Turn LED OFF
    digitalWrite(ledPin, LOW);
    Serial.println("Light detected -> LED OFF");
  } else {
    // Dark -> Turn LED ON
    digitalWrite(ledPin, HIGH);
    Serial.println("Dark detected -> LED ON");
  }
  
  // 500ms delay
  delay(500);
}

Upload and Testing Procedure

  1. Select board NodeMCU 1.0 (ESP-12E Module) from Tools > Board menu
  2. Select the connected Port
  3. Click Upload to flash the code
  4. Open Serial Monitor and set Baud Rate to 9600
Serial Monitor showing LDR readings and LED on/off status

Adjusting Threshold Value

Default threshold is 500, suitable for normal room conditions. To adjust sensitivity:

  • Lower threshold (e.g., 300) → LED turns on with less darkness
  • Higher threshold (e.g., 700) → LED turns on only in very dark conditions

Test by shining a flashlight on the LDR and observing value changes in Serial Monitor.

Practical Applications

Practical applications like automatic plant grow box or automatic night light

After understanding the basics, you can apply this knowledge to various projects:

  • Automatic Night Light - Turns on when dark
  • Plant Grow Box - Light control system
  • Light Alarm System - Alerts when light is abnormal
  • Light-Following Robot - Robot that avoids or follows light sources

Summary

Using Analog Input with NodeMCU ESP8266 and LDR is fundamental for IoT projects involving environment sensing. The analogRead() command reads values from 0 to 1023, and these values can be processed to control other devices as needed.

อยากทำโปรเจคแบบนี้?

รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน

If you need Arduino project service or urgent IoT development, see full service details on the home page

จ้างทำโปรเจคเลย

ความคิดเห็น

Verified user reviews

รีวิวและความคิดเห็นจากผู้ใช้จริง

ล็อกอินด้วยบัญชีบนเว็บนี้แล้วให้คะแนนหรือคอมเมนต์ได้เลย ระบบเก็บผ่าน Supabase ไม่ต้องใช้ GitHub แล้ว

กำลังโหลดรีวิว...