⚡ ESP32 Switching Circuits

Interfacing High-Power Devices with Low-Voltage GPIO Outputs

1. Introduction

In the previous lesson, we explored how the ESP32 can be used to read digital inputs (such as switches and sensors). Now we shift our focus to the output side — specifically, how to use ESP32 GPIO pins to control (switch) various external devices such as motors, relays, buzzers, solenoids, and lights.

This is where switching circuits come into play. A switching circuit acts as an intermediary between the low-power ESP32 pin and the higher-power device you want to control.

2. Why Do We Need a Switching Circuit?

Every GPIO pin on the ESP32 can only output one of two voltage levels:

Digital HIGH ≈ 3.3 V   |   Digital LOW ≈ 0 V

This 3.3 V signal is very weak in terms of power. It is only strong enough to directly drive very small loads such as a single LED or a small piezo buzzer — and even then, only through a current-limiting resistor. Devices such as motors, relays, solenoids, or high-power lamps require much higher voltages and currents than the ESP32 pin can safely supply.

Important: Never connect a motor, relay coil, or any high-current device directly to a GPIO pin. Doing so can permanently damage the ESP32.

To safely control such devices, we use a small "helper" circuit — a switching circuit — that uses the weak 3.3 V signal to control a much larger external voltage and current.

3. Two Categories of Switching Circuits

Based on the type of load being controlled, switching circuits are broadly divided into two categories:

Type Description Typical Loads
DC Load Switching Used to switch loads that operate on Direct Current (DC) DC motors, relays, solenoids, LED strips, buzzers
AC Load Switching Used to switch loads that operate on Alternating Current (AC) mains supply Fans, bulbs, home appliances (via relay/TRIAC)

In this lesson, we focus on DC load switching. AC load switching (using relays or TRIACs) will be covered separately, as it involves additional safety considerations.

4. Understanding DC Load Switching (Transistor Switch)

The simplest and most common way to switch a DC load using an ESP32 GPIO pin is through a bipolar junction transistor (BJT) configured as an electronic switch.

4.1 Circuit Diagram

+Vs (Supply Voltage) LOAD Collector (C) Q1 NPN Transistor Rb GPIO Pin Emitter (E) GND ESP32 GND and transistor GND must be common
Fig. 1 — Basic NPN Transistor Switching Circuit for a DC Load

4.2 Circuit Components

ComponentRole
Vs (Supply Voltage)Voltage matching the load's requirement (e.g., 5 V, 6 V, 12 V) — independent of the ESP32's 3.3 V
LoadThe device being switched (motor, relay coil, buzzer, etc.) — connected between Vs and the transistor's Collector
Transistor (Q1)Acts as the electronic switch between Collector and Emitter
Base Resistor (Rb)Limits the current flowing into the transistor's Base from the GPIO pin
GPIO PinProvides the 3.3 V control signal that turns the transistor ON or OFF

4.3 How It Works

The circuit operates in two states, controlled entirely by the GPIO pin:

GPIO Pin = LOW (0 V)

  • No current flows into the Base
  • Transistor remains OFF (open circuit between C and E)
  • No current flows through the Load
  • Load remains OFF

GPIO Pin = HIGH (3.3 V)

  • Small current flows from GPIO → Rb → Base → Emitter
  • This turns the transistor fully ON ("saturation")
  • A path is created from Vs → Load → Collector → Emitter → GND
  • Load current flows and the device turns ON

In short: the tiny 3.3 V Base current acts as a "trigger" that allows a much larger current (from the separate Vs supply) to flow through the Load. The transistor essentially acts as a voltage/current-controlled switch.

5. Design Considerations

5.1 Base Current Limit

The GPIO pin can only supply a very small current safely. As a design guideline, the Base current (IB) should be kept low — typically in the order of a few milliamps (commonly capped around 10 mA) — to avoid overloading the ESP32 pin.

The Base resistor value can be calculated using:

Rb = (V_GPIO − V_BE) / I_B
        

Where:

V_GPIO = 3.3 V  (GPIO HIGH output)
V_BE   ≈ 0.7 V  (typical Base-Emitter voltage drop for a silicon BJT)
I_B    = desired Base current (e.g., 5 mA = 0.005 A)
        

5.2 Choosing the Right Transistor

The transistor must be able to handle the Collector current (IC) demanded by the load. This is the main factor when selecting a transistor — the choice does not depend on the ESP32 side of the circuit, only on the load's current requirement.

Load Current Range Recommended Transistor Type Example Loads
Low current (< 100–200 mA) General-purpose small-signal NPN transistor (e.g., BC547, 2N2222) Small buzzer, LED array, small relay coil
Higher current (> 500 mA / up to a few Amps) Power NPN transistor or Darlington pair (e.g., TIP122, TIP3055) DC motor, solenoid, high-current relay
Rule of thumb: Only the transistor changes when the load current requirement increases — everything else in the circuit (Base resistor logic, GPIO connection) remains conceptually the same.

5.3 Common Reference Point

The Emitter of the transistor and the ESP32's GND pin must always be connected together (a common ground), even if the Load's supply voltage (Vs) is different from the ESP32's 3.3 V/5 V supply.

6. Example: Switching a 12 V DC Relay/Motor

Suppose we want to switch a 12 V, 200 mA DC relay coil using GPIO pin 26 on the ESP32.

6.1 Circuit Values

Vs = 12 V
Load current (Ic) = 200 mA
Transistor = BC547 (rated up to ~500 mA, sufficient for this load)
V_BE = 0.7 V
Desired I_B = 5 mA
Rb = (3.3 − 0.7) / 0.005 = 520 Ω  (use nearest standard value, e.g., 470 Ω or 560 Ω)
        

6.2 Sample ESP32 Code

Below is a simple Arduino-style sketch to turn the load ON and OFF through GPIO 26:

#define RELAY_PIN 26

void setup() {
  pinMode(RELAY_PIN, OUTPUT);
}

void loop() {
  digitalWrite(RELAY_PIN, HIGH);   // Turn load ON
  delay(2000);                     // Wait 2 seconds
  digitalWrite(RELAY_PIN, LOW);    // Turn load OFF
  delay(2000);                     // Wait 2 seconds
}
        

7. Summary

Coming up next: In the next lesson, we will explore AC load switching — how to safely switch AC mains-powered devices using relays.