ค้นหาโค้ด

รหัส Arduino และวิดีโอสำหรับรีเลย์ 5V แบบสองช่อง

รหัส Arduino และวิดีโอสำหรับรีเลย์ 5V แบบสองช่อง

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

2 chanel relay module

ตลอดกระบวนการนี้ เราจะครอบคลุมส่วนประกอบที่จำเป็น คำแนะนำการเดินสายไฟ และการอธิบายโค้ด Arduino ที่ใช้ควบคุมรีเลย์ โปรเจกต์นี้เหมาะสำหรับผู้เริ่มต้นที่ต้องการสำรวจโลกของอิเล็กทรอนิกส์และระบบอัตโนมัติ สำหรับคำแนะนำแบบภาพ อย่าลืมดูวิดีโอที่เกี่ยวข้อง (ในวิดีโอที่ 00:00)

อธิบายฮาร์ดแวร์

ส่วนประกอบหลักในโปรเจกต์นี้คือรีเลย์ 5V แบบสองช่องสัญญาณ รีเลย์นี้ประกอบด้วยสวิตช์อิสระสองตัวที่สามารถควบคุมโหลดแยกกันได้ แต่ละรีเลย์มีหน้าสัมผัสสามจุด: ปกติปิด (NC), ปกติเปิด (NO) และคอมมอน (COM) เมื่อรีเลย์ถูกกระตุ้น หน้าสัมผัส NO จะเชื่อมต่อ ทำให้กระแสไหลผ่านโหลดได้

ส่วนประกอบสำคัญอื่นๆ รวมถึงบอร์ด Arduino ซึ่งจะควบคุมรีเลย์ผ่านพินดิจิทัล โมดูลรีเลย์ยังมีไฟ LED แสดงสถานะที่สว่างขึ้นเมื่อรีเลย์ถูกกระตุ้น นอกจากนี้ รีเลย์ยังมีการแยกสัญญาณด้วยแสง (optically isolated) ซึ่งให้ความปลอดภัยโดยการแยกวงจรควบคุมออกจากวงจรโหลด

รายละเอียดจาก Datasheet

ผู้ผลิตSongle
หมายเลขชิ้นส่วนSRD-05VDC-SL-C
แรงดันไฟฟ้าลอจิก/IO5 V
แรงดันไฟฟ้าจ่าย5 V
กระแสเอาต์พุต (ต่อช่อง)10 A
กระแสสูงสุด (ต่อช่อง)10 A
คำแนะนำความถี่ PWMN/A
เกณฑ์ลอจิกอินพุตขั้นต่ำ 2.5 V
แรงดันตก / RDS(on) / อิ่มตัว80 mΩ
ขีดจำกัดความร้อน70 °C
แพ็คเกจโมดูล
หมายเหตุ / รุ่นอื่นๆแยกสัญญาณด้วยแสง

  • ตรวจสอบให้แน่ใจว่ามีการแยกสัญญาณอย่างเหมาะสมเมื่อเชื่อมต่อโหลด AC
  • ใช้ฟิวส์ที่เหมาะสมเพื่อความปลอดภัยกับโหลดสูง
  • เก็บโมดูลรีเลย์ให้ห่างจากความชื้น
  • พิจารณาการใช้แผ่นระบายความร้อนสำหรับการใช้งานที่ใช้พลังงานสูง
  • ตรวจสอบการเดินสายไฟก่อนเปิดเครื่องเพื่อหลีกเลี่ยงความเสียหาย
  • ใช้แหล่งจ่ายไฟแยกสำหรับอุปกรณ์ที่ใช้กระแสสูงหากจำเป็น

คำแนะนำการเดินสายไฟ

Arduino wiring for 2 channel relay module AC load
Arduino wiring for 2 channel relay module AC load

ในการเดินสายไฟรีเลย์สองช่องสัญญาณเข้ากับ 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 wiring for 2 channel relay module AC load
Arduino wiring for 2 channel relay module AC load
2 chanel relay module
2 chanel relay module
2 chanel relay module
2 chanel relay module
2 chanel relay module
2 chanel relay module
2 chanel relay module
2 chanel relay module
46-This is the Arduino code for a dual-channel 5V relay.
ภาษา: C++
/*
 * 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


}
47-Control AC loads using a 2-channel relay and an Arduino
ภาษา: C++
/*
 * 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


}

สิ่งที่คุณอาจต้องการ

แหล่งข้อมูลและเอกสารอ้างอิง

ยังไม่มีทรัพยากรเลย

ไฟล์📁

ไม่มีไฟล์ที่พร้อมใช้งาน