# Simplified Wiring Diagram ## The Key Insight: One Driver for Everything! 🎯 **You only need ONE LM18200 H-Bridge driver** - it handles both DC and DCC modes. The ESP32 just sends different signals to the same pins depending on which mode you select: ``` ESP32-2432S028R β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ GPIO 18 ──PWM / DCC_A │───┐ GPIO 19 ──DIR / DCC_B │──── GPIO 23 ──BRAKE │──── GPIO 4 ──RELAY │───┼──→ To Relay Module GPIO 35 ──ADC (ACK) │◄──┼──→ From ACS712 OUT GND ── │──── 5V ── │──── β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ ↓ LM18200 H-Bridge Module β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” GPIO 18 ──── PWM Input β”‚ GPIO 19 ──── DIR Input β”‚ GPIO 23 ──── BRAKE β”‚ GND ──── GND β”‚ 5V ──── VCC (logic) β”‚ 12-18V ──── VS (power) β”‚ β”‚ β”‚ β”‚ OUT1 OUT2 β”‚ β””β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”˜ β”‚ β”‚ ↓ ↓ ACS712 Current Sensor β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” OUT1 ──── IP+ β”‚ β”‚ β”‚ To Track ◄─── IP- OUT β”œβ”€β”€β†’ GPIO 35 (ADC) Rail 1 β”‚ β”‚ β”‚ VCC GND β”œβ”€β”€β†’ GND 5V ──── β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ ↓ Track Rail 1 β”‚ (Rail 2 from OUT2) ``` ## How It Works ### DC Analog Mode When you select **DC Analog** mode in the UI: - GPIO 18 outputs **20kHz PWM** (0-100% duty cycle for speed) - GPIO 19 outputs **HIGH or LOW** (sets direction: FWD or REV) - LM18200 amplifies this to create variable DC voltage on the track - Your DC locomotive responds to the voltage - ACS712 monitors current (optional - can display on screen) ### DCC Digital Mode When you select **DCC** mode in the UI: - GPIO 18 outputs **DCC Signal A** (square wave: 58ΞΌs or 100ΞΌs pulses) - GPIO 19 outputs **DCC Signal B** (inverted version of Signal A) - LM18200 amplifies these to create DCC waveform on the track - Your DCC decoder locomotive responds to the digital commands - ACS712 monitors current for normal operation ### DCC Programming Mode When you press **[PROG]** button in DCC mode: - GPIO 18/19 send **service mode packets** (22-bit preamble) - Decoder receives CV programming commands - Decoder responds with **60mA ACK pulse** for 6ms if command valid - ACS712 detects current spike and sends voltage to GPIO 35 - ESP32 reads ADC and confirms successful programming - UI shows "Verified!" or "Failed - No ACK" ## Complete Connection List ### LM18200 Module to ESP32 | LM18200 Pin | ESP32 GPIO | Purpose | |-------------|------------|---------| | PWM Input | 18 | Speed (DC) / DCC Signal A (DCC) | | Direction Input | 19 | Direction (DC) / DCC Signal B (DCC) | | Brake Input | 23 | Emergency stop | | GND | GND | Ground reference | | VCC (logic) | 5V | Control logic power | ### LM18200 Module Power & Outputs | LM18200 Pin | Connection | Purpose | |-------------|------------|---------| | VS (motor power) | 12-18V supply + | High current power | | GND (power) | 12-18V supply - | Power ground | | OUT1 | ACS712 IP+ | To current sensor | | OUT2 | Track Rail 2 | Direct to track | ### ACS712 Current Sensor Module | ACS712 Pin | Connection | Purpose | |------------|------------|---------| | IP+ | LM18200 OUT1 | Current input (from driver) | | IP- | Track Rail 1 | Current output (to track) | | VCC | 5V | Sensor power | | GND | GND | Ground reference | | OUT | GPIO 35 (ADC) | Analog current reading | **ACS712 Variants:** - **ACS712-05A**: Β±5A max (recommended for small locomotives) - **ACS712-20A**: Β±20A max (for larger locos or multiple) - **ACS712-30A**: Β±30A max (overkill, but works) **Output Voltage:** - At 0A: 2.5V (Vcc/2) - Sensitivity: - 5A model: 185 mV/A - 20A model: 100 mV/A - 30A model: 66 mV/A - ACK Detection (60mA): ~2.5V + (0.06A Γ— sensitivity) ### Relay Module (2-rail/3-rail switching) | Relay Pin | ESP32 GPIO | Purpose | |-----------|------------|---------| | Signal IN | 4 | Relay control | | VCC | 5V | Relay power | | GND | GND | Ground | ### Power Supply Connections ``` 12-18V Power Supply β”œβ”€β†’ LM18200 VS (motor power) β”œβ”€β†’ DC-DC Buck Converter β†’ 5V β†’ ESP32 + Relay + LM18200 VCC + ACS712 VCC └─→ GND (common ground for all modules) ``` ## ACS712 Current Sensor Details ### Why ACS712? βœ… **Hall-effect sensor** - Electrically isolated, no voltage drop βœ… **Analog output** - Easy to read with ESP32 ADC βœ… **Bi-directional** - Measures current in both directions βœ… **Module available** - Pre-built boards with 5V supply βœ… **ACK Detection** - Sensitive enough to detect 60mA programming pulses ### Wiring the ACS712 The ACS712 goes **in series** with ONE track rail: ``` LM18200 OUT1 ──→ [ACS712 IP+]──[IP-] ──→ Track Rail 1 β”‚ [OUT] ──→ GPIO 35 (ESP32 ADC) β”‚ [VCC] ──← 5V β”‚ [GND] ──← GND LM18200 OUT2 ──────────────────────────→ Track Rail 2 ``` ### Reading Current in Software The ACS712 outputs an analog voltage proportional to current: ```cpp // ACS712 5A model example #define ACS712_PIN 35 #define ACS712_SENSITIVITY 0.185 // 185 mV/A for 5A model #define ACS712_ZERO 2.5 // 2.5V at 0A (Vcc/2) float readCurrent() { int adcValue = analogRead(ACS712_PIN); float voltage = (adcValue / 4095.0) * 3.3; // Convert to voltage float current = (voltage - ACS712_ZERO) / ACS712_SENSITIVITY; return current; // Returns current in Amps } ``` ### ACK Detection with ACS712 For DCC programming track ACK (60mA pulse): ```cpp bool DCCGenerator::waitForAck() { #define CURRENT_SENSE_PIN 35 #define ACS712_ZERO_VOLTAGE 2.5 #define ACS712_SENSITIVITY 0.185 // For 5A model #define ACK_CURRENT_THRESHOLD 0.060 // 60mA in Amps unsigned long startTime = millis(); // Wait up to 20ms for ACK pulse while (millis() - startTime < 20) { int adcValue = analogRead(CURRENT_SENSE_PIN); float voltage = (adcValue / 4095.0) * 3.3; float current = abs((voltage - ACS712_ZERO_VOLTAGE) / ACS712_SENSITIVITY); // If current spike detected (60mA+) if (current > ACK_CURRENT_THRESHOLD) { Serial.println("ACK detected!"); return true; } delayMicroseconds(100); } Serial.println("No ACK"); return false; } ``` ### Calibration Before using ACK detection, calibrate the zero point: 1. **Power on** with no locomotive on track 2. **Read GPIO 35** multiple times and average 3. **Calculate zero voltage** (should be ~2.5V) 4. **Update ACS712_ZERO** in code if needed ```cpp // Calibration routine (run once) void calibrateCurrentSensor() { float sum = 0; for (int i = 0; i < 100; i++) { int adc = analogRead(35); float voltage = (adc / 4095.0) * 3.3; sum += voltage; delay(10); } float zeroVoltage = sum / 100.0; Serial.printf("ACS712 Zero Point: %.3fV\n", zeroVoltage); } ``` ## Why This Works The LM18200 is just an amplifier. It doesn't care if you're feeding it: - PWM signals (for DC speed control) - DCC square waves (for digital commands) It simply takes the 3.3V logic signals from the ESP32 and amplifies them to track voltage (12-18V). **In DC mode**: The amplified PWM creates variable DC voltage **In DCC mode**: The amplified square waves create the DCC signal **In Programming mode**: The ACS712 detects decoder ACK pulses ### Benefits of Using ACS712 βœ… **No voltage drop** - Hall-effect sensor doesn't load the circuit βœ… **Isolated measurement** - Safe for ESP32 ADC input βœ… **Both directions** - Works with forward/reverse current βœ… **Module form** - Easy to wire, includes filtering capacitors βœ… **DCC ACK capable** - Sensitive enough for 60mA detection βœ… **Current monitoring** - Can display real-time current on UI βœ… **Overcurrent detect** - Software can trigger emergency stop ## Safety Notes βœ… **Always power OFF before switching modes** (automatic in the UI) βœ… **Common ground** - All GND connections must be tied together βœ… **Heat sink** - LM18200 can get hot, use appropriate heat sinking βœ… **Fusing** - Add fuse on track output for overcurrent protection βœ… **ACS712 rating** - Use 5A model for most O-scale locos, 20A for larger βœ… **Short circuit** - Monitor current and auto-shutdown on excessive draw ## Shopping List ### Required Components - βœ… **ESP32-2432S028R** module (includes display + touch) - βœ… **LM18200 H-Bridge module** (or bare IC + heatsink) - βœ… **ACS712 current sensor module** (5A or 20A version) - βœ… **5V relay module** (for 2-rail/3-rail switching) - βœ… **12-18V power supply** (2A minimum) - βœ… **DC-DC buck converter** (12-18V to 5V, 1A) - βœ… **Wires** (22-24 AWG for logic, 18-20 AWG for track) - βœ… **Fuse holder** + appropriate fuse ### Optional but Recommended - πŸ”§ Heatsink for LM18200 (if using bare IC) - πŸ”§ Terminal blocks for easy track connections - πŸ”§ Emergency stop button (wired to GPIO 23 or power) - πŸ”§ Case/enclosure for professional finish ## No Separate DCC Booster Needed! You do **NOT** need: - ❌ Separate DCC booster circuit - ❌ Different outputs for DC vs DCC - ❌ Mode selection switches in hardware - ❌ Separate programming track booster - ❌ Complex current sensing circuits (ACS712 handles it!) Everything is handled in software by the ESP32 touchscreen UI. ## Connection Summary **Minimum wiring** for full functionality: 1. **ESP32 to LM18200**: 5 wires (GPIO 18, 19, 23, GND, 5V) 2. **LM18200 to ACS712**: 2 wires (OUT1 to IP+, IP- continues to track) 3. **ACS712 to ESP32**: 3 wires (OUT to GPIO 35, VCC to 5V, GND) 4. **ESP32 to Relay**: 3 wires (GPIO 4, 5V, GND) 5. **Power supply**: 12-18V to LM18200, 5V to all logic Total: **~16 connections** for a complete dual-mode test bench with programming! --- **Bottom Line**: Wire up ONE LM18200 + ONE ACS712, and you get: - βœ… DC analog speed control - βœ… DCC digital operation - βœ… DCC programming track with ACK verification - βœ… Real-time current monitoring - βœ… Overcurrent protection capability