diff --git a/docs/tutorials/building-a-simple-usb-flashlight.mdx b/docs/tutorials/building-a-simple-usb-flashlight.mdx index bdf3473..5e171eb 100644 --- a/docs/tutorials/building-a-simple-usb-flashlight.mdx +++ b/docs/tutorials/building-a-simple-usb-flashlight.mdx @@ -1,42 +1,326 @@ ---- -title: Building a Simple USB Flashlight -description: Learn how to build a simple USB-powered flashlight circuit using tscircuit with a push button, LED, and USB-C connector. ---- - -## Overview - -This tutorial will walk you through building a simple USB flashlight using -tscircuit. - -import TscircuitIframe from "@site/src/components/TscircuitIframe" - - { - return ( - - - .pos", pin2: "net.VBUS" }} - pcbX={0} - pcbY={-1} - /> - - - - - - - ) -} -`} /> +--- +title: Building a Simple USB Flashlight (Complete Guide) +description: Step-by-step tutorial to build a USB-powered flashlight circuit using tscircuit — components, schematic, PCB layout, and manufacturing. +--- + +## Overview + +This tutorial walks you through building a simple USB-powered flashlight using tscircuit. You'll learn how to design a circuit that takes 5V from a USB-C connector, uses a push button for on/off control, drives an LED through a current-limiting resistor, and lays it out as a manufacturable PCB. + +import TscircuitIframe from "@site/src/components/TscircuitIframe" + +## Components + +| Ref | Part | Description | Footprint | +|-----|------|-------------|-----------| +| J1 | USB-C Receptacle | USB Type-C connector for 5V power | `smd-usb-c` | +| SW1 | Tactile Push Button | Momentary SPST switch for on/off | `pushbutton` | +| LED1 | Red LED | 0603 surface-mount indicator LED | `0603` | +| R1 | 150Ω Resistor | Current limiting resistor for LED | `0603` | + +## Circuit Theory + +The circuit is straightforward: + +1. **USB-C Power**: The USB-C connector provides 5V on VBUS and a ground reference on GND +2. **Push Button**: Acts as a switch between VBUS and the LED circuit +3. **Current Limiting**: The 150Ω resistor limits LED current to approximately 20mA +4. **LED Indicator**: The red LED illuminates when the button is pressed + +### Current Calculation + +Using Ohm's Law: `I = (V_source - V_LED) / R` + +For a red LED with forward voltage ~2.0V: +`I = (5.0V - 2.0V) / 150Ω = 20mA` + +This is the typical operating current for a standard 0603 indicator LED. + +## Step 1: USB-C Connector + +Start with the USB-C receptacle. We use the community-sourced `@tsci/seveibar.smd-usb-c` component: + +```tsx +import { SmdUsbC } from "@tsci/seveibar.smd-usb-c" + + +``` + +Key points: +- Both GND pins are tied to the ground net +- Both VBUS pins are tied to the power net +- `pcbY={-10}` places it at the board edge for connector access +- `schX={-4}` positions it on the left side of the schematic + +## Step 2: Push Button Switch + +The push button connects between VBUS and the rest of the circuit: + +```tsx + .pos", + }} + pcbX={0} + pcbY={-1} +/> +``` + +When pressed, pin1 connects to pin2, completing the circuit from VBUS through the LED. + +## Step 3: LED and Current-Limiting Resistor + +```tsx + + + .pin2", + neg: ".LED1 > .pos", + }} +/> +``` + +The resistor is placed in series with the LED to limit current. Without it, the LED would draw excessive current and burn out. + +## Step 4: Ground Return Path + +```tsx + +``` + +The LED's cathode connects to ground, completing the circuit. + +## Step 5: Complete Circuit + +Here's the full circuit file: + +```tsx +import { SmdUsbC } from "@tsci/seveibar.smd-usb-c" + +export default () => { + return ( + + + .pos", + }} + pcbX={0} + pcbY={-1} + /> + .pin2", + neg: ".LED1 > .pos", + }} + /> + + + ) +} +``` + + { + return ( + + + .pos" }} + pcbX={0} + pcbY={-1} + /> + + + + + + + ) +} +`} /> + +## PCB Layout Considerations + +### Board Dimensions +- **Width**: 12mm — narrow enough to fit in a pen-sized housing +- **Height**: 30mm — provides enough space for all components with good spacing + +### Component Placement +- USB-C at the bottom edge (-10mm) for external access +- Push button in the middle for easy pressing +- LED at the top (12mm) as the light source +- Resistor between button and LED in the current path + +### Trace Routing +The tscircuit autorouter handles the connections, but you can also manually route traces: + +```tsx + + + +``` + +Use wider traces (0.5mm) for power connections and standard width (0.3mm) for signal lines. + +## Schematic View + +The schematic shows the circuit topology clearly: + +``` +USB-C VBUS ──┬── SW1 ── R1 ── LED1 ── GND + │ +USB-C GND ───┴──────────────────────── +``` + +- Current flows from VBUS through the button when pressed +- The resistor limits current to the LED +- The return path goes through ground + +## Building and Testing + +### Local Development + +```bash +# Create a new tscircuit project +mkdir usb-flashlight && cd usb-flashlight +tsci init + +# Save the circuit file +# (copy the circuit code into index.circuit.tsx) + +# Build the project +tsci build + +# View in browser +tsci preview +``` + +### Generate Manufacturing Files + +```bash +# Generate Gerber files for PCB fabrication +tsci build --gerber + +# Generate BOM (Bill of Materials) +tsci build --bom + +# Generate pick-and-place file +tsci build --pick-and-place +``` + +## Cost Estimate + +| Component | Unit Cost | Quantity | Total | +|-----------|-----------|----------|-------| +| USB-C Receptacle | $0.15 | 1 | $0.15 | +| Tactile Push Button | $0.05 | 1 | $0.05 | +| Red LED 0603 | $0.02 | 1 | $0.02 | +| Resistor 150Ω 0603 | $0.01 | 1 | $0.01 | +| PCB (2-layer) | $0.50 | 1 | $0.50 | +| **Total** | | | **$0.73** | + +At scale (1000+ units), the per-unit cost drops below $0.30. + +## Extensions + +Once you have the basic flashlight working, try these modifications: + +### Multiple LEDs +```tsx + + + + +``` + +### Different LED Colors +```tsx + + + +``` + +### Add a Capacitor for Persistence +Add a small capacitor to keep the LED lit briefly after releasing the button: + +```tsx + +``` + +## Summary + +You've built a complete USB-powered flashlight circuit using tscircuit, covering: +- Component selection and specification +- Circuit theory and current calculations +- Complete circuit implementation in TSX +- PCB layout with proper component placement +- Manufacturing file generation +- Cost analysis +- Extension ideas for more advanced versions + +This project demonstrates tscircuit's ability to handle everything from simple LED circuits to complex multi-layer PCB designs. diff --git a/docs/tutorials/esp32-pcb-layout-routing.mdx b/docs/tutorials/esp32-pcb-layout-routing.mdx new file mode 100644 index 0000000..bdab417 --- /dev/null +++ b/docs/tutorials/esp32-pcb-layout-routing.mdx @@ -0,0 +1,329 @@ +--- +title: ESP32 PCB Layout and Routing Tutorial +description: Learn how to design a complete ESP32 development board with tscircuit — component placement, trace routing, USB connectivity, and manufacturing. +--- + +## Overview + +This tutorial covers designing a complete ESP32 development board using tscircuit. You'll learn professional PCB layout techniques including component placement, power trace routing, USB differential pairs, crystal oscillator placement, and antenna keepout zones. + +import TscircuitIframe from "@site/src/components/TscircuitIframe" + +## Project Specifications + +| Parameter | Value | +|-----------|-------| +| Board Size | 30mm × 60mm | +| Layers | 2-layer (Top/Bottom) | +| Target | ESP32-WROOM-32 development board | +| USB | USB-C for programming and power | +| Voltage Regulator | 5V to 3.3V LDO (AMS1117-3.3) | +| Programming | Auto-reset via DTR/RTS from USB-UART | + +## Components + +| Ref | Part | Description | Footprint | +|-----|------|-------------|-----------| +| U1 | ESP32-WROOM-32 | Dual-core MCU with WiFi/BT | `esp32-wroom-32` | +| J1 | USB-C Receptacle | USB Type-C for power/programming | `smd-usb-c` | +| U2 | CH340G | USB-to-Serial bridge | `sop-16` | +| U3 | AMS1117-3.3 | 3.3V LDO regulator | `sot-223` | +| Y1 | 32.768 kHz Crystal | RTC clock oscillator | `3215_smd` | +| Y2 | 40 MHz Crystal | Main MCU oscillator | `2520_smd` | +| C1-C4 | Decoupling caps | 100nF ceramic capacitors | `0402` | +| R1-R6 | Pull-up/pull-down | 10kΩ resistors | `0402` | +| LED1 | Power indicator | Green status LED | `0603` | +| LED2 | TX indicator | Yellow activity LED | `0603` | +| SW1 | Reset button | Tactile push button | `pushbutton` | +| SW2 | Boot button | Tactile push button | `pushbutton` | + +## Step 1: Power Architecture + +The ESP32 requires a stable 3.3V supply. We use an AMS1117-3.3 LDO regulator: + +```tsx + + + + + + + + +``` + +### Decoupling Capacitors + +Each power pin on the ESP32 needs a 100nF decoupling capacitor placed as close as possible: + +```tsx + + + + +``` + +**Rule of thumb**: Place decoupling caps within 2mm of the power pin they're decoupling. + +## Step 2: ESP32 Module Placement + +The ESP32-WROOM-32 should be placed with the antenna overhanging the board edge for optimal WiFi performance: + +```tsx + +``` + +### Critical Placement rules: + +1. **Antenna keepout**: No copper pour within 15mm of the antenna area +2. **Ground plane**: Solid ground plane under the module +3. **Crystal proximity**: Place 40MHz crystal within 5mm of OSC pins +4. **Flash memory**: Keep high-speed signals away from the antenna side + +## Step 3: USB-C and CH340G Circuit + +The CH340G provides USB-to-serial conversion for programming: + +```tsx +import { SmdUsbC } from "@tsci/seveibar.smd-usb-c" + + .d-", + D2: ".U2 > .d+", + }} + pcbX={-25} + pcbY={0} +/> + + +``` + +### Auto-Reset Circuit + +The ESP32 needs an auto-reset circuit for programming. Connect CH340G DTR and RTS to ESP32 EN and GPIO0: + +```tsx + + + + + + + +``` + +## Step 4: Crystal Oscillators + +### 40 MHz Main Crystal + +```tsx + + + + + + + + + +``` + +### 32.768 kHz RTC Crystal + +```tsx + + + + +``` + +## Step 5: LED Indicators + +```tsx + + + + + + +``` + +## Step 6: Reset and Boot Buttons + +```tsx + + + + + + + + +``` + +## Complete Circuit + +```tsx +import { SmdUsbC } from "@tsci/seveibar.smd-usb-c" + +export default () => { + return ( + + {/* ESP32 Module */} + + + {/* USB-C Connector */} + + + {/* CH340G USB-UART */} + + + {/* AMS1117-3.3 Voltage Regulator */} + + + {/* Power LED */} + + + + {/* Reset Button */} + + + {/* Boot Button */} + + + {/* 40 MHz Crystal */} + + + {/* 32.768 kHz Crystal */} + + + {/* Decoupling Capacitors */} + + + + + + ) +} +``` + + { + return ( + + + + + + + + + + + + + ) +} +`} /> + +## PCB Layout Rules + +### Power Traces +- **5V input**: Minimum 0.5mm trace width (500mA current) +- **3.3V output**: Minimum 0.3mm trace width (ESP32 peak ~500mA) +- **GND plane**: Use a solid ground plane on the bottom layer + +### Signal Traces +- **UART (TX/RX)**: 0.2mm traces, keep short +- **SPI**: 0.2mm traces, controlled impedance not needed at ESP32 speeds +- **I2C**: 0.2mm traces with 4.7kΩ pull-ups + +### USB Differential Pair +- **D+/D-**: Route as differential pair with 90Ω impedance +- **Length matching**: Keep length difference under 5mm +- **Ground reference**: Solid ground plane underneath + +### Antenna Guidelines +- **Keepout zone**: 15mm no-copper zone around antenna +- **No vias**: No vias under or near the antenna +- **Ground plane**: Cut out ground plane in antenna area + +## Manufacturing Files + +```bash +# Generate Gerbers +tsci build --gerber + +# Generate BOM +tsci build --bom + +# Generate Assembly +tsci build --pick-and-place +``` + +## Cost Estimate + +| Component | Unit Cost | Qty | Total | +|-----------|-----------|-----|-------| +| ESP32-WROOM-32 | $2.50 | 1 | $2.50 | +| CH340G | $0.30 | 1 | $0.30 | +| AMS1117-3.3 | $0.15 | 1 | $0.15 | +| USB-C Receptacle | $0.15 | 1 | $0.15 | +| Crystals (×2) | $0.20 | 2 | $0.40 | +| Passives (×20) | $0.01 | 20 | $0.20 | +| PCB (2-layer) | $1.00 | 1 | $1.00 | +| **Total** | | | **$4.70** | + +## Summary + +You've designed a complete ESP32 development board covering: +- Power architecture with LDO regulation +- USB-C connectivity with CH340G programming +- Auto-reset circuit for seamless flashing +- Crystal oscillators for MCU timing +- PCB layout rules for WiFi performance +- Manufacturing file generation + +This board is ready for production and can be manufactured at any standard PCB fab house.