A relay is a switch you can control from code, used when you need to turn on something that runs at a much higher voltage or current than an Arduino pin can handle, think light bulbs, fans, electromagnetic locks or linear actuators. The Arduino UNO R4 sends a simple LOW or HIGH signal to the relay, and the relay does the actual switching on the high-voltage side. This tutorial covers wiring a relay to the UNO R4 and writing the few lines of code needed to flip it.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino UNO R4 - Relay
What you need
- Arduino UNO R4 WiFi
- DIYables STEM V4 IoT, Compatible with Arduino Uno R4 WiFi
- DIYables STEM V4B IoT, Compatible with Arduino Uno R4 WiFi
- USB Cable Type-A to Type-C (for USB-A PC)
- USB Cable Type-C to Type-C (for USB-C PC)
- Relay
- LED Strip
- DC Power Jack
- Breadboard
- Jumper Wires
- Screw Terminal Block Shield for Arduino UNO R4
- Sensors/Servo Expansion Shield for Arduino UNO R4
- Breadboard Shield for Arduino UNO R4
- Enclosure for Arduino UNO R4
- Power Splitter for Arduino UNO R4
- Prototyping Base Plate & Breadboard Kit for Arduino UNO
- DIYables STEM V4 IoT Starter Kit (Arduino included)
- DIYables STEM V4B IoT Starter Kit (Arduino included)
- DIYables Sensor Kit (18 sensors/displays)
Wiring
A relay has two separate pin groups. The input group is low voltage and connects to the Arduino: DC- goes to GND (0V), DC+ goes to VCC (5V), and IN receives the control signal from an Arduino pin. The output group is high voltage and connects to the device you're switching, usually through a screw terminal: COM is the common pin used in both switching modes, and NO is the normally open pin used in normally open mode. Attach your chosen Arduino digital pin, for example pin 3, to the relay's IN pin.
How it works
Relays behave differently depending on how they're built and wired, so it helps to know the two settings involved. The input mode is either LOW level trigger or HIGH level trigger, meaning the relay activates when IN is driven LOW or when it's driven HIGH, depending on the specific module. The output mode is either normally open or normally closed, describing whether the circuit to your device is open or closed when IN is at LOW (0V), which is the 'normal' resting state. Together these two settings determine exactly what signal from the Arduino turns your high-voltage device on versus off.
Code
The sketch sets the Arduino pin connected to the relay's IN pin as an output using pinMode(). From there, calling digitalWrite() with LOW or HIGH on that pin switches the relay, which in turn switches the high-voltage device connected to its output terminals. Depending on your relay's trigger mode, LOW might turn the device on and HIGH off, or the reverse, so it's worth testing which way yours responds.
arduino-uno-r4-relay.ino
/*
* This Arduino UNO R4 code was developed by newbiely.com
*
* This Arduino UNO R4 code is made available for public use without any restriction
*
* For comprehensive instructions and wiring diagrams, please visit:
* https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-relay
*/
#define RELAY_PIN 3 // The Arduino UNO R4 pin connected to the IN pin of relay
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin as an output.
pinMode(RELAY_PIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(RELAY_PIN, HIGH);
delay(500);
digitalWrite(RELAY_PIN, LOW);
delay(500);
}
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-relay

Top comments (0)