Ajout prise en charge ESP-Display

This commit is contained in:
Serge NOEL
2025-12-01 13:53:54 +01:00
parent bcd88909b7
commit ae375b8fe2
26 changed files with 3945 additions and 1017 deletions

385
WIRING.md
View File

@@ -1,107 +1,312 @@
# Wiring Diagram
# Simplified Wiring Diagram
## LM18200 H-Bridge Connection
## 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 D1 Mini LM18200 Track/Motor
GPIO 25 (PWM) ──────────────► PWM
GPIO 26 (DIR) ──────────────► DIR
GPIO 27 (BRAKE)──────────────► BRAKE
5V ──────────────► Vcc
GND ──────────────► GND
VS ◄───────── 12-18V Power Supply (+)
GND ◄───────── Power Supply GND
OUT1 ──────────► Track Rail 1
OUT2 ──────────► Track Rail 2
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)
```
## DCC Signal Output (Optional Booster Required)
## 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
```
ESP32 D1 Mini DCC Booster Track
GPIO 32 (DCC_A) ────────────► Signal A
GPIO 33 (DCC_B) ────────────► Signal B
Power In ◄──── 12-18V Supply
Track A ──────────► Rail 1
Track B ──────────► Rail 2
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)
```
## WS2812 LED Indicators
## 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:
```
ESP32 D1 Mini WS2812 LEDs
GPIO 4 (LED_DATA) ──────────► DIN
5V ──────────► VCC
GND ──────────► GND
LED 0: Power Status
- Green: Power ON
- Red: Power OFF
LED 1: Mode Indicator
- Blue (pulsing): DCC mode
- Yellow (pulsing): Analog mode
LM18200 OUT1 ──→ [ACS712 IP+]──[IP-] ──→ Track Rail 1
[OUT] ──→ GPIO 35 (ESP32 ADC)
[VCC] ──← 5V
[GND] ──← GND
LM18200 OUT2 ──────────────────────────→ Track Rail 2
```
## Complete System Diagram
### Reading Current in Software
```
┌─────────────────┐
│ Power Supply │
│ 12-18V DC │
└────────┬─────────┘
┌────────┴─────────┐
│ │
┌───────▼────────┐ ┌──────▼──────┐
│ LM18200 │ │ 5V Regulator│
│ H-Bridge │ │ (if needed) │
└───────┬────────┘ └──────┬───────┘
│ │
│ ┌────────▼────────┐
│ │ ESP32 D1 Mini │
│ │ │
│ │ GPIO 25 → PWM │───┐
│ │ GPIO 26 → DIR │───┤
│ │ GPIO 27 → BRAKE│───┤
│ │ │ │
│ │ GPIO 32 → DCC A│ │
│ │ GPIO 33 → DCC B│ │
│ │ │ │
│ │ GPIO 4 → LEDS │───┼──► WS2812 LEDs
│ │ │ │ (Power & Mode)
│ │ WiFi (Built-in)│ │
│ └─────────────────┘ │
│ │
└───────────────────────────────┘
┌───────▼────────┐
│ Track/Rails │
│ │
│ ┌──────────┐ │
│ │Locomotive│ │
│ └──────────┘ │
└────────────────┘
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
}
```
## Pin Summary Table
### ACK Detection with ACS712
| Function | ESP32 Pin | Device Pin | Notes |
|----------|-----------|------------|-------|
| Motor PWM | GPIO 25 | LM18200 PWM | 20kHz PWM signal |
| Motor Direction | GPIO 26 | LM18200 DIR | High=Forward, Low=Reverse |
| Motor Brake | GPIO 27 | LM18200 BRAKE | Active LOW |
| DCC Signal A | GPIO 32 | DCC Booster A | Requires booster circuit |
| DCC Signal B | GPIO 33 | DCC Booster B | Inverted signal |
| LED Data | GPIO 4 | WS2812 DIN | 2 LEDs for status |
| LED Power | 5V | WS2812 VCC | LED strip power |
| Power (5V) | 5V | LM18200 Vcc | Logic power |
| Ground | GND | LM18200/LEDs GND | Common ground |
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