# DCC-Central

DCC-Central consist in a complete system to drive DCC scale models. 

## Principles

System is based on ESP32-S3 WROOM devKit, with some peripherals :

- ILI9341 4.0" display module
  To display what is happening 
- Touch screen
  To allow direct managing
- MCT2515 can interface
  Used for accessory and feedback
- LM 18200 Hbridge (Drive track power / accessories )
    - DCC signals or DC - can be switched to program, 
    - second to send square signal - powers accessories
- ACS712 current sensor
  Read DCC power send to track
- RAILCOM detector  - input pin
- KY-040 rotary encoder
  set speed / enventually help to navigate in menus 
- relay
  Swith 2 Rails / 3 rails


## Functionnalities

DCC-Central is connected with CDM-Rail (protocol to choose), but can work alone. It will be RailCom compatible

On display we must have : 
- Select Autonomous / CDM-Rail driven switch
- Select DC / DCC type
- Select 2Rails / 3 Rails

If autonomous and DCC
- Select Programming mode
- Select Loco ID
- Display Functions buttons
- Add a tab to display informations returned


### Running

1. User choose a DCC-id to drive a loco with help of display
2. he selects type of signal DCC/DC (display)
3. track type ( 2rails / 3 rails)
4. Switch 'ON' track power

System response

- Switch relay ON (2 rails), OFF (3 rails)
- generate PWM (DC) / DCC signal (DCC)
- if DCC signal, set DCC loco id and drive DCC signal
- if DC signal, set DC (PWM) 
- Power ON LM 18200

## Wiring



## Wiring

Below is a general guide on how to wire the main elements of the DCC-Central system, with suggested ESP32 GPIO numbers:

- **ESP32-S3 WROOM devKit**: Serves as the main controller. All peripherals connect to its GPIO pins.

- **ILI9341 3.2" Display Module**: Connect the SPI pins to the ESP32 as follows (suggested):
    - MOSI: GPIO 23
    - MISO: GPIO 19
    - SCK:  GPIO 18
    - CS:   GPIO 5
    - DC:   GPIO 21
    - RESET: GPIO 22
  Power the display with 3.3V or 5V as required by the module.

- **Touch Screen**: If integrated with the display, connect the touch controller (often XPT2046) SPI pins as follows (suggested):
    - T_MOSI: GPIO 13
    - T_MISO: GPIO 12
    - T_SCK:  GPIO 14
    - T_CS:   GPIO 15
    - T_IRQ:  GPIO 27
  You may share SPI bus with the display if supported. Connect IRQ pin to a free GPIO.

- **MCP2515 CAN Interface**: Connect SPI pins as follows (suggested):
    - MOSI: GPIO 23 (shared with display)
    - MISO: GPIO 19 (shared with display)
    - SCK:  GPIO 18 (shared with display)
    - CS:   GPIO 4
    - INT:  GPIO 16
  Connect CANH and CANL to the CAN bus. Power with 3.3V or 5V as required.

- **LM18200 H-Bridge**: Connect control pins as follows (suggested):
    - IN1: GPIO 32
    - IN2: GPIO 33
    - PWM: GPIO 25
  Connect motor outputs to the track or accessories. Power the H-Bridge with the appropriate voltage for your track/accessories.

- **ACS712 Current Sensor**: Connect VCC and GND to power, OUT to ESP32 GPIO 36 (analog input, VP). Place the sensor in series with the track power line.

- **RailCom Detector**: Connect the detector output to ESP32 GPIO 26 (digital input). Follow the detector's datasheet for power and signal wiring.

- **KY-040 Rotary Encoder**: Connect as follows (suggested):
    - CLK: GPIO 34
    - DT:  GPIO 35
    - SW:  GPIO 17 (if used)
  Use pull-up resistors if required.

- **Relay**: Connect relay control pin to ESP32 GPIO 2. Use a transistor driver circuit if the relay requires more current than the ESP32 can provide. Connect relay contacts to switch between 2-rail and 3-rail track wiring.

### ESP32 Pin Usage Summary

| Peripheral                | Signal         | ESP32 GPIO |
|---------------------------|----------------|------------|
| ILI9341 Display           | MOSI           | 23         |
|                           | MISO           | 19         |
|                           | SCK            | 18         |
|                           | CS             | 5          |
|                           | DC             | 21         |
|                           | RESET          | 22         |
| Touch Screen (XPT2046)    | T_MOSI         | 13         |
|                           | T_MISO         | 12         |
|                           | T_SCK          | 14         |
|                           | T_CS           | 15         |
|                           | T_IRQ          | 27         |
| MCP2515 CAN Interface     | CS             | 4          |
|                           | INT            | 16         |
| LM18200 H-Bridge          | IN1            | 32         |
|                           | IN2            | 33         |
|                           | PWM            | 25         |
| ACS712 Current Sensor     | OUT            | 36 (VP)    |
| RailCom Detector          | OUT            | 26         |
| KY-040 Rotary Encoder     | CLK            | 34         |
|                           | DT             | 35         |
|                           | SW             | 17         |
| Relay                     | CTRL           | 2          |


**Tips:**
- Double-check voltage compatibility for each module.
- Use common ground for all modules.
- Keep power and signal wires as short as possible to reduce noise.
- Refer to each module's datasheet for exact pinouts and requirements.

**NB** a PCB card will be designed with KiCad Software for easier assembly.