Example Code for Arduino-Control relay ON or OFF
Last revision 2026/01/07
The article provides a straightforward demonstration on how to control the Gravity: Digital 16A relay Module using Arduino, offering clear code examples for switching relays ON and OFF, ideal for beginners in electronics and programming.
Assembly Instructions

Hardware Preparation
- DFR0251 Digital 16A Relay Module (SKU:DFR0251 x 1)
- DFRduino UNO R3 with IO Expansion Shield and USB Cable A-B (SKU:DFR0216-2 x 1)
Software Preparation
- Development Tool: Arduino IDE (version unspecified). Download link: Arduino IDE
Wiring Diagram

Sample Code
/*!
* @brief A simple example to demo the control of the Gravity: Digital 16A relay
* Module
*
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (https://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [Henry]([email protected])
* @version V1.0
* @date 2020-01-02
* @get from CN: https://www.dfrobot.cn.com
* @get from EN: https://www.dfrobot.com/product-992.html
*/
//Configure the control IO pin
const int RelayPin = 2;
void setup() {
pinMode(RelayPin, OUTPUT);
}
void loop() {
//Turn the relay ON/OFF every 1s
digitalWrite(RelayPin, HIGH); //Turn on relay, COM and NO connected
delay(1000);
digitalWrite(RelayPin, LOW); //Turn off relay, COM and NC connected
delay(1000);
}
Result
- The LED indicator will blink every 1s. The sound of "tick-tock" can be heard, when the COM terminal is repeatedly connected to NC or NO。
Was this article helpful?
