ไฟกลางคืนอัตโนมัติ
เปิด LED เมื่อมืดลงและตรวจพบการเคลื่อนไหว
ไฟกลางคืนอัตโนมัติ เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Arduino & IoT Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Project Goal
Now combine your skills into one device: an automatic night light that switches an LED on when it is dark and someone moves.
Two Inputs, One Output
You will read two inputs, the LDR for light and the PIR for motion, then drive one output: an LED.
Plan the Pins
Assign each part a pin: the LDR on an analog input, the PIR on a digital input, and the LED on a digital output.
Set Up the LED
In setup, mark the LED pin as an OUTPUT so the board can drive current to it on command.
pinMode(ledPin, OUTPUT);Read Both Sensors
Each loop, grab a fresh light value and the current motion state so your decision uses up-to-date data.
int light = analogRead(A0);
int motion = digitalRead(pirPin);Combine With AND
The light should turn on only when it is dark and motion is present. Use && to require both at once.
if (light < threshold && motion == HIGH)Turn the LED On
When both conditions hold, drive the LED pin HIGH to light it up for whoever just walked in.
digitalWrite(ledPin, HIGH);Turn the LED Off
Otherwise, set the pin LOW so the light goes dark again when motion stops or the room is bright.
digitalWrite(ledPin, LOW);Keep It Lit Briefly
People dislike a light that snaps off instantly. Add a short hold time so it stays on a while after the last motion.
Skip Daytime Triggers
The light check matters: requiring dark means daytime motion is ignored, saving power and avoiding pointless blinks.
Make It Real
Swap the LED for a relay and you can switch a real lamp. Same logic, bigger impact in your hallway.
Quick Check
What condition turns the night light on?
Recap: Your Night Light
You merged an LDR, a PIR, and an LED, using a threshold and && so the light glows only in the dark with motion.
คำถามที่พบบ่อย
บทเรียน “ไฟกลางคืนอัตโนมัติ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ไฟกลางคืนอัตโนมัติ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ไฟกลางคืนอัตโนมัติ”
เปิด LED เมื่อมืดลงและตรวจพบการเคลื่อนไหว คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ไฟกลางคืนอัตโนมัติ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม
ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- อ่านเซนเซอร์ตรวจจับการเคลื่อนไหว PIR
- วัดแสงด้วย LDR
- ตั้งค่าเกณฑ์แสง
- ไฟกลางคืนอัตโนมัติ