How to design a relay circuit for Arduino and ESP32

This tutorial will discuss how to design a relay circuit diagram for any DC or AC loads. By using this relay circuit with Arduino, esp32, or any microcontroller you can drive (ON-OFF) any high-power load like a motor, or a heater.

Component List

  • 1x Optocoupler (we are using PC817X)
  • 1x NPN Transistor (TIP122)
  • 1x 5V Relay (SRD-05VDC-SL-C)
  • 1x Schottky diode (1N5819)
  • 1x Any microcontroller (Arduino, Esp32, Stm32)
  • 2x Resistor (330R and 1K)
  • 1x 5V Power source

For troubleshooting, some extremely useful test equipment

Equipment Name

Purchase Link

Best Oscilloscope for Professionals

Best Oscilloscope for Beginners and Students

Logic Analyzer

Best Budget Multimeter

Adjustable Bench Power Supply

Affiliate Disclosure: When you click on links to make a purchase, this can result in this website earning a commission. Affiliate programs and affiliations include, but are not limited to

Relay Circuit Diagram

Arduino relay circuit

Circuit explanation

In this relay circuit, we use an optocoupler (PC817x)  to separate the high-voltage part of the circuit (relay part) from the low-voltage part. We connect the anode of the optocoupler to the microcontroller (Arduino, esp32, or any microcontroller) signal pin through a 330-ohm resistor, and the cathode connects to the ground. The collector of the optocoupler is connected to a power source (we use a 5-volt power source). The Emmeter of the optocoupler connects to a transistor’s base (we are using TIP122)  via a 1 Kilo-ohm resistor.

The transistor collector pin connects to one pin of the relay coil and the ammeter pin of the transistor is connected to the ground. The other side of the relay coil connects to the power source (5V). We also use a Schottky diode across the relay for reverse current protection.

Mahamudul Hasan

I hold a B.Sc degree in Electrical & Electronic Engineering from Daffodil International University, Bangladesh. I am a Printed Circuit Board (PCB) designer and Microcontroller programmer with an avid interest in Embedded System Design and IoT. As an Engineer, I love taking challenges and love being part of the solution. My Linkedin Profile Link:

Recent Posts