11 KiB
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:
// 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):
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:
- Power on with no locomotive on track
- Read GPIO 35 multiple times and average
- Calculate zero voltage (should be ~2.5V)
- Update ACS712_ZERO in code if needed
// 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:
- ESP32 to LM18200: 5 wires (GPIO 18, 19, 23, GND, 5V)
- LM18200 to ACS712: 2 wires (OUT1 to IP+, IP- continues to track)
- ACS712 to ESP32: 3 wires (OUT to GPIO 35, VCC to 5V, GND)
- ESP32 to Relay: 3 wires (GPIO 4, 5V, GND)
- 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