กลับไปหน้ารวมไฟล์
automatic-night-light-arduino-en.md

Environment Reaction: The Auto Night Light

The Automatic Night Light is a classic first-step into environmental robotics. It teaches the fundamental principle of taking an analog input (environmental state) to trigger a digital threshold (actuation), acting as a basic dawn-to-dusk sensor.

button_led_basic_interaction_1772681969235.png

Analog-to-Digital Conversion (ADC)

The Arduino "sees" light through an LDR (Light Dependent Resistor).

  1. The Voltage Divider: You must pair the LDR with a fixed resistor (e.g., 10k-ohm) in a circuit. As light changes, the resistance of the LDR changes, which alters the voltage hitting Arduino pin A0.
  2. The Read: The analogRead(A0) function takes that 0-5V voltage and converts it into a number between 0 and 1023.
    • Bright light = 900
    • Pitch black = 150

The Control Threshold

Instead of a complex equation, you create a simple logical threshold boundary:

int lightThreshold = 400; // Calibrate this number based on your room

if (analogRead(A0) < lightThreshold) {
  digitalWrite(ledPin, HIGH); // Turn lights ON in the dark
} else {
  digitalWrite(ledPin, LOW); // Turn lights OFF in the light
}

Adding Smooth Fades

For a premium touch, instead of instantly snapping the light on (digitalWrite), you can use the map() function. You link the darkness level directly to an analogWrite(PWM) pin. As the sun slowly sets, the LED will slowly fade brighter and brighter in a perfectly smooth transition!

Basic Hardware Requirements

  • Arduino Uno/Nano: The logic gate.
  • LDR (Photoresistor).
  • 10k-ohm Resistor: Essential for the voltage divider.
  • LED or small 5V Relay module.

ข้อมูล Frontmatter ดั้งเดิม

title: "Automatic Night Light Project"
description: "Your first automation! Build a simple, intelligent dawn-to-dusk light that automatically turns on high-power LEDs only when the room gets dark."
category: "Basic Electronics"
difficulty: "Easy"