Example Code for Arduino-Relay Control
Last revision 2026/01/06
This article offers a comprehensive guide on using Arduino to control a relay, including necessary hardware and software setups, wiring instructions, and sample code to switch the relay on and off, ensuring efficient control of electrical components.
Hardware Preparation
- DFR0473 Gravity: Digital 10A Relay Module (SKU:DFR0473 x 1)
- DFRduino UNO R3 with IO Expansion Shield and USB Cable A-B (SKU:DFR0216-2 x 1)
- 1.5V AA battery (or other power supply) x 2
- LED lights (or other electrical equipment)
- Jumper wires
Software Preparation
- Development Tool: Arduino IDE (version unspecified). Download link: Arduino IDE
Wiring Diagram

Other Preparation Work
- Be ware of electric shock and burn out the circuit board.
- The input voltage of this module is 2.8 ~ 5.5V.
Sample Code
/***************************************************
Digital Relay Module (Arduino & Raspberry Pi compatible)
<https://www.dfrobot.com/wiki/index.php/Gravity:_Digital_Relay_Module_(Arduino_&_Raspberry_Pi_Compatible)_SKU:_DFR0473#Connection_Diagram>
***************************************************/
/***************************************************
This example shows how to use 3V Relay Module(DFR0473).
Created 2016-12-20
By Free Li <[email protected]>
GNU Lesser General Public License.
See <http://www.gnu.org/licenses/> for details.
All above must be included in any redistribution
****************************************************/
/***********Notice and Trouble shooting***************
1.Be ware of electric shock and burn out the circuit board.
2.The input voltage of this module is 2.8 ~ 5.5V.
****************************************************/
const int RelayPin = 2;
void setup() {
pinMode(RelayPin, OUTPUT);
}
void loop() {
digitalWrite(RelayPin, HIGH); //Turn on relay
delay(1000);
digitalWrite(RelayPin, LOW); //Turn off relay
delay(1000);
}
Result
- The LED will change its state every second, and you will hear a loud pop at the same time.
- When the module gets HIGH signal: NO terminal connects to COM port, LED is ON.
- When the module gets LOW signal: NO terminal disconnects from COM port, LED is OFF.
Was this article helpful?
