Build a Working Radar: Mapping Rooms with Arduino, Ultrasnonics, and Servos
Combine sensors and servos to build a real-time scanning radar. Learn Polar Coordinates, Serial Plotting, and data visualization.
Welcome to Day 18. Yesterday, we learned how to spin a small motor using a Transistor. That was fun. But transistors have limits.
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.

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 Sequence:

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.
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:

A single relay consumes about 70mA to 90mA when active. The Arduino’s 5V pin can supply about 400mA total (if powered via USB).
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.
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.
Relays have three high-voltage terminals. Understanding them is critical.
The Door Analogy:

We will simulate a High Voltage lamp using a specialized setup. Components:
Wiring Instructions (Control Side):
Wiring Instructions (High Power Side):
Notice: The 9V Battery Negative does NOT connect to Arduino GND here. Isolation is maintained!

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:
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);

Mechanical relays are great, but they have flaws:
The Solution? The Logic Relay. SSR (Solid State Relay). It uses massive internal SCRs or Triacs (Silicon).
For dimming lights? Use an SSR. For turning on a water pump once a day? Use a Mechanical Relay.

You will encounter many relays in your career.
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.
You have:
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);
}
}

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:
This is the grandfather of the Silicon Flip-Flop (Bit Memory).
Look closely at the text printed on your blue relay. It usually says: 10A 250VAC | 10A 30VDC.
10A 250VAC: This is the Absolute Maximum for AC.
10A 30VDC: This is the limit for 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).
If you look at your Relay Module, you might see a yellow jumper connecting VCC and JD-VCC.
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.
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:
| Component | Quantity | Value | Notes |
|---|---|---|---|
| Arduino Uno | 1 | Any | The Brain. |
| Relay Module | 1 | 5V 1-Channel | Ensure it says “5V DC” on the coil label. “SRD-05VDC-SL-C” is standard. |
| Jumper Wires | 10 | M-F | Male-to-Female wires are needed for the module pins. |
| Load | 1 | DC Motor/LED | A safe low-voltage test load. |
| Power Supply | 1 | 9V/12V | To power the Load. |
| Screwdriver | 1 | Small Philips | To tighten the Relay Module terminals. |
| Wire Stripper | 1 | Any | To strip the ends of thick wires. |
| Sound Sensor | 1 | KY-037 | Optional: For the “Clapper” logic. |
| Multimeter | 1 | Any | To verify Voltage before connecting loads. |
You have mastered the Switch.
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.