Lesson 1.14: Relay
A relay lets a 5 V signal control a completely separate circuit: another supply, another voltage, another current. It's the bridge between the Arduino and loads it can't handle by itself.
Parts
How it works
Inside there's a coil and a moving contact. When current flows, the coil becomes an electromagnet and moves the contact from NC (normally closed) to NO (normally open). You hear a click on every change.
The coil of a 5 V relay draws around 70–90 mA, too much for a pin. An NPN transistor acts as the middleman: the pin only supplies the small base current, and the transistor carries the coil current.
When the current is cut, the coil generates a reverse voltage spike that can destroy the transistor. A diode across the coil, cathode towards positive, gives it a safe path.
The relay's contacts are isolated from the control circuit: that's why the GNDs don't need joining here. In this course they're only used with low voltages; never connect mains power (110–230 V) on a breadboard.
Wiring
| Component | Connects to | Note |
|---|---|---|
| Transistor, base | Pin 7 | Through a 1 kΩ resistor |
| Transistor, emitter | GND | |
| Transistor, collector | Relay, coil (terminal 1) | |
| Relay, coil (terminal 2) | 5V | |
| Diode | Across the coil | Cathode (the band) towards 5V |
| Relay, COM | Power module 5V rail | |
| Relay, NO | Motor, terminal 1 | |
| Motor, terminal 2 | Power module GND rail |
Code
// Switches the relay on for two seconds out of every four.
const int RELAY_PIN = 7;
void setup() {
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(RELAY_PIN, HIGH); // transistor conducts: coil energised
Serial.println("Relay on: motor running");
delay(2000);
digitalWrite(RELAY_PIN, LOW);
Serial.println("Relay off: motor stopped");
delay(2000);
}What the code does
- The program doesn't know there's a relay: it sets a pin HIGH or LOW, just like with an LED. The circuit does all the work.
- With HIGH on the base the transistor conducts, and the coil current flows from 5V to GND through it.
- The two-second pauses are kind to the relay: its contacts take a few milliseconds to move and it isn't made to switch hundreds of times a second.
- If you use a relay module instead of a bare relay, check whether it switches on with HIGH or LOW: many work the other way round.
Step by step
- Identify the transistor's legs in its datasheet, and the relay's: two for the coil, COM, NO and NC.
- Build the control circuit: pin 7, resistor, transistor, coil and diode.
- Build the load circuit separately, with the power module: COM, NO and the motor.
- Upload the code and open the serial monitor at 9600 baud.
Result
Every two seconds the relay clicks and the motor starts or stops, even though it runs from a different supply than the Arduino.
If it doesn't work
- If you hear the click but the motor doesn't start, the load is on NC instead of NO, or the power module is off.
- If there's no click, check the transistor: with its legs in another order it won't conduct.
- If the board resets or the transistor heats up when switching, the diode is missing or reversed.