# KiCad Schematic and PCB Design Guide

## Step-by-Step Schematic Creation

### 1. Component Libraries Setup

First, ensure you have the necessary libraries:
- **ESP32-WROOM-32**: ESP32 module footprint
- **TXB0104**: Logic level shifter
- **AMS1117**: Voltage regulator
- **CP2102N**: USB-to-Serial (optional)
- Standard libraries: Resistors, Capacitors, LEDs, Connectors

### 2. Schematic Components List

Add these components to your schematic:

#### Power Section
- **U1**: AMS1117-3.3 (Voltage Regulator)
- **C1**: 10µF Capacitor (Input)
- **C2**: 10µF Capacitor (Output)
- **C3**: 100nF Capacitor (Input decoupling)
- **C4**: 100nF Capacitor (Output decoupling)
- **USB1**: USB Micro-B Connector (Power input)

#### ESP32 Module
- **U2**: ESP32-WROOM-32E Module

#### Logic Level Shifter
- **U3**: TXB0104PWR (4-channel level shifter)

#### Midea AC Connector
- **J1**: JST-XH 4-pin Connector (or USB Type-A female)

#### USB-to-Serial (Optional)
- **U4**: CP2102N (USB-to-Serial converter)
- **USB2**: USB Micro-B Connector (Programming)

#### Status LEDs
- **LED1**: LED (WiFi Status) - GPIO2
- **LED2**: LED (BLE Status) - GPIO4
- **R1**: 220Ω Resistor (LED1 current limiting)
- **R2**: 220Ω Resistor (LED2 current limiting)

#### Buttons
- **SW1**: Tactile Switch (Reset)
- **SW2**: Tactile Switch (Boot/Flash)
- **R3**: 10kΩ Resistor (Reset pull-up)
- **R4**: 10kΩ Resistor (Boot pull-up)

#### Decoupling Capacitors
- **C5-C10**: 100nF Capacitors (ESP32 power pins)

---

## Detailed Schematic Connections

### Power Supply Section

```
USB1 (Micro-B):
  Pin 1 (VCC) → C1+ → U1.IN
  Pin 1 (VCC) → C3+ → U1.IN
  Pin 4 (GND) → GND

U1 (AMS1117-3.3):
  IN → USB1.VCC (via C1, C3)
  GND → GND
  OUT → C2+ → 3V3 (Power Rail)
  OUT → C4+ → 3V3 (Power Rail)

Power Rails:
  3V3 → ESP32 VDD pins
  3V3 → Level Shifter VCCA
  3V3 → LED anodes (via resistors)
  3V3 → Button pull-ups
  GND → Common ground plane
```

### ESP32-WROOM-32E Connections

```
U2 (ESP32-WROOM-32E):
  VDD (Pins: 1,3,14,21,22,27,28,33,42) → 3V3 (via decoupling caps)
  GND (Pins: 2,4,13,15,20,23,26,29,34,38,40) → GND
  
  GPIO17 (TX) → U3.A1 (Level Shifter LV side)
  GPIO16 (RX) → U3.A2 (Level Shifter LV side)
  
  GPIO2 → R1 → LED1 (WiFi Status)
  GPIO4 → R2 → LED2 (BLE Status)
  
  GPIO0 → SW2 (Boot Button) → GND
  GPIO0 → R4 → 3V3 (Pull-up)
  
  EN → SW1 (Reset Button) → GND
  EN → R3 → 3V3 (Pull-up)
  
  GPIO1 (TX) → U4.RXD (if USB-to-Serial included)
  GPIO3 (RX) → U4.TXD (if USB-to-Serial included)
```

### Logic Level Shifter (TXB0104)

```
U3 (TXB0104PWR):
  VCCA → 3V3 (Low voltage side)
  VCCB → 5V (High voltage side - from USB or external)
  GND → GND
  OE → 3V3 (Always enabled)
  
  A1 (LV1) → U2.GPIO17 (ESP32 TX)
  B1 (HV1) → J1.RX (AC Dongle RX)
  
  A2 (LV2) → U2.GPIO16 (ESP32 RX)
  B2 (HV2) → J1.TX (AC Dongle TX)
  
  A3, A4, B3, B4 → NC (Not connected)
```

### Midea AC Connector

```
J1 (JST-XH 4-pin or USB Type-A):
  Pin 1 (VCC) → 5V (if AC dongle needs power)
  Pin 2 (RX) → U3.B1 (Level Shifter HV1)
  Pin 3 (TX) → U3.B2 (Level Shifter HV2)
  Pin 4 (GND) → GND
```

### USB-to-Serial (Optional)

```
U4 (CP2102N):
  VDD → 3V3
  GND → GND
  DTR → U2.GPIO0 (Auto-reset for flashing)
  RXD → U2.GPIO1 (ESP32 TX)
  TXD → U2.GPIO3 (ESP32 RX)
  VBUS → USB2.VCC (5V detection)

USB2 (Micro-B):
  VCC → U4.VBUS
  D+ → U4.D+
  D- → U4.D-
  GND → GND
```

### Status LEDs

```
LED1 (WiFi Status):
  Anode → R1 → U2.GPIO2
  Cathode → GND

LED2 (BLE Status):
  Anode → R2 → U2.GPIO4
  Cathode → GND
```

### Buttons

```
SW1 (Reset):
  Pin 1 → U2.EN
  Pin 2 → GND
  (R3 pull-up: U2.EN → 3V3)

SW2 (Boot):
  Pin 1 → U2.GPIO0
  Pin 2 → GND
  (R4 pull-up: U2.GPIO0 → 3V3)
```

---

## PCB Layout Guidelines

### Component Placement Order

1. **ESP32 Module (Center)**
   - Place ESP32-WROOM-32E in center of board
   - Leave 15mm clearance around antenna area (top-right corner)
   - No ground plane under antenna

2. **Power Section (Top Left)**
   - USB connector near edge
   - AMS1117 regulator close to USB
   - Decoupling capacitors within 5mm of regulator

3. **Level Shifter (Between ESP32 and AC Connector)**
   - TXB0104 close to ESP32 GPIO17/GPIO16
   - Positioned to minimize trace length to AC connector

4. **AC Connector (Right Edge)**
   - JST-XH connector on right edge
   - Easy access for cable connection

5. **USB-to-Serial (Bottom Left, Optional)**
   - CP2102N and USB connector
   - Separate from power USB

6. **Status LEDs (Top Edge)**
   - Visible when board is mounted
   - Near board edge

7. **Buttons (Accessible Location)**
   - Reset and Boot buttons
   - Easy to press during development

### Routing Priorities

1. **Power Traces (Highest Priority)**
   - 3V3: Minimum 0.5mm (20mil) width
   - 5V: Minimum 0.5mm (20mil) width
   - Use power planes if 4-layer board

2. **Ground (Critical)**
   - Ground plane on bottom layer
   - Connect all GND pins to plane
   - Keep ground continuous

3. **UART Signals (High Priority)**
   - Keep traces short (<50mm)
   - Avoid crossing power traces
   - Route together (differential pair style)
   - 0.2mm (8mil) minimum width

4. **GPIO Signals (Standard)**
   - 0.15mm (6mil) minimum width
   - Keep away from antenna area

### Design Rules

- **Trace Width**: 
  - Power: 0.5mm (20mil)
  - Signal: 0.15-0.2mm (6-8mil)
  
- **Via Size**: 
  - Diameter: 0.5mm (20mil)
  - Drill: 0.2mm (8mil)

- **Clearance**: 
  - Trace to trace: 0.15mm (6mil)
  - Trace to pad: 0.15mm (6mil)

- **Antenna Keepout**: 
  - 15mm radius around ESP32 antenna
  - No ground plane
  - No components
  - No traces (except necessary)

### Layer Stackup (2-Layer)

**Top Layer:**
- Components
- Signal traces
- Power traces (3V3, 5V)

**Bottom Layer:**
- Ground plane (primary)
- Power traces (if needed)
- Signal traces (minimal)

### Layer Stackup (4-Layer Recommended)

**Layer 1 (Top):**
- Components
- Signal traces

**Layer 2 (Inner 1):**
- Ground plane

**Layer 3 (Inner 2):**
- Power plane (3V3)

**Layer 4 (Bottom):**
- Ground plane
- Signal traces

---

## KiCad Specific Instructions

### 1. Adding Components

1. Open KiCad Schematic Editor
2. Click "Place Symbol" (A key)
3. Search for components:
   - `ESP32-WROOM-32E` (may need to download footprint)
   - `TXB0104` (may need custom symbol)
   - `AMS1117` (search in library)
   - Standard: `R`, `C`, `LED`, `SW_Push`, `Conn_01x04_Male`

### 2. Creating Custom Symbols (if needed)

**ESP32-WROOM-32E:**
- Create new symbol in Symbol Editor
- Add pins according to ESP32-WROOM-32E datasheet
- Save to custom library

**TXB0104:**
- Create symbol with 14 pins (TSSOP-14)
- Pins: VCCA, A1-A4, GND, OE, B1-B4, VCCB, GND

### 3. Assigning Footprints

1. Open "Assign Footprints" tool
2. Assign footprints:
   - ESP32-WROOM-32E → `ESP32-WROOM-32` footprint
   - TXB0104 → `TSSOP-14` footprint
   - AMS1117 → `SOT-223` or `SOT-89` footprint
   - Resistors → `R_0603` or `R_0805`
   - Capacitors → `C_0603` or `C_0805`
   - LEDs → `LED_0603`
   - Buttons → `SW_PUSH_6mm`
   - Connectors → Appropriate JST or USB footprint

### 4. Netlist and PCB

1. Generate Netlist (F8)
2. Open PCB Editor
3. Read Netlist (F8 in PCB Editor)
4. Place components according to guidelines
5. Route traces following priorities

### 5. Design Rule Check (DRC)

1. Run DRC before finalizing
2. Check for:
   - Unconnected nets
   - Clearance violations
   - Trace width violations
   - Via size issues

---

## Component Footprint Reference

| Component | Footprint | Package |
|-----------|-----------|---------|
| ESP32-WROOM-32E | Custom/ESP32-WROOM-32 | Module |
| TXB0104PWR | TSSOP-14 | TSSOP |
| AMS1117-3.3 | SOT-223 | SOT-223 |
| CP2102N | QFN-24 | QFN |
| Resistors | R_0603 or R_0805 | 0603/0805 |
| Capacitors | C_0603 or C_0805 | 0603/0805 |
| LEDs | LED_0603 | 0603 |
| Buttons | SW_PUSH_6mm | 6×6mm |
| USB Micro-B | USB_Micro-B | Through-hole |
| JST-XH | JST_XH_B4B-XH-A | Through-hole |

---

## Testing Checklist

After PCB assembly:

- [ ] Power supply: 3.3V stable at ESP32 VDD
- [ ] ESP32 boots (check serial output)
- [ ] WiFi connects
- [ ] BLE beacon transmits
- [ ] UART communication works
- [ ] Level shifter: 3.3V → 5V conversion verified
- [ ] Midea AC responds to commands
- [ ] Status LEDs function
- [ ] Buttons work (Reset, Boot)
- [ ] OTA updates work

---

## Common Issues and Solutions

### Issue: ESP32 doesn't boot
- **Check**: Power supply voltage (should be 3.3V)
- **Check**: Decoupling capacitors
- **Check**: EN pin connection

### Issue: UART communication fails
- **Check**: Level shifter connections
- **Check**: 5V power to level shifter VCCB
- **Check**: TX/RX not swapped

### Issue: WiFi/BLE interference
- **Check**: Antenna clearance (15mm)
- **Check**: No ground plane under antenna
- **Check**: Component placement

### Issue: Power regulator overheating
- **Check**: Adequate copper for heat dissipation
- **Check**: Thermal vias under regulator
- **Check**: Input voltage (should be 5V)

---

## Next Steps

1. **Create Schematic** following the connections above
2. **Assign Footprints** to all components
3. **Generate Netlist** and import to PCB
4. **Place Components** according to guidelines
5. **Route Traces** following priorities
6. **Run DRC** and fix any issues
7. **Generate Gerbers** for manufacturing
8. **Order Prototype** (start with 5-10 boards)

---

**Ready to start?** Open KiCad and begin with the power supply section, then add ESP32, level shifter, and connectors.

