Click-Clack: How to Control High Voltage with Arduino Relays (Safely)

Click-Clack: How to Control High Voltage with Arduino Relays (Safely)


📑Table of Contents
What You'll Need 4 items
Arduino Uno R3
Buy
5V Relay Module (1-Channel)
Buy
Breadboard
Buy
Jumper Wires
Buy

Affiliate links help support this site at no extra cost to you.

Welcome to Day 18. Yesterday, we learned how to spin a small motor using a Transistor. That was fun. But transistors have limits.

  • They get hot.
  • They only work for DC (Direct Current).
  • If you connect them to AC (your wall outlet), they will explode.

Today, we go old school. We are going to use a component that was invented in 1835. It makes a satisfying “Click-Clack” sound. It can control 220V/110V AC logic safely. It is the Electromechanical Relay.

Safety Warning: Today’s theory involves High Voltage concepts. DO NOT cut open your desk lamp cord and wire it up unless you are a certified electrician or have adult supervision. We will learn the Theory and simulate the High Voltage part with 12V LEDs. Electricity is invisible and it hurts. Respect it.

Intro Art: Sound waves from a mechanical relay click

What is a Relay?

A relay is simply A Switch operated by an Electromagnet. Inside the blue plastic box, there is no silicon magic. It is purely mechanical.

The Anatomy of a Click

  1. The Coil: A coil of copper wire. When we send 5V through it, it becomes a Magnet.
  2. The Armature: A metal lever held by a spring.
  3. The Contacts: Two pieces of metal that touch (ON) or separate (OFF).

The Sequence:

  1. Arduino sends 5V.
  2. Coil becomes Magnetic.
  3. Magnet pulls the Armature down (“CLICK!”).
  4. Contacts touch. Power flows.
  5. Arduino stops 5V.
  6. Spring pulls Armature back (“CLACK!”).
  7. Power stops.

Relay Internal Diagram: Coil, Magnet, Spring

Galvanic Isolation: The Safety Gap

This is the most important concept of the day. Inside a Relay, the Coil Circuit (Arduino side) and the Contact Circuit (High Voltage side) are physically separated. There is no wire connecting them. They are separated by air and plastic. This is called Galvanic Isolation.

If a lightning bolt hits your desk lamp, it might melt the Relay Contacts. But it cannot travel back to your Arduino because there is no path. The relay sacrifices itself to save the microcontroller.

The Relay Module

Do not buy a “bare” relay component. Relays need a lot of current (70mA+) to energize the coil. The Arduino pins can only give 40mA. (Remember Day 17: “Brain vs Muscle”). So, we use a Relay Module. This module contains:

  1. The Blue Relay.
  2. A Transistor (to drive the coil).
  3. A Flyback Diode (to eat the coil’s kickback).
  4. Screw Terminals (for thick wires).

Relay Module Pinout: NO, NC, COM, IN

Critical Warning: The Power Budget

A single relay consumes about 70mA to 90mA when active. The Arduino’s 5V pin can supply about 400mA total (if powered via USB).

  • 1 Relay = 70mA (Fine).
  • 4 Relays = 280mA (Borderline).
  • 8 Relays = 560mA (CRASH).

If you are using a 4-Channel or 8-Channel Relay module, you MUST use an external 5V power supply. If you try to power 8 relays from the Arduino, the voltage will sag, and the Arduino will reset randomly.

Historical Fun Fact: The First “Bug”

We call software errors “Bugs”. Do you know why? In 1947, computer pioneer Grace Hopper was working on the Harvard Mark II computer. It wasn’t made of silicon; it was made of thousands of Electromechanical Relays. The computer failed. They opened it up and found a real moth stuck between the contacts of Relay #70. The moth prevented the contacts from touching. She taped the dead moth into the logbook with the note: “First actual case of bug being found.” Every time your code fails, you are paying homage to that moth.

Logic Class: NO, NC, and COM

Relays have three high-voltage terminals. Understanding them is critical.

  1. COM (Common): The Center pin. This is where you connect your Power Source (Line).
  2. NO (Normally Open): This connection is Broken by default.
    • Relay OFF -> Disconnected.
    • Relay ON -> Connected.
    • Usage: Turning a lamp ON.
  3. NC (Normally Closed): This connection is Connected by default.
    • Relay OFF -> Connected.
    • Relay ON -> Disconnected.
    • Usage: Security Alarm (Triggers if power fails).

The Door Analogy:

  • NO is like a Door that is normally shut. You have to push to open it (allow traffic).
  • NC is like a Door that is normally open. You have to push to close it (block traffic).

NO vs NC Analogy: Door visualization

The Build: The “Safety” Lamp

We will simulate a High Voltage lamp using a specialized setup. Components:

  1. Arduino Uno.
  2. Relay Module (1-Channel).
  3. 9V Battery (Simulating AC Source).
  4. DC Motor or LED Strip (Simulating the Lamp).
  5. Jumper Wires.

Wiring Instructions (Control Side):

  1. VCC: To Arduino 5V.
  2. GND: To Arduino GND.
  3. IN (Signal): To Pin 7.

Wiring Instructions (High Power Side):

  1. COM: To 9V Battery Positive.
  2. NO: To Motor Positive.
  3. Motor Negative: To 9V Battery Negative.

Notice: The 9V Battery Negative does NOT connect to Arduino GND here. Isolation is maintained!

Schematic: Relay Driver Circuit

The Code: Simple Clicker

The code is laughable simple. Because the Relay Module has a transistor built-in, we just use digitalWrite.

const int relayPin = 7;

void setup() {
  pinMode(relayPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("System Ready");
}

void loop() {
  Serial.println("Lights ON");
  digitalWrite(relayPin, HIGH); // CLICK!
  delay(2000);
  
  Serial.println("Lights OFF");
  digitalWrite(relayPin, LOW); // CLACK!
  delay(2000);
}

Important Pro Tip: Cycle Protection Relays are mechanical. They have a physical lifespan (e.g., 100,000 cycles). If you write a bug in your code that toggles the relay 100 times a second:

  1. It will sound like a machine gun.
  2. It will burn out in 15 minutes.
  3. The inductive spikes might reset your Arduino.

Always implement a “Cooldown” timer in your logic. Don’t allow the relay to switch state more than once every 2 seconds.

unsigned long lastSwitchTime = 0;
const int COOLDOWN = 2000;

void toggleRelay() {
  if (millis() - lastSwitchTime > COOLDOWN) {
     // Safe to switch
     digitalWrite(relayPin, !digitalRead(relayPin));
     lastSwitchTime = millis();
  }
}

Troubleshooting: The “Active Low” Trap Some Relay Modules are “Active Low”. This means LOW turns them ON, and HIGH turns them OFF. It works backwards. If your relay starts ON when you expect OFF, just swap your HIGH/LOW logic. digitalWrite(relayPin, !state);

Breadboard Setup: Wiring Render

Deep Dive: Solid State Relays (SSR)

Mechanical relays are great, but they have flaws:

  1. Speed: They are slow (10ms switching time). You cannot do PWM (Dimming) with them.
  2. Noise: They make clicking sounds.
  3. Wear: The metal contacts spark (“Arcing”) every time they close. Eventually, they burn out (after ~100,000 clicks).

The Solution? The Logic Relay. SSR (Solid State Relay). It uses massive internal SCRs or Triacs (Silicon).

  • Pros: Silent, Fast, Infinite Life.
  • Cons: Expensive, can generate heat.

For dimming lights? Use an SSR. For turning on a water pump once a day? Use a Mechanical Relay.

SSR vs Mechanical Comparison

You will encounter many relays in your career.

  1. Reed Relay: Tiny glass tube inside a coil. Used for very fast, low-power signal switching (like in older telephone exchanges).
  2. Automotive Relay: The big black cube in your car’s fuse box. Designed to switch 30A-40A at 12V. Great for robotics.
  3. Latching Relay: A special relay that stays ON even if power is cut. It has two coils (Set and Reset).
  4. Contactor: A relay on steroids. Used to switch entire buildings or industrial motors (100A+). The “Click” sounds like a gunshot.

Advanced Logic: The Snubber Circuit

When you switch off an AC load (like a Fan), it creates a voltage spike (Inductive Kickback), just like our DC motor yesterday. In AC, this causes Arcing across the Relay contacts. It looks like blue lightning inside the plastic box. This melts the contacts and they might weld shut (Stick ON).

The Fix: A Snubber Network. A Resistor (100Ω) and a Capacitor (0.1uF) in series, placed across the relay contacts. It absorbs the arc. Many high-quality relay modules come with Snubbers built-in.

Challenge: The Smart Home Hub

You have:

  1. Button (Day 2).
  2. Relay (Day 18).
  3. LDR (Day 16).

Combine them. Mode 1: Push Button -> Toggle Light (Manual Override). Mode 2: If Dark -> Turn ON Light (Auto).

const int button = 2;
const int relay = 7;
const int ldr = A0;
bool manualMode = false;

void loop() {
  if (digitalRead(button) == HIGH) {
    manualMode = !manualMode; // Toggle Mode
    delay(500); // Debounce
  }
  
  if (manualMode) {
    // Just toggle relay logic here
  } else {
    // Auto Mode
    if (analogRead(ldr) < 300) digitalWrite(relay, HIGH);
    else digitalWrite(relay, LOW);
  }
}

Smart Home Hub Design Concept

The Self-Latching Circuit (Memory without Code)

Here is a “Magic Trick” you can do with relays. You can create a circuit that “Remembers” it is ON, even after you release the button. This is how elevator buttons worked in the 1920s.

The Concept:

  1. Connect a Push Button in parallel with the Relay’s Own NO contact.
  2. When you push the button: Coil energizes -> Contact closes.
  3. Power now flows through the Contact to keep the Coil energized.
  4. You release the button. The Coil stays energized because it is feeding itself!
  5. To turn it OFF, you need a second button (NC) to break the loop.

This is the grandfather of the Silicon Flip-Flop (Bit Memory).

Decoding the Label: What do the numbers mean?

Look closely at the text printed on your blue relay. It usually says: 10A 250VAC | 10A 30VDC.

  1. 10A 250VAC: This is the Absolute Maximum for AC.

    • It can handle 250 Volts AC.
    • It can handle 10 Amps.
    • However: for Inductive loads (Motors), de-rate this by 50%. So only 5A.
  2. 10A 30VDC: This is the limit for DC.

    • Notice the Voltage is much lower (30V).
    • WHY? Because DC doesn’t cross zero (AC crosses zero 100 times a second).
    • If you try to switch 100V DC, the arc will never extinguish. It will burn the relay instantly.
    • If you try to switch 100V DC, the arc will never extinguish. It will burn the relay instantly.
    • Never use these relays for High Voltage DC (like Solar panels or EV batteries).

Deep Dive: The Physics of the Arc (AC vs DC)

Why is DC so dangerous? When you open a switch, the electricity wants to keep flowing. It ionizes the air, creating a plasma bridge (an Arc).

  • AC Power: The voltage hits “Zero Volts” 100 times a second (50Hz/60Hz sine wave). Every time it hits zero, the arc dies. The air cools down. The arc is extinguished.
  • DC Power: The voltage is constant. It never hits zero. The arc sustains itself. You can pull a 1-inch arc from a 50V DC source. This plasma is 5000°C. It melts plastic, copper, and hope. Rule: Respect DC ratings. They are there for a reason.

Isolation Upgrade: The JD-VCC Jumper

If you look at your Relay Module, you might see a yellow jumper connecting VCC and JD-VCC.

  • With Jumper (Default): The Arduino powers the Relay Coil directly (via the module’s transistor).
    • Risk: Current spikes from the coil might reset the Arduino.
  • Without Jumper (Advanced):
    • Remove the jumper.
    • Connect Arduino 5V to VCC.
    • Connect External 5V Power Supply to JD-VCC and GND.
    • Do NOT connect Arduino GND to Module GND.
    • Now, the Arduino only powers the Optocoupler LED (tiny current). The External supply does the heavy lifting.
    • This provides Total Galvanic Isolation. The Arduino is electrically invincible.

Diagnostic Trick: The Flashlight Test How do you know if your relay module is truly isolated? Look at the circuit board. Find the black chip with 4 legs (usually an EL817 or PC817). Trace the traces. If there is a clear “Gap” on the PCB where no copper crosses (except under that chip), you have an isolated module. Cheap modules often skip this. Always check the PCB layout.

Troubleshooting: Contact Welding (The “Sticky” Light)

One day, your relay will fail. You will send digitalWrite(LOW), you will hear the “Clack”, but the light will stay ON. Why? The metal contacts have Welded together. This happens if you switch a load that has a huge “Inrush Current” (like a large motor or a cheap LED driver). The spark melted the metal, and they fused. The Fix:

  1. Power off immediately.
  2. Take the handle of a screwdriver and give the relay a sharp Tap. The physical shock might break the weld.
  3. Replace the relay. Once it welds once, the surface is rough. It will weld again soon. Prevention: Use a bigger relay or an Inrush Current Limiter (NTC Thermistor).

Important: High Voltage Safety Rules

  1. Unplug First: Never touch wires while plugged into the wall.
  2. One Hand Rule: keep one hand in your pocket when working with high voltage. If you get shocked, electricity won’t travel across your chest (Heart).
  3. Enclosures: Always put 110V/220V projects inside a plastic box. Exposed screws are deadly.
  4. Wire Gauge: Use thick wires for the relay contacts. Thin jump wires will melt and catch fire.
  5. Fire Safety: Keep a Class C (Electrical) fire extinguisher nearby. Never use water on an electrical fire.
  6. The “Switch” Rule: Always switch the Live (Hot) wire, never the Neutral. If you switch Neutral, the lightbulb socket is still “Live” even when off.

The Bill of Materials (BOM)

ComponentQuantityValueNotes
Arduino Uno1AnyThe Brain.
Relay Module15V 1-ChannelEnsure it says “5V DC” on the coil label. “SRD-05VDC-SL-C” is standard.
Jumper Wires10M-FMale-to-Female wires are needed for the module pins.
Load1DC Motor/LEDA safe low-voltage test load.
Power Supply19V/12VTo power the Load.
Screwdriver1Small PhilipsTo tighten the Relay Module terminals.
Wire Stripper1AnyTo strip the ends of thick wires.
Sound Sensor1KY-037Optional: For the “Clapper” logic.
Multimeter1AnyTo verify Voltage before connecting loads.

Conclusion

You have mastered the Switch.

  • Transistor: Fast, DC only, Low/Medium Power.
  • Relay: Slow, AC/DC, Massive Power, Isolated.

You now possess the ability to control literally any electrical device in your home. The barrier between “Digital Code” and “Physical Reality” is gone. You can write a if() statement that turns on your coffee machine.

Tomorrow, on Day 19, we look at the Fourth Dimension. Time. We will learn how to make our projects aware of the exact time and date. We are building a Real Time Clock (RTC). No more delay(1000). We are going to schedule events.

See you on Day 19.

Comments