กลับไปหน้ารวมไฟล์
rgb-led-with-3-function-dipswitch-2b8bcc.md

แนวคิดโปรเจกต์

โปรเจกต์ RGB LED กับ DIP Switch 3 สวิตช์ จะช่วยให้น้องๆ สลับโหมดสีหรือรูปแบบการกระพริบได้ทันทีโดยไม่ต้องมานั่งอัปโหลด Code ใหม่ให้เสียเวลา แค่ใช้ DIP Switch 3 ตำแหน่ง เราก็สามารถสร้างสถานะที่แตกต่างกันได้ถึง 8 รูปแบบ (2^3) เลยนะน้อง จะเอาไปทำไฟประดับหรือไฟสถานะก็หล่อเท่เลยงานนี้

การต่อวงจร (Hardware Configuration)

  • RGB LED: เหมือนกับโปรเจกต์ RGB ทั่วไปเลย ขา Red, Green และ Blue ให้ต่อเข้ากับ Pins ที่รองรับ PWM (เช่น Pins 9, 10, 11) และที่สำคัญ ห้ามลืม ใส่ตัวต้านทาน (Resistor) 220 โอห์ม เพื่อจำกัดกระแสด้วยนะน้อง ห้ามช็อตนะตัวนี้ เดี๋ยว LED จะลาโลกไปซะก่อน
  • DIP Switch: ขาทั้ง 3 ของสวิตช์ให้ต่อเข้ากับ Digital Input Pins (เช่น Pins 5, 6, 7) โดยจะใช้ Internal หรือ External Pull-up Resistor ก็ได้ เพื่อให้บอร์ด Arduino อ่านค่าสถานะ Logic เป็น HIGH หรือ LOW ได้แม่นๆ ไม่เอ๋อ

Logic และการควบคุม

ตัว Arduino จะคอยอ่านค่าสถานะจาก Pins ของ DIP Switch ทั้ง 3 ตัวด้วยคำสั่ง digitalRead() จากนั้นเราจะใช้ if...else หรือ switch case เพื่อเช็คเงื่อนไขในการเรียกฟังก์ชันสีต่างๆ เช่น:

  • Switch 1 ON: ไฟสีแดง (Red)
  • Switch 2 ON: ไฟสีเขียว (Green)
  • Switch 1 & 2 ON: กลายเป็นสีเหลือง (Yellow) เกิดจากการผสมแสงแดงกับเขียวเข้าด้วยกัน
  • OFF ทุกสวิตช์: ก็ปิดไฟสิครับน้อง รออะไร!

ตัวอย่าง Logic ใน Code

int sw1 = digitalRead(5);
int sw2 = digitalRead(6);
int sw3 = digitalRead(7);

if (sw1 == HIGH && sw2 == LOW && sw3 == LOW) {
  setColor(255, 0, 0); // สีแดงจัดไปวัยรุ่น
} else if (sw1 == LOW && sw2 == HIGH && sw3 == LOW) {
  setColor(0, 255, 0); // สีเขียวเหนี่ยวทรัพย์
}

การนำไปใช้งาน

การเลือกโหมดด้วย Hardware แบบนี้มันเจ๋งตรงที่น้องสามารถปรับแต่งค่าได้หน้างานแบบ Manual เลย เหมาะมากกับโปรเจกต์ที่ต้องการความไว เช่น การตั้งค่าโหมดไฟในโคมไฟเปลี่ยนสี (Mood Lamp), ทำป้ายไฟ หรือเป็นไฟ Indicator บอกสถานะใน Module ใหญ่ๆ สู้งานนะน้อง จัดไปอย่าให้เสียชื่อสายช่าง!

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

title: "RGB LED with 3 function dipswitch"
description: "Control RGB LED patterns using a 3-pin DIP switch."
author: "sathyaraj00"
category: "Lights & LEDs"
tags:
  - "rgb"
  - "dipswitch"
  - "arduino"
views: 1843
likes: 0
price: 435
difficulty: "Intermediate"
components:
  - "1x Arduino UNO"
  - "1x RGB LED (generic)"
  - "1x DIP Switch, 3 Position"
  - "3x Resistor 220 ohm"
tools: []
apps:
  - "1x Arduino IDE"
downloadableFiles: []
documentationLinks: []
passwordHash: "2b..."
encryptedPayload: "U2FsdGVkX1/..."
seoDescription: "Use a 3-pin DIP switch to toggle through different RGB LED lighting functions on your Arduino."
heroImage: "https://cdn.jsdelivr.net/gh/bigboxthailand/arduino-assets@main/images/projects/rgb-led-with-3-function-dipswitch-2b8bcc_cover.jpg"
lang: "th"