below is a complete, step-by-step circuit connection guide you can follow to wire your ESP32 8-channel AC dimmer + relays + RS-485 + zero-cross. I cover every connection (mains, protection, optos, TRIACs, ESP32 pins, RS-485, fuses, grounds), component values, and practical tips for wiring and PCB layout. Treat this as the wiring checklist and reference during assembly.

1) Safety first (must read)

Work with mains only if qualified. Use an isolation transformer for testing. Switch mains OFF while wiring.

Use proper fuses, earth/grounding, insulated terminal blocks, X2 capacitors, and mains-rated resistors where noted.

Keep mains and logic wiring physically separated on the board. Use optocouplers for galvanic isolation.

Use heatsinks for TRIACs and an enclosure with adequate creepage/clearance.

2) Overview (signal & power flow)

Mains Live → Main fuse (F_MAIN) → Relay COM (all channels share COM).

Relay NO → Channel fuse Fi → TRIAC MT2. TRIAC MT1 → Load → Neutral.

ESP32 GPIO → 220 Ω → MOC3021 LED anode (pin1); MOC cathode (pin2) → GND. MOC output pins (4 & 6) connect to TRIAC MT1/gate via 100 Ω as described.

H11AA1 senses mains zero-cross: AC side through two 100 kΩ resistors; logic side collector → 10 kΩ pull-up → 3.3 V and GPIO34; emitter → GND.

RS-485 transceiver (3.3 V preferred) DI ← GPIO17, RO → GPIO16 (level-safe), DE/RE ← GPIO5.

3) Pin mapping (ESP32) — use exact pins in firmware

Relays CH1..CH8 control (logic to relay module IN pins):
CH1 = GPIO12
CH2 = GPIO13
CH3 = GPIO14
CH4 = GPIO15
CH5 = GPIO16
CH6 = GPIO17
CH7 = GPIO18
CH8 = GPIO19

TRIAC / MOC LED drive pins (ESP32 → 220Ω → MOC pin1):
TRIAC1 = GPIO22
TRIAC2 = GPIO23
TRIAC3 = GPIO25
TRIAC4 = GPIO26
TRIAC5 = GPIO27
TRIAC6 = GPIO32
TRIAC7 = GPIO33
TRIAC8 = GPIO4

Zero-cross input: GPIO34 (H11AA1 collector via 10 kΩ pull-up to 3.3 V).

RS-485: TX = GPIO17 → DI; RX = GPIO16 ← RO; DE/RE = GPIO5 (set HIGH to transmit, LOW to receive).

Power: ESP32 3.3V & GND, RS-485 transceiver VCC = 3.3V (preferred), Relay VCC = 5V (or 12V depending on module).

4) Per-channel detailed wiring (repeat for channels 1..8)

Use this as the schematic for one channel; replicate 8 times.

Components per channel:

MOC3021 optotriac

BTA16 (or BT136 for lower current) TRIAC

220 Ω (MOC LED resistor)

100 Ω (TRIAC gate resistor)

RC snubber: 100 Ω (0.5 W) in series with 10 nF X2 (275 VAC) across MT1–MT2

Channel fuse Fi and holder (time-delay suggested)

Wiring (one-channel):

BTA16 MT1 → Load (lamp) → AC Neutral (N).

Relay NO → Channel fuse Fi → BTA16 MT2.

AC Live (L) → F_MAIN (main fuse) → Relay COM.

Mains / Relay / Fuse / TRIAC / Load

MOC3021 pin 6100 Ω → BTA16 Gate.

MOC3021 pin 4 → BTA16 MT1 (connect to the same node that goes to the lamp).

MOC3021 pin 2 (LED Cathode) → ESP32 GND.

ESP32 TRIAC GPIO (e.g., GPIO22) → 220 Ω → MOC3021 pin 1 (LED Anode).

MOC3021 ↔ TRIAC gate

Across BTA16 MT1 — MT2: series circuit 100 Ω (≥0.5W) + 10 nF X2-rated capacitor. Connect the RC directly across MT1 & MT2.

Snubber across TRIAC

Use separate copper areas for MT1/MT2 currents and keep snubber close to TRIAC.

Keep the MOC-to-TRIAC gate wiring very short and route away from noisy traces.

Physical notes

ASCII mini-schematic for one channel:
     AC L --- F_MAIN --- Relay COM 
                          | 
                     Relay NO 
                          | 
                        Fi (fuse) 
                          | 
                       BTA16 MT2 
                       |       
                       |       ---- snubber 100R + 10nF X2 
                       | 
                     BTA16 MT1 ----+---- Load ---- AC N 
                                    | 
                                 MOC3021 pin4 
MOC LED side: 
ESP32 GPIO22 -- 220R --|> (MOC3021 pin1) 
MOC3021 pin2 ---- GND 

MOC3021 pin6 -- 100R --|> BTA16 Gate
5) Zero-cross detector (full wiring)

Use one H11AA1 (or H11AA1M / 305Q) for all 8 channels.

Components:

H11AA1 (pins 1–6)

Resistors R1, R2 = 100 kΩ 0.5 W (AC side)

Pull-up R3 = 10 kΩ to 3.3 V

Optional C1 = 100 nF (0.1 µF) from GPIO to GND

Connections:

AC side:

AC Live → R1 (100 kΩ) → H11AA1 pin1

AC Neutral → R2 (100 kΩ) → H11AA1 pin2

Opto outputs (logic side):

H11AA1 pin5 (collector) → R3 (10 kΩ) → 3.3 V

H11AA1 pin5 → ESP32 GPIO34 (input)

H11AA1 pin4 (emitter) → ESP32 GND

Optional: C1 (100 nF) between GPIO34 and GND (to filter spurious pulses)

Pin reference (6-pin DIP):
    ┌───┐ 
1 ──┤   ├── 4  (Anode - LED+) 
2 ──┤   ├── 5  (Cathode - LED-) 
3 ──┤   ├── 6  (N/C - Not Connected) 
   └───┘
(Note: pins 1 & 2 are the AC LED pair, pins 4 emitter and 5 collector are the transistor on the logic side; confirm with datasheet but this matches standard H11AA1 pinout.)

Software: configure GPIO34 with internal/external pull-up, attach interrupt on falling edge (or any edge), and reset half-cycle timer inside ISR.

6) RS-485 wiring & notes

Preferred: use a 3.3 V RS-485 transceiver (MAX3485, SN65HVD3082, etc.). If you must use MAX485 at 5 V, level-shift RO to 3.3V.

Connections (3.3 V transceiver):

VCC → 3.3 V

GND → common logic GND

DI → ESP32 GPIO17 (TX)

RO → ESP32 GPIO16 (RX)

DE → ESP32 GPIO5

RE → ESP32 GPIO5 (tie DE & RE together)

A/B → RS-485 bus twisted pair

Put 120 Ω termination across A–B at both physical ends of bus.

Add biasing at one end (master): A → VCC through 680 Ω, B → GND through 680 Ω.

Important: when transmitting, set GPIO5 HIGH, write bytes, wait for transmission to complete (delay or check UART status), then set GPIO5 LOW.

7) Power supplies & grounding

Use an isolated 5 V SMPS (e.g., HLK-PM01) for relay module and other 5 V needs.

Use a 3.3 V regulator (or onboard ESP32 regulator) for ESP32 and 3.3 V transceiver.

Connect logic grounds together: ESP32 GND, RS-485 GND, pull-ups, MOC cathodes. Do not connect logic ground to mains Live/Neutral. Earth (protective) should connect to chassis/earth points as required.

Keep AC and DC wiring physically separated.

8) Fuses (main + per channel)

Main input fuse (F_MAIN) on AC Live before relays (size per total load). Example: for up to ~1.5 kW choose 6.3 A slow-blow.

Per-channel fuse Fi between Relay NO and TRIAC MT2: select time-delay (T) type based on load current per channel (typical incandescent channel: T250 mA — T1 A depending on lamp). Place fuse as near to relay as practical.

9) Component values & recommended parts

H11AA1 / H11AA1M (or part with marking 305Q) — zero-cross optocoupler.

R1, R2 (AC side) = 100 kΩ, 0.5 W (for 230 VAC). For 110 VAC, use 47 kΩ 0.5 W.

R3 (pull-up) = 10 kΩ, 1/4 W.

C1 (optional filter) = 100 nF, 50 V or 100 V (on logic side).

MOC3021 — optotriac (non-zero-cross).

MOC3021 LED resistor = 220 Ω, 1/4 W.

TRIAC = BTA16-600 (capable for your mains voltage & current). Alternative BT136 for lower currents.

TRIAC gate resistor = 100 Ω, 1/4 W.

Snubber: 100 Ω, 0.5 W + 10 nF X2 (275 VAC) in series.

Fuses: main + channel as discussed (time-delay recommended).

RS-485 transceiver: 3.3 V part recommended.

10) Firmware summary (what the hardware expects)

Zero-cross ISR on GPIO34: reset elapsed_us = 0, fired_mask = 0. (Keep ISR minimal.)

Periodic timer (e.g., every 25–50 µs): increment elapsed_us. For each channel with state==ON and not fired this half-cycle, if elapsed_us >= compute_delay(brightness) then pulse corresponding GPIO (gate) for ~120 µs. Mark channel fired.

compute_delay_us(brightness) = (100 - brightness) * HALF_CYCLE_US / 100. For 50 Hz HALF_CYCLE_US = 10000 µs.

Send/receive RS-485 frames per your 4-byte protocol. Toggle DE/RE pin when transmitting.

11) PCB / wiring layout tips

Keep mains (Live/Neutral) traces and high-current traces wide and separated from logic traces.

Place TRIACs at board edge with heatsinks outside.

Place snubber components close to TRIAC MT pins.

Place H11AA1 on AC side with sufficient isolation gap and keep its AC-side resistors physically near it. Use isolation slots if possible.

Keep MOC3021 outputs and TRIAC gates routing short and direct.

Use screw terminal blocks for mains, loads, and RS-485 bus.

12) Full wiring checklist (step-by-step to assemble & test)

Build and test the logic side first (ESP32, RS-485, relay module) with no mains connected. Verify relays switch from firmware.

Connect all pull-ups and grounds: 3.3 V → pull-ups, common logic GND.

Assemble one TRIAC channel (MOC3021 + BTA16 + snubber) and wire the MOC LED resistor and gate resistor. Keep TRIAC unpowered (no mains) and verify LED current when you toggle the GPIO.

Wire H11AA1 AC side (but keep mains off). Connect collector → 10 kΩ pull-up → GPIO34 and emitter → GND. Use a function generator or manually toggle GPIO34 to simulate zero-cross and test timer code.

With isolation transformer and a test lamp, insert F_MAIN and F1 for channel 1. Power mains and observe ZERO-CROSS pulses at GPIO34 (use scope or toggle an LED in ISR).

Turn on relay for channel 1, run dim commands from RS-485 and observe lamp brightness. Tune GATE_PULSE_US and timer tick if needed.

Once channel 1 is stable, add other channels one by one.

13) BOM summary (concise)

ESP32 dev board

H11AA1 / H11AA1M (or 305Q)

R1,R2 = 100 kΩ 0.5 W ×2

R3 = 10 kΩ ×1

C1 = 100 nF (logic side filter)

8 × MOC3021

8 × 220 Ω resistors (MOC LED)

8 × BTA16 (or BT136) TRIACs + heatsinks

8 × 100 Ω gate resistors

8 × snubber (100 Ω 0.5W + 10 nF X2)

Relay module (8-ch) or individual relays + drivers

RS-485 transceiver (3.3 V) + termination resistors

F_MAIN + 8 × Fi (time-delay) + fuse holders

SMPS 5 V + 3.3 V regulator

Terminal blocks, connectors, wiring, enclosure