How to connect a relay to Arduino
5 V module on D7, triggered by LOW.
Wiring diagram
Open in the builderStep-by-step assembly
3 steps- 3 wires in order: ground, power, signals.Click a wire on the diagram or a table row to open its step.The ← → arrow keys flip through the steps.
- Step 1: GND of the relay to GND of the board
- Step 2: VCC of the relay to 5V of the board5 V. The relay draws about 75 mA, the pin supplies about 400 mA.
- Step 3: IN of the relay to D7 of the board
- Done: 3 wires in place.Check against the connection table below.
Connections
| Step | From | To | Color |
|---|---|---|---|
| 1 | Arduino Uno GND | Relay GND | black |
| 2 | Arduino Uno 5V | Relay VCC | red |
| 3 | Arduino Uno 7 | Relay IN | white |
Power and wiring check
- The schematic check, the same as in the builder, found no errors: pin voltages match, ground is common, buses are fully connected.
- Relay draws about 75 mA from the 5V pin of Arduino Uno, the pin supplies about 400 mA: USB gives 500 mA, the board takes part of it.
Current of the whole circuit and how long it runs on a battery: power budget. Where to plug what: breadboard layout.
Example code
Arduino IDE, no libraries.
const int RELAY = 7; // the relay turns on with a low level
void setup() {
digitalWrite(RELAY, HIGH); // off first, then make it an output
pinMode(RELAY, OUTPUT);
}
void loop() {
digitalWrite(RELAY, LOW); // turn on
delay(2000);
digitalWrite(RELAY, HIGH); // turn off
delay(2000);
}
Part pinouts
Next steps for this circuit
How it is connected
A relay module with an optocoupler turns on with a low level on IN: digitalWrite(7, LOW) closes COM and NO. That's why in setup you first write HIGH and only then make the pin an output, otherwise the relay clicks at startup.
The coil draws about 70 mA from the module's VCC. The JD-VCC to VCC jumper is fitted, so the coil runs from the same 5 V. One or two modules work from the Arduino's 5V pin on USB, while four or more need a separate 5 V supply: remove the jumper, connect the supply to JD-VCC and GND, and leave VCC on the Arduino.
The load connects to the terminals: COM and NO for "turn on on command", COM and NC for "turn off on command". 220 V mains on an open module is dangerous: the contacts and traces have no enclosure, so keep the current within 5 A.
FAQ
- Can I drive this relay from an ESP32?
- The ESP32's high level is 3.3 V, and a 5 V module may not turn off with it. Remove the JD-VCC jumper and put 3.3 V on VCC and 5 V on JD-VCC, or use a 3.3 V relay module.
- Do I need a diode across the coil?
- The module already has one, along with the transistor. You need a diode if the relay is bare and driven by your own transistor.