Your Arduino is excellent at “thinking” and processing data, but it is fundamentally a low-power device. Its digital pins can output a maximum of 5 Volts and about 40 milliamps of current. That’s enough to light up an LED or run a small sensor, but what if you want your Arduino to turn on a 12V water pump, a loud siren, or an LED light strip?
To bridge the gap between low-power microcontrollers and high-power devices, we use a Relay.
A relay module allows a small 5V signal to mechanically switch a completely separate, high-power circuit.
A relay is simply an electromechanical switch. Inside the little blue box on a relay module is an electromagnet. When the Arduino sends a small 5V signal to the relay, the electromagnet turns on, magnetically pulling a physical metal contact to close a much larger switch.
Because the low-voltage side (Arduino) and the high-voltage side (the device you are controlling) are physically separated by air, your Arduino is protected from dangerous power surges.
Note: While relays can technically switch mains AC voltage (like wall outlets), we strongly advise beginners to stick to DC voltages under 24V. Working with mains AC power can be deadly if you make a mistake.
On the high-power side of the relay (the screw terminals), you will see three connections:
For most projects, you will wire your device between COM and NO.
Let’s connect the low-voltage side to the Arduino. It only takes three wires!
The code for a relay is brilliantly simple. To the Arduino, a relay module acts exactly like a giant LED. You just turn the pin HIGH or LOW.
int relayPin = 7;
void setup() {
// Set the relay pin as an output
pinMode(relayPin, OUTPUT);
}
void loop() {
// Turn the relay ON
digitalWrite(relayPin, HIGH);
delay(5000); // Wait 5 seconds
// Turn the relay OFF
digitalWrite(relayPin, LOW);
delay(5000); // Wait 5 seconds
}
Upload the code. You will hear a satisfying click every 5 seconds as the electromagnet physically pulls the switch open and closed.
Try pairing this with the PIR Motion Sensor from Lesson 123 to create a motion-activated light!