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

Name of ComponentQuantityPurchase Link
Optocoupler (we are using PC817X)1Amazon | AliExpress
5V Relay (SRD-05VDC-SL-C)1Amazon | AliExpress
NPN Transistor (TIP122)1Amazon | AliExpress
Schottky diode (1N5819)1Amazon | AliExpress
Any microcontroller (Arduino, Esp32, STM32)1Amazon | AliExpress
Resistor (330R and 1K)2Amazon | AliExpress
5V Power source1Amazon | AliExpress

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 Amazon.com

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: https://linkedin.com/in/mheee

Recent Posts