ESP32 input wiring¶
This page describes how to connect buttons and other contacts to a wESP32 running TriggerGate: how the firmware reads its inputs, which GPIOs can be used, the default assignment, and a recommended isolated input circuit. For flashing and setting up the board, see Set up an ESP32 board.
How the firmware reads an input¶
- Each input is active low: an input is on while its GPIO is pulled low, and off while it is high. A press that pulls the pin low is reported as a rising edge (the input turning on), and the release as a falling edge.
- The firmware turns the ESP32's internal pull-up and pull-down resistors off on every input. Each input therefore needs an external pull-up, as in the circuit below; without one the pin floats and reads noise.
- Every input raises an interrupt on both edges, and the firmware timestamps the edge as the interrupt arrives.
- A press less than 200 ms after that input's last detected change in either direction is ignored before it can enter a faceoff or restart a lockout. Releases normally report unless the input has already entered and is closed; settled levels can be corrected without a new edge. See Input diagnostics.
Which GPIOs to use¶
| Class | GPIOs | Notes |
|---|---|---|
| Inputs or outputs | 2, 4, 5, 12, 13, 14, 15, 18, 23, 32, 33 | Can be an input or the status output. |
| Inputs only | 34, 35, 36, 39 | No output driver and no internal pull resistors. Can only be an input. |
| Strapping, take care | 2, 12 | Read by the ESP32 at power-on. The board's maker requires that they are not pulled high when the board powers up: leave them floating or hold them low. Do not put an input with a pull-up to 3.3 V on GPIO 2 or GPIO 12. |
| Reserved by the board | 0, 16, 17, 19, 21, 22, 25, 26, 27 | Ethernet (GPIO 0 is the Ethernet clock input). Not offered. |
| Reserved by the ESP32 | 1, 3, 6–11 | Serial console (1 and 3) and flash memory (6–11). Not offered. |
The settings dialog of the web page offers only the GPIOs in the first two rows, and refuses a GPIO assigned twice.
Default assignment¶
A board with no saved settings uses:
| Logical input | GPIO |
|---|---|
| 0 | 14 |
| 1 | 5 |
| 2 | 33 |
| 3 | 35 |
| 4 | 34 |
| 5 | 36 |
| 6 | 39 |
| 7 | 32 |
| Status output | 2 |
Change it in the web page's settings dialog: each of the eight logical inputs can be given a GPIO or left unassigned, and the status output can be moved or turned off. The board saves the assignment in its flash. See the wESP32 product brief for where each GPIO is on the board's headers.
The status output is driven high for one second after any input changes, and for half a second when a client sends Identify. It is driven low otherwise. GPIO 2 is a strapping pin, so whatever is connected to it must not pull it high at power-on.
Recommended input circuit¶
An optocoupler per input isolates the board from the field wiring, and a pull-up on the ESP32 side gives the clean logic level the firmware needs.
flowchart LR
subgraph Field["Field side (5 to 24 V)"]
INP["INPUT +"]
R1["R1"]
LED["Optocoupler LED"]
INM["INPUT −"]
TVS["TVS diode<br/>bidirectional"]
end
subgraph Board["ESP32 side (3.3 V)"]
V33["+3.3 V"]
R2["R2 10 kΩ"]
GPIO["GPIO"]
PT["Optocoupler<br/>phototransistor"]
GND["GND"]
end
INP --> R1 --> LED --> INM
INP --- TVS --- INM
V33 --> R2 --> GPIO
GPIO --> PT --> GND
LED -. light .-> PT
| Ref | Part | Value | Purpose |
|---|---|---|---|
| R1 | Resistor | See Choosing R1 | Limits the current through the optocoupler's LED. |
| R2 | Resistor | 10 kΩ | Pull-up from the GPIO to 3.3 V. |
| U1 | Optocoupler | PC817, TLP291 or equivalent | Isolates the field wiring from the board. |
| D1 | TVS diode | Bidirectional, see Protection | Clamps transients on the field side. |
How it works:
- Off. No voltage across the field terminals. The optocoupler's LED and phototransistor are off, and R2 holds the GPIO high. The input reads off.
- On. Voltage across INPUT+ and INPUT− drives current through R1 and the LED. The phototransistor turns on and pulls the GPIO low. The input reads on, and TriggerGate reports a rising edge.
- Released. The field voltage goes away, the phototransistor turns off, and R2 pulls the GPIO high again. TriggerGate reports a falling edge.
Choosing R1¶
The optocoupler's LED needs about 5 to 20 mA to switch reliably. Choose R1 from the field voltage:
R1 = (Vfield − Vf) / ILED, where Vf is about 1.2 V for most optocouplers.
| Field voltage | R1 | LED current (approx.) |
|---|---|---|
| 5 V | 220 Ω | 17 mA |
| 12 V | 2.2 kΩ | 5 mA |
| 24 V | 4.7 kΩ | 5 mA |
Protection¶
A transient voltage suppressor (TVS) diode across the field terminals clamps spikes before they reach the optocoupler, such as electrostatic discharge and the kickback of relays or solenoids on the same wiring run.
- Place a bidirectional TVS diode directly across INPUT+ and INPUT−.
- Choose its standoff (working) voltage slightly above the field voltage, so that it never conducts in normal use: for example a 5 V standoff part such as SMAJ5.0CA for a 3.3 to 5 V field, and a correspondingly higher standoff for 12 V or 24 V fields.
- In a noisy environment, add a small ferrite bead or a 100 Ω resistor in series before the TVS diode.
Schmitt trigger (optional)¶
An optocoupler usually produces clean edges by itself. If an input has slow edges or picks up noise that survives the optocoupler, or for an input wired directly without isolation, put a Schmitt-trigger buffer such as the SN74LVC1G17 between the optocoupler's output (with R2 still pulling it up) and the GPIO. It runs from the 3.3 V supply, adds about 500 mV of hysteresis, and keeps the same polarity, so the input still reads on when the contact closes.
Example parts list for eight inputs¶
| Qty | Ref | Part |
|---|---|---|
| 8 | R1 | Resistor, ¼ W, sized for the field voltage (2.2 kΩ for 12 V) |
| 8 | R2 | 10 kΩ resistor, ¼ W |
| 8 | U1 | PC817 optocoupler (DIP-4 or SMD-4) |
| 8 | D1 | Bidirectional TVS diode, sized for the field voltage |
| 1 | — | 8-position terminal block, 5.08 mm pitch |
| 1 | — | Prototype board or custom PCB |
Wiring checklist¶
- Each button or contact is normally open and wired between INPUT+ and INPUT−.
- R1 is sized for the field voltage.
- Each input GPIO has a 10 kΩ pull-up (R2) to 3.3 V.
- No input with a pull-up is on GPIO 2 or GPIO 12, and nothing pulls GPIO 2 or GPIO 12 high at power-on.
- No input or output uses a GPIO reserved for Ethernet, the serial console or flash.
- The input assignment in the web page's settings dialog matches the wiring.
- TVS diodes are fitted where the wiring runs near relays, solenoids or long cables.