DEV Community

Cover image for Arduino - Relay
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at arduinogetstarted.com

Arduino - Relay

An Arduino output pin can switch an LED just fine, but it can't safely control a light bulb, fan, or lock that runs on high voltage or high current. A relay sits between the two: Arduino flips a low-voltage signal, and the relay does the actual switching on the high-voltage side. It's a simple way to let your sketch control real-world appliances without touching mains wiring directly.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino - Relay

What you need

Wiring

Arduino - Relay wiring diagram

The relay has two separate pin groups. The input group connects to Arduino: DC- goes to GND (0V), DC+ goes to VCC (5V), and IN receives the control signal from an Arduino pin. The output group connects to the high voltage device, usually through a screw terminal: COM is the common pin used in both modes, and NO is the normally open pin. Because relay wires are often thick and don't sit well in breadboard headers, a screw terminal shield makes the connections easier without changing the wiring itself.

How it works

Relays can behave differently depending on how they're built and wired, so it's worth checking which mode yours uses. On the input side, there are two trigger modes: in LOW level trigger mode the relay activates when IN is pulled low, in HIGH level trigger mode it activates when IN goes high. On the output side, there are two switching modes: normally open and normally closed, which behave oppositely. "Normally" here means the state when IN is at LOW (0V). Knowing which combination your relay uses tells you what digitalWrite() value actually turns your device on.

Code

Controlling the relay from code is the same pattern as blinking an LED. You set the Arduino pin connected to IN as an output with pinMode(), then use digitalWrite() to drive that pin LOW or HIGH, which switches the relay and therefore the connected high voltage device. The example sketch toggles pin 3 with delay() calls in between, so the connected LED strip blinks on and off.

arduino-relay.ino

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-relay
 */

// constants won't change
const int RELAY_PIN = 3;  // the Arduino pin, which connects 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);
}
Enter fullscreen mode Exit fullscreen mode

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://arduinogetstarted.com/tutorials/arduino-relay

Top comments (0)