Skip to main content
Tutorials

How to Properly Interface a 5V Relay Module with Custom Microcontroller Projects

Learn how to safely wire, configure, and code a 5V single-channel relay module with custom microcontrollers to automate real-world electrical appliances.

Microcontroller development board wired cleanly to a 5V single-channel relay module and testing apparatus on an engineering workbench

Introduction to Relay Modules in Microcontroller Projects

Microcontrollers operate on low voltage direct current, usually 3.3V or 5V. However, most everyday electrical devices in Pakistan, such as water pumps, ceiling fans, room heaters, and tube well motors, run on 220V alternating current. A microcontroller cannot power these high-voltage appliances directly because the excessive current would instantly destroy the delicate digital pins.

This is where an electromechanical relay module becomes essential. A relay acts as an electrically operated switch. By sending a small digital signal from your microcontroller, you can open or close a separate high-power electrical circuit without exposing your microcontroller board to dangerous currents. In this practical tutorial from the engineering team at NetBit, we walk you through the complete process of wiring, configuring, and testing a 5V relay module safely.

Understanding the 5V Relay Module Pinout and Terminals

Before connecting any wires, it helps to understand the two distinct sides of a standard relay module: the low-voltage control side and the high-voltage switching side.

1. The Low-Voltage Control Header

The control side typically has three pins designed to connect directly with your development board:

  • VCC: Connects to the 5V power output of your power supply or microcontroller board.
  • GND: Connects to the system ground pin to establish a common electrical reference.
  • IN (Input Signal): Receives the digital logic trigger from one of your microcontroller general-purpose input/output (GPIO) pins.

2. The High-Voltage Screw Terminals

The output side features three screw terminals that handle the load circuit:

  • COM (Common): The main power terminal where your incoming live wire is connected.
  • NO (Normally Open): The circuit stays open (turned off) until the microcontroller sends a trigger signal to energize the internal coil.
  • NC (Normally Closed): The circuit remains closed (turned on) by default, and breaks open only when the relay is energized.

For most automation projects built at netbit tech, the Normally Open (NO) terminal is preferred because it keeps appliances safely switched off until an active command is executed.

Hardware Requirements for this Tutorial

To follow along with this tutorial, gather the following components:

  1. One 5V single-channel relay module with built-in optocoupler isolation.
  2. Any standard microcontroller development board (such as an ATmega328P based board or an ESP32).
  3. Jumper wires (female-to-male and male-to-male).
  4. An external DC load for safe testing, such as a 12V DC cooling fan or a small 12V LED lamp with its DC adapter.
  5. A wire stripper and a small flathead screwdriver for the screw terminals.

Safety Note: If you are a beginner or student, always test your project using low-voltage DC loads first before working with dangerous 220V AC household wiring.

Step-by-Step Wiring Instructions

Carefully follow these wiring steps to connect your board without triggering short circuits or brownout resets.

Step 1: Connect the Ground Wire

Connect the GND pin of the relay module directly to the GND pin of your development board. A secure ground connection ensures clean digital switching and prevents erratic triggering.

Step 2: Connect the Power Line

Connect the VCC pin of the relay module to the 5V pin on your microcontroller. While a single small relay coil can run off USB power during basic prototyping, multiple relays require an independent external 5V power supply to prevent the microcontroller from rebooting whenever the relay coil draws sudden current.

Step 3: Connect the Signal Pin

Connect the IN pin of the relay module to digital pin D7 (or any free digital GPIO pin) on your microcontroller board.

Step 4: Wire the Load Circuit

Cut the positive wire of your 12V test lamp power supply. Strip both exposed ends. Insert the side coming from the power supply into the COM terminal and tighten the screw firmly. Next, insert the wire going toward the lamp into the NO terminal and tighten it. Leave the negative wire connected directly from the power supply to the lamp.

Writing the Control Firmware

Most commercial relay modules use an "Active LOW" trigger mechanism, meaning the relay clicks on when the signal pin is pulled to LOW, and turns off when the pin is pulled to HIGH. You can write a straightforward firmware sketch to test switching cycles:

const int RELAY_PIN = 7;

void setup() {
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, HIGH); // Keep relay OFF initially on active-low boards
}

void loop() {
  digitalWrite(RELAY_PIN, LOW); // Turn load ON
  delay(5000);                 // Keep running for 5 seconds
  digitalWrite(RELAY_PIN, HIGH);// Turn load OFF
  delay(5000);                 // Wait for 5 seconds
}

Compile and upload this firmware using your development environment. Once uploaded, you should hear a crisp mechanical clicking sound every five seconds, accompanied by an onboard indicator LED lighting up on the module.

Testing and Troubleshooting Common Issues

If your relay does not behave as expected, check these common hardware pitfalls identified during custom hardware projects at netbit software:

  • Microcontroller restarts when relay triggers: This happens due to electrical noise and voltage drop when the relay coil energizes. Add a 100uF capacitor across VCC and GND, or power the relay from a dedicated 5V power adapter while keeping grounds connected.
  • Relay logic is inverted: If your appliance turns on when it should turn off, your module is active-low. Adjust your code so LOW represents ON and HIGH represents OFF, or move your wire from the NO terminal to the NC terminal.
  • Module LED turns on but no click sound: This indicates insufficient voltage. A 5V relay coil will struggle to engage if your board only delivers 3.3V to the VCC terminal. Always supply a solid 5V rail to the coil.

Practical Applications in Pakistan

Mastering basic relay interfacing opens up numerous real-world engineering possibilities. In Balochistan and across Pakistan, local businesses and farms frequently utilize microcontroller-driven relay controllers for:

  • Automated overhead water tank fill systems linked with float switches.
  • Smart solar inverter switching systems to manage load during peak hours.
  • Scheduled agricultural drip irrigation and tube well pump controllers.
  • Temperature-triggered ventilation fans for commercial poultry and storage setups.

Ready to Turn Your Technical Ideas into Working Systems? Partner with NetBit Tech Today

Building reliable hardware controllers and integrating them with custom mobile apps, web dashboards, or embedded logic takes experienced hands. At NetBit tech software house, located in the heart of Balochistan at netbit khuzdar, we turn complex technical requirements into dependable, tailored engineering solutions.

Whether your business needs custom microcontroller design, industrial automation boards, smart monitoring interfaces, or custom business software, our dedicated team at netbit.pk is prepared to build systems that work under real-world conditions. You do not have to struggle alone with unstable prototypes or electrical wiring errors. Visit us online at netbit.pk or reach out directly to the engineers at netbit tech today. Let us discuss your next technical milestone, design the ideal blueprint, and turn your project vision into an efficient, professional solution that delivers results for your enterprise.

Discussion

Reader comments.

Share your question, suggestion or experience related to this article.

0 Approved Comments

No comments yet

There are no approved comments on this article yet. You can be the first person to leave one.

Keep Reading

Related insights.

View all articles
WhatsApp