Switch a Load with a Relay (Arduino)
Use a relay module to switch a separate circuit on and off from an Arduino pin. Learn how relays drive bigger loads.
Beginner15 min
What you need
Wiring
Hover a pin or a wire to trace the connection.
Code
relay.ino
// Click a relay on and off every 2 seconds.
// NOTE: many relay modules are ACTIVE-LOW — if yours switches backwards,
// swap HIGH and LOW below.
const int relayPin = 7;
void setup() {
pinMode(relayPin, OUTPUT);
}
void loop() {
digitalWrite(relayPin, HIGH); // relay on (or off if active-LOW)
delay(2000);
digitalWrite(relayPin, LOW);
delay(2000);
}
Steps
- Wire the control side: VCC to 5V, GND to GND, IN to D7.
- Upload — you'll hear the relay click every 2 seconds.
- Put your load on COM + NO (on when activated). Start with a low-voltage load.
- ⚠️ Mains voltage is dangerous — only wire AC if you know what you're doing.