本教程是的一部分: 使用 Arduino 控制继电器
这里是所有与接力赛相关的视频合集。其他视频的链接在本文下方。
Arduino Code and Video for a Dual-Channel 5V Relay
In this tutorial, we will learn how to control AC and DC loads using a dual-channel 5V relay with an Arduino. The relay allows you to switch between two different loads, such as an AC bulb and a DC motor, providing flexibility for various applications. By the end of this guide, you'll be able to set up your own relay system and control it through simple Arduino code.

Throughout the process, we'll cover the necessary components, wiring instructions, and a walkthrough of the Arduino code used to control the relay. This project is perfect for beginners looking to explore the world of electronics and automation. For a visual guide, be sure to watch the associated video (in video at 00:00).
Hardware Explained
The main component in this project is the dual-channel 5V relay. This relay consists of two independent switches that can control separate loads. Each relay has three contacts: Normally Closed (NC), Normally Open (NO), and Common (COM). When the relay is activated, the NO contact connects, allowing current to flow through the load.
Other essential components include the Arduino board, which will control the relay through its digital pins. The relay module also features indicator LEDs that light up when the relay is activated. Additionally, the relay is optically isolated, providing safety by separating the control circuit from the load circuit.
Datasheet Details
| Manufacturer | Songle |
|---|---|
| Part number | SRD-05VDC-SL-C |
| Logic/IO voltage | 5 V |
| Supply voltage | 5 V |
| Output current (per channel) | 10 A |
| Peak current (per channel) | 10 A |
| PWM frequency guidance | N/A |
| Input logic thresholds | 2.5 V min |
| Voltage drop / RDS(on) / saturation | 80 mΩ |
| Thermal limits | 70 °C |
| Package | Module |
| Notes / variants | Optically isolated |
- Ensure proper isolation when connecting AC loads.
- Use appropriate fuses for safety with high loads.
- Keep the relay module away from moisture.
- Consider heatsinking for high-power applications.
- Verify wiring before powering on to avoid damage.
- Use separate power supplies for high-current devices if necessary.
Wiring Instructions

To wire the dual-channel relay to the Arduino, start by connecting the VCC pin of the relay module to the 5V pin on the Arduino. Connect the GND pin of the relay to the GND pin on the Arduino. This provides power to the relay module.
Next, connect the control pins: link the IN1 pin of the relay to digital pin 7 on the Arduino and the IN2 pin to digital pin 8. This setup will allow you to control both relays independently. Finally, connect your AC or DC loads to the relay contacts according to your needs, ensuring proper safety precautions are followed when dealing with AC voltage.
Code Examples & Walkthrough
In the Arduino code, we begin by initializing the serial communication and setting the pin modes for the relay control pins. The variables relay1Pin and relay2Pin are defined to represent the pins connected to each relay. The output states of these pins are then toggled in the loop.
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
}
This excerpt initializes the serial monitor to output debug information and sets the relay control pins as outputs. This is crucial for ensuring the relays can be turned on and off as needed.
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
}
This code toggles the relays every three seconds, turning one on while turning the other off. The serial monitor outputs the state of the pins, which helps in debugging and understanding the relay operations.
Demonstration / What to Expect
When the setup is complete, you should see the relay switching between the connected loads every three seconds. The relay will activate the connected AC bulb or DC motor alternately. Be cautious when working with AC loads, as improper handling can lead to dangerous situations (in video at 12:34).
Video Timestamps
- 00:00 - Introduction to the dual-channel relay
- 04:15 - Wiring instructions
- 08:30 - Code explanation
- 10:45 - Demonstration of the relay
本教程是……的一部分: 使用 Arduino 控制继电器
- Controlling a 5V Relay Using Arduino to cotrol AC or DC load like bulb or motor
- TTP224 4-Channel Touch Sensor to Turn AC/DC Loads with Relay
- 使用5V继电器模块(低触发)与Arduino
- Using a MAX6675 K-Type Thermocouple with Relay and Display
- Using a Reed Switch to Control a Relay and AC/DC Loads with an Arduino
- Using a TTP223B touch module and relay to control AC/DC loads with an Arduino
- Using an Arduino push button to switch a relay and AC bulb
/*
* 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
}
|||您可能需要的东西
-
全球速卖通购买 5v 12v 1 2 4 6 8 通道继电器模块s.click.aliexpress.com
资源与参考
尚无可用资源。
文件📁
没有可用的文件。