0Pricing
Arduino & IoT Academy · Lesson

Switch Mains Devices with a Relay

Toggle a lamp or fan from a digital pin.

Switch Mains Devices with a Relay is a free Arduino & IoT Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Arduino & IoT Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Controlling Big Things

Sometimes you want your Arduino to switch a lamp or a fan, not just an LED. For mains-powered gear you reach for a relay. 💡

A Relay Is a Switch

A relay is an electrically controlled switch. A small coil current pulls a metal contact closed, connecting a totally separate, much higher-power circuit.

Electrical Isolation

The coil and the contacts never touch electrically. This isolation is why a 5V Arduino can safely command a 220V device without sharing wires.

Use a Relay Module

Grab a ready-made relay module. It already has the driving transistor, a flyback diode, and screw terminals, so you just supply a control signal.

The Three Control Wires

A typical module has three pins: VCC, GND, and a signal input. Connect signal to any Arduino digital pin to control the relay.

Driving It in Code

Treat the relay like an LED. Set the pin HIGH or LOW to close or open the contact, depending on how your module is wired.

int relay = 7;
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH); // switch on

Active-Low Surprise

Many modules are active-low: LOW turns the load on and HIGH turns it off. Test yours so the logic in your sketch matches reality.

NO and NC Terminals

The output side offers NO and NC contacts. Normally Open stays off until triggered; Normally Closed is on until the relay switches.

Wire the Load Through COM

Run one mains wire to the COM terminal and the other to NO. When the relay clicks, the circuit closes and your device powers up.

Respect Mains Safety

Mains voltage can kill. Never touch live terminals, keep the high-voltage side enclosed, and have an adult or pro check anything wired to the wall. ⚠️

Hear the Click

When the relay switches you hear a satisfying click as the coil snaps the contact. That sound confirms your code reached the real world.

Quick Check

What is the main reason a relay is used to switch mains-powered devices from an Arduino?

Recap

A relay is an isolated switch: a small signal closes contacts that power big loads. Use a module, mind active-low logic, and respect mains safety. 🚀

Frequently asked questions

Is the “Switch Mains Devices with a Relay” lesson free?

Yes — the full text of “Switch Mains Devices with a Relay” is free to read here on the web, and the Arduino & IoT Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Arduino & IoT Academy course, upgrade to CoddyKit PRO.

What will I learn in “Switch Mains Devices with a Relay”?

Toggle a lamp or fan from a digital pin. You practise Arduino & IoT Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Arduino & IoT Academy?

No prior experience is required. Arduino & IoT Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Switch Mains Devices with a Relay” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Arduino & IoT Academy lesson?

Yes. Every Arduino & IoT Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why You Need a Driver
  2. Speed & Direction with an H-Bridge
  3. Switch Mains Devices with a Relay
  4. Protect Against Flyback Spikes
← Back to Arduino & IoT Academy