บทเรียนนี้เป็นส่วนหนึ่งของ: การควบคุมรีเลย์โดยใช้ Arduino
นี่คือกลุ่มวิดีโอที่เกี่ยวข้องกับการวิ่งผลัดทั้งหมด ลิงก์ไปยังวิดีโออื่นๆ อยู่ด้านล่างบทความนี้
รหัส Arduino และวิดีโอสำหรับรีเลย์ 5V แบบสองช่อง
ในบทช่วยสอนนี้ เราจะเรียนรู้วิธีควบคุมโหลดไฟฟ้ากระแสสลับ (AC) และไฟฟ้ากระแสตรง (DC) โดยใช้รีเลย์ 5V แบบสองช่องสัญญาณร่วมกับ Arduino รีเลย์ช่วยให้คุณสลับระหว่างโหลดสองชนิดที่แตกต่างกัน เช่น หลอดไฟ AC และมอเตอร์ DC ให้ความยืดหยุ่นสำหรับการใช้งานที่หลากหลาย เมื่อจบบทช่วยสอนนี้ คุณจะสามารถตั้งค่าระบบรีเลย์ของคุณเองและควบคุมผ่านโค้ด Arduino ที่ง่ายได้

ตลอดกระบวนการนี้ เราจะครอบคลุมส่วนประกอบที่จำเป็น คำแนะนำการเดินสายไฟ และการอธิบายโค้ด Arduino ที่ใช้ควบคุมรีเลย์ โปรเจกต์นี้เหมาะสำหรับผู้เริ่มต้นที่ต้องการสำรวจโลกของอิเล็กทรอนิกส์และระบบอัตโนมัติ สำหรับคำแนะนำแบบภาพ อย่าลืมดูวิดีโอที่เกี่ยวข้อง (ในวิดีโอที่ 00:00)
อธิบายฮาร์ดแวร์
ส่วนประกอบหลักในโปรเจกต์นี้คือรีเลย์ 5V แบบสองช่องสัญญาณ รีเลย์นี้ประกอบด้วยสวิตช์อิสระสองตัวที่สามารถควบคุมโหลดแยกกันได้ แต่ละรีเลย์มีหน้าสัมผัสสามจุด: ปกติปิด (NC), ปกติเปิด (NO) และคอมมอน (COM) เมื่อรีเลย์ถูกกระตุ้น หน้าสัมผัส NO จะเชื่อมต่อ ทำให้กระแสไหลผ่านโหลดได้
ส่วนประกอบสำคัญอื่นๆ รวมถึงบอร์ด Arduino ซึ่งจะควบคุมรีเลย์ผ่านพินดิจิทัล โมดูลรีเลย์ยังมีไฟ LED แสดงสถานะที่สว่างขึ้นเมื่อรีเลย์ถูกกระตุ้น นอกจากนี้ รีเลย์ยังมีการแยกสัญญาณด้วยแสง (optically isolated) ซึ่งให้ความปลอดภัยโดยการแยกวงจรควบคุมออกจากวงจรโหลด
รายละเอียดจาก Datasheet
| ผู้ผลิต | Songle |
|---|---|
| หมายเลขชิ้นส่วน | SRD-05VDC-SL-C |
| แรงดันไฟฟ้าลอจิก/IO | 5 V |
| แรงดันไฟฟ้าจ่าย | 5 V |
| กระแสเอาต์พุต (ต่อช่อง) | 10 A |
| กระแสสูงสุด (ต่อช่อง) | 10 A |
| คำแนะนำความถี่ PWM | N/A |
| เกณฑ์ลอจิกอินพุต | ขั้นต่ำ 2.5 V |
| แรงดันตก / RDS(on) / อิ่มตัว | 80 mΩ |
| ขีดจำกัดความร้อน | 70 °C |
| แพ็คเกจ | โมดูล |
| หมายเหตุ / รุ่นอื่นๆ | แยกสัญญาณด้วยแสง |
- ตรวจสอบให้แน่ใจว่ามีการแยกสัญญาณอย่างเหมาะสมเมื่อเชื่อมต่อโหลด AC
- ใช้ฟิวส์ที่เหมาะสมเพื่อความปลอดภัยกับโหลดสูง
- เก็บโมดูลรีเลย์ให้ห่างจากความชื้น
- พิจารณาการใช้แผ่นระบายความร้อนสำหรับการใช้งานที่ใช้พลังงานสูง
- ตรวจสอบการเดินสายไฟก่อนเปิดเครื่องเพื่อหลีกเลี่ยงความเสียหาย
- ใช้แหล่งจ่ายไฟแยกสำหรับอุปกรณ์ที่ใช้กระแสสูงหากจำเป็น
คำแนะนำการเดินสายไฟ

ในการเดินสายไฟรีเลย์สองช่องสัญญาณเข้ากับ Arduino ให้เริ่มต้นโดยเชื่อมต่อพิน VCC ของโมดูลรีเลย์เข้ากับพิน 5V บน Arduino เชื่อมต่อพิน GND ของรีเลย์เข้ากับพิน GND บน Arduino ซึ่งจะจ่ายไฟให้กับโมดูลรีเลย์
จากนั้น เชื่อมต่อพินควบคุม: เชื่อมต่อพิน IN1 ของรีเลย์เข้ากับพินดิจิทัล 7 บน Arduino และพิน IN2 เข้ากับพินดิจิทัล 8 การตั้งค่านี้จะช่วยให้คุณควบคุมรีเลย์ทั้งสองได้อย่างอิสระ สุดท้าย เชื่อมต่อโหลด AC หรือ DC ของคุณเข้ากับหน้าสัมผัสรีเลย์ตามความต้องการ โดยปฏิบัติตามข้อควรระวังด้านความปลอดภัยอย่างเหมาะสมเมื่อจัดการกับแรงดันไฟฟ้า AC
ตัวอย่างโค้ดและคำอธิบาย
ในโค้ด Arduino เราเริ่มต้นด้วยการเริ่มต้นการสื่อสารแบบอนุกรมและตั้งค่าโหมดพินสำหรับพินควบคุมรีเลย์ ตัวแปร relay1Pin และ relay2Pin ถูกกำหนดเพื่อแทนพินที่เชื่อมต่อกับรีเลย์แต่ละตัว จากนั้นสถานะเอาต์พุตของพินเหล่านี้จะถูกสลับในลูป
const int relay1Pin = 7; // กำหนดพินสำหรับรีเลย์ 1
const int relay2Pin = 8; // กำหนดพินสำหรับรีเลย์ 2
void setup() {
Serial.begin(9600); // ตั้งค่า Serial Monitor เพื่อแสดงข้อมูล
pinMode(relay1Pin, OUTPUT); // เชื่อมต่อกับรีเลย์ 1
pinMode(relay2Pin, OUTPUT); // เชื่อมต่อกับรีเลย์ 2
}
ข้อความนี้เริ่มต้น Serial Monitor เพื่อแสดงข้อมูลการดีบักและตั้งค่าพินควบคุมรีเลย์เป็นเอาต์พุต ซึ่งสำคัญต่อการทำให้แน่ใจว่ารีเลย์สามารถเปิดและปิดได้ตามต้องการ
void loop() {
digitalWrite(relay2Pin, LOW); // ปิดรีเลย์ 2
Serial.print("Pin 8 LOW");
digitalWrite(relay1Pin, HIGH); // เปิดรีเลย์ 1
Serial.println(" Pin 7 HIGH");
delay(3000); // คงสถานะรีเลย์ 2 ปิดและรีเลย์ 1 เปิดเป็นเวลา 3 วินาที
digitalWrite(relay1Pin, LOW); // ปิดรีเลย์ 1
digitalWrite(relay2Pin, HIGH); // เปิดรีเลย์ 2
Serial.print("Pin 7 LOW");
Serial.println(" Pin 8 HIGH");
delay(3000); // คงสถานะรีเลย์ 1 ปิดและรีเลย์ 2 เปิดเป็นเวลา 3 วินาที
}
โค้ดนี้สลับรีเลย์ทุกสามวินาที โดยเปิดตัวหนึ่งในขณะที่ปิดอีกตัวหนึ่ง Serial Monitor แสดงสถานะของพิน ซึ่งช่วยในการดีบักและทำความเข้าใจการทำงานของรีเลย์
การสาธิต / สิ่งที่คาดหวัง
เมื่อการตั้งค่าเสร็จสมบูรณ์ คุณควรเห็นรีเลย์สลับระหว่างโหลดที่เชื่อมต่อทุกสามวินาที รีเลย์จะกระตุ้นหลอดไฟ AC หรือมอเตอร์ DC ที่เชื่อมต่อสลับกัน ควรระมัดระวังเมื่อทำงานกับโหลด AC เนื่องจากการจัดการที่ไม่ถูกต้องอาจนำไปสู่สถานการณ์อันตราย (ในวิดีโอที่ 12:34)
ไทม์สแตมป์วิดีโอ
- 00:00 - แนะนำรีเลย์สองช่องสัญญาณ
- 04:15 - คำแนะนำการเดินสายไฟ
- 08:30 - คำอธิบายโค้ด
- 10:45 - การสาธิตรีเลย์
บทเรียนนี้เป็นส่วนหนึ่งของ: การควบคุมรีเลย์โดยใช้ Arduino
- ควบคุมรีเลย์ 5V ด้วย Arduino เพื่อควบคุมโหลด AC หรือ DC เช่น หลอดไฟหรือมอเตอร์
- TTP224 4 ช่องเซนเซอร์สัมผัสสำหรับเปิด/ปิดโหลด AC/DC ด้วยรีเลย์
- การใช้โมดูลรีเลย์ 5V (แบบกระตุ้นต่ำ) กับ Arduino
- ใช้ MAX6675 เทอร์โมคัปเปิลชนิด K ร่วมกับรีเลย์และจอแสดงผล
- การใช้รีดสวิตช์เพื่อควบคุมรีเลย์และโหลดไฟฟ้ากระแสสลับ/กระแสตรงด้วยอาร์ดูอิโน
- ใช้โมดูลสัมผัส TTP223B และรีเลย์เพื่อควบคุมโหลด AC/DC ด้วย Arduino
- การใช้ปุ่มกด Arduino เพื่อสลับรีเลย์และหลอดไฟ AC
/*
* This is the Arduino code for Dual Channel 5V Relay
* to control turning ON or OFF AC or DC loads. You can control 2 different loads such as bulbs, or a heater, or other machines.
Watch the video instruction for this code: https://youtu.be/EclIomn_3dI
* *
* Written by Ahmad Shamshiri for Robojax Video
* Date: December 26, 2017, in Ajax, Ontario, Canada
* Permission granted to share this code given that this
* note is kept with the code.
* Disclaimer: this code is "AS IS" and for educational purposes only.
Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in a structured course with all materials, wiring diagrams and libraries
all in one place. Purchase my course on Udemy.com: http://robojax.com/L/?id=62
If you found this tutorial helpful, please support me so I can continue creating
content like this. You can support me on Patreon: http://robojax.com/L/?id=63
or make a donation using PayPal: http://robojax.com/L/?id=64
*
* This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
void setup() {
Serial.begin(9600);// setup Serial Monitor to display information
pinMode(7, OUTPUT);// connected to Relay 1
pinMode(8, OUTPUT);// connected to Relay 2
}
void loop() {
digitalWrite(8, LOW); // turn relay 2 OFF
Serial.print("Pin 8 LOW");
digitalWrite(7,HIGH);// turn relay 1 ON
Serial.println(" Pin 7 HIGH");
delay(3000);// keep in relay 2 OFF and relay 1 On for 3 seconds
digitalWrite(7, LOW);// turn relay 1 OFF
digitalWrite(8,HIGH);// turn relay 2 ON
Serial.print("Pin 7 LOW");
Serial.println(" Pin 8 HIGH");
delay(3000);// keep in relay 1 OFF and relay 2 On for 3 seconds
}
/*
* This is the Arduino code for a dual-channel 5V relay
* to control turning ON or OFF AC or DC loads. You can control 2 different loads such as bulbs, a heater, or other machines.
Watch the video instruction for this code: https://youtu.be/EclIomn_3dI
* *
* Written by Ahmad Shamshiri for Robojax Video
* Date: December 26, 2017, in Ajax, Ontario, Canada
* Permission granted to share this code, given that this
* note is kept with the code.
* Disclaimer: This code is "AS IS" and for educational purposes only.
Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in a structured course with all materials, wiring diagrams, and libraries
all in one place. Purchase my course on Udemy.com: http://robojax.com/L/?id=62
If you find this tutorial helpful, please support me so I can continue creating
content like this. You can support me on Patreon: http://robojax.com/L/?id=63
or make a donation using PayPal: http://robojax.com/L/?id=64
*
* This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const int relay1Pin =7;// define pin for relay 1
const int relay2Pin =8; // define pin for relay 2
void setup() {
Serial.begin(9600);// setup Serial Monitor to display information
pinMode(relay1Pin, OUTPUT);// connected to Relay 1
pinMode(relay2Pin, OUTPUT);// connected to Relay 2
}
void loop() {
digitalWrite(relay2Pin, LOW); // turn relay 2 OFF
Serial.print("Pin 8 LOW");
digitalWrite(relay1Pin,HIGH);// turn relay 1 ON
Serial.println(" Pin 7 HIGH");
delay(3000);// keep in relay 2 OFF and relay 1 On for 3 seconds
digitalWrite(relay1Pin, LOW);// turn relay 1 OFF
digitalWrite(relay2Pin,HIGH);// turn relay 2 ON
Serial.print("Pin 7 LOW");
Serial.println(" Pin 8 HIGH");
delay(3000);// keep in relay 1 OFF and relay 2 On for 3 seconds
}
สิ่งที่คุณอาจต้องการ
-
อาลีเอ็กซ์เพรสBuy 5V/12V 1, 2, 4, 6, or 8-channel relay modules.click.aliexpress.com
แหล่งข้อมูลและเอกสารอ้างอิง
ยังไม่มีทรัพยากรเลย
ไฟล์📁
ไม่มีไฟล์ที่พร้อมใช้งาน