prerequisite

Voltage, current, and Ohm's law

What voltage, current, and resistance are, and the one equation that relates them, enough to wire an LED and a button safely.

Before this

Nothing beyond first-year college math. This is a starting page.

Why you need this

The first thing most people wire to an ESP32 is an LED, and the second is a button. Both are safe only if you can work out how much current will flow, and that takes one equation. This page gives you that equation, the power rule that goes with it, and the one voltage rule that keeps an ESP32 alive: its pins are 3.3 V pins.

The idea

Charge, current, voltage, resistance

Electric charge is carried by electrons moving through a wire. Current is how much charge passes a point each second. Its symbol is II and its unit is the ampere (A); small circuits use milliamperes, where 1 mA is a thousandth of an ampere.

Voltage is the push that drives current: the difference in electrical "pressure" between two points. Its symbol is VV and its unit is the volt (V). Voltage is always between two points. When a page says "the pin is at 3.3 V", it means 3.3 V above ground (GND), the reference point every voltage on the board is measured from.

Resistance is how hard a part makes it for current to flow. Its symbol is RR and its unit is the ohm (Ω). A resistor is a part made to have one known resistance, such as 10 kΩ (10,000 Ω).

In a water picture, voltage is the height of a tank, current the flow through the pipe, and resistance how narrow the pipe is.

Ohm's law

For a resistor, the three quantities are tied together by Ohm's law:

V=IRV = I R

where VV is the voltage across the resistor in volts, II the current through it in amperes, and RR its resistance in ohms. Rearranged, I=V/RI = V / R and R=V/IR = V / I.

With small numbers: 3.3 V across a 330 Ω resistor drives I=3.3/330=0.01I = 3.3 / 330 = 0.01 A, which is 10 mA.

Power

Current flowing through a part turns energy into heat (or light, for an LED). The rate is the power:

P=VIP = V I

where PP is in watts (W), VV is the voltage across the part, and II the current through it. Resistors are sold with power ratings; a common small one is rated a quarter of a watt. Anything under a few tens of milliwatts is far inside that.

Series

Parts connected end to end, so the same current flows through each, are in series. Their resistances add: 10 kΩ in series with 10 kΩ is 20 kΩ. The voltage of the supply is shared between them; across two equal resistors, each gets half.

LEDs need a resistor

An LED (light-emitting diode) lets current flow one way only, and while it conducts, the voltage across it stays close to a fixed value called its forward voltage, often around 2 V for a red LED (the LED's own datasheet gives the real figure). It does not behave like a resistor. Above its forward voltage, a tiny extra push makes the current shoot up, so an LED wired straight across 3.3 V draws as much current as the pin or supply can give, and something gets hot. A current-limiting resistor in series fixes this: the resistor takes whatever voltage the LED does not, and Ohm's law on the resistor sets the current.

3.3 V logic, and why 5 V is damage

An ESP32's pins work at 3.3 V: a pin set high sits at about 3.3 V, and an input expects 0 V to 3.3 V. Espressif's ESP32 Series Datasheet (version 5.3, Table 5-3) puts the highest allowed input voltage at the supply voltage plus 0.3 V, which is 3.6 V on a 3.3 V board, and Table 5-1 caps the supply pins themselves at 3.6 V. A 5 V signal is well past that. The author's notes for the WROOM-32 DevKit say it plainly: the module is not 5 V tolerant on any pin. Many boards have a pin labeled 5V or VIN; that pin feeds the board's regulator and is never a signal level for a GPIO. How the pin turns a voltage into a 0 or a 1 is on Digital logic and pull-up resistors.

Worked example

Sizing an LED resistor

You want an LED with a forward voltage of 2.0 V to carry 10 mA from a 3.3 V pin.

  1. The LED takes 2.0 V, so the resistor gets the rest: 3.3−2.0=1.33.3 - 2.0 = 1.3 V.
  2. The same 10 mA flows through both, since they are in series.
  3. Ohm's law on the resistor: R=V/I=1.3/0.010=130R = V / I = 1.3 / 0.010 = 130 Ω.
Part Voltage across it Current Power P=VIP = VI
resistor, 130 Ω 1.3 V 10 mA 13 mW
LED 2.0 V 10 mA 20 mW
whole circuit 3.3 V 10 mA 33 mW

13 mW in the resistor is tiny next to a quarter-watt rating. With no 130 Ω resistor, round up, never down: 150 Ω gives 1.3/150≈8.71.3 / 150 \approx 8.7 mA, slightly dimmer and safer. The LED's datasheet tells you its maximum current; stay under it.

For scale, the ESP32 datasheet's Table 5-3 lists about 40 mA as the current one pin can source at its strongest drive setting with the output still above 2.64 V. A 10 mA LED is comfortably inside that.

The current through a pull-up

A pull-up resistor connects an input pin to 3.3 V so the pin reads high when nothing else is connected. Take a 10 kΩ pull-up with a button from the pin to GND. When the button is pressed, the full 3.3 V sits across the resistor:

I=3.310,000=0.00033 A=0.33 mAI = \frac{3.3}{10{,}000} = 0.00033 \text{ A} = 0.33 \text{ mA}

and the power is P=3.3×0.00033≈1.1P = 3.3 \times 0.00033 \approx 1.1 mW. When the button is released, almost no current flows, because the input pin draws almost none. That is why pull-ups are large values: big enough that a pressed button wastes very little, small enough to hold the pin firmly. The next page works out the voltages.

In an ESP32 project

Many boards already carry LEDs with their resistors on the board. The ESP_32_DAD project blinks the red LED on the back of a CYD; this MicroPython excerpt has run on real hardware:

from machine import Pin
import time

led = Pin(4, Pin.OUT, value=1)  # GPIO 4 = red LED, start OFF (active low)

while True:
    led.value(0)        # ON
    time.sleep_ms(500)
    led.value(1)        # OFF
    time.sleep_ms(500)

Active low means the LED lights when the pin is driven to 0 V. The LED circuit's other end sits at a higher voltage, so current flows when the pin is low and stops when the pin is high and there is no voltage difference left to push it. Two more rules from the author's board notes:

  • The 3.3 V pin has a budget. The CYD and WROOM-32 DevKit notes say not to draw more than about 40 mA from the board's 3.3 V pin for external parts, to leave headroom for the WiFi radio's current spikes.
  • Never feed 5 V into two places at once. The CYD has two USB connectors; powering both, or USB plus a 5V pin, puts two supplies against each other.

Common mistakes

  • LED with no resistor. Symptom: very bright for a moment, then dim or dead, and the pin may be damaged.
  • LED backwards. It conducts one way only. Symptom: nothing lights, nothing gets hot. Turn it around.
  • Resistor rounded down. 47 Ω instead of 130 Ω gives about 28 mA. Symptom: the LED runs hot or fades over weeks.
  • 5 V sensor straight into a GPIO. Symptom: works for a while, then the pin reads wrong or the chip stops responding. Use a 3.3 V part or a level shifter.
  • Forgetting the common ground. A voltage only means something relative to GND. Symptom: a second board or sensor reads garbage until the GND pins are wired together.
  • Too much from the 3.3 V pin. Symptom: random resets when the radio turns on, because the supply sags.

Cost

Parts cost little: resistors and LEDs are among the cheapest components there are. The energy cost is what matters on a battery. An LED at 10 mA from 3.3 V uses 33 mW the whole time it is lit; a pull-up held pressed uses about 1.1 mW; a released pull-up uses almost nothing. Mistakes cost more than parts: a 5 V signal on a GPIO can ruin the chip, and on a board like the CYD the chip is soldered to the display.

Going further

  • Kirchhoff's voltage and current laws, which generalize the series rule to any circuit.
  • Voltage dividers: two resistors in series used to scale a voltage down for an analog input.
  • Level shifters, for connecting 5 V sensors to 3.3 V pins.
  • Digital logic and pull-up resistors, the next page.

Leads to

Back to ESP32 development: assembly, C, MicroPython, and CircuitPython