Files
ESP32-S3-LCD-1.3/wiki/pages/patrol-gauge-outputs.md
2026-09-28 12:12:15 +02:00

133 lines
12 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
type: concept
tags: [gauges, pwm, ledc, nissan-patrol, hardware, design]
updated: 2026-09-21
---
# Driving the Patrol Cluster Gauges
Goal (user, 2026-09-21): generate signals from Inst. Cluster to drive the Nissan Patrol instrument cluster's **RPM (tachometer)** and **engine coolant temperature** gauges, fed by the BMW data that arrives over the [[controller-link]].
## Why B and not A
Main CTRL (5B) has no free native GPIO on any header: the RGB LCD, I2C, CAN, RS485, USB and PSRAM consume every pin, and its two "DO" terminals are optocoupled outputs driven through a CH422G I2C expander — no hardware timer can reach them `(sibling: 5B wiki → esp32-s3-touch-lcd-5b; schematic reviewed 2026-09-21)`. B exposes 13 free GPIOs on headers ([[esp32-s3-lcd-1-3]]), all usable by the ESP32-S3 LEDC peripheral.
## Two very different signals
| Gauge | What the cluster expects | What B must generate | Status |
|---|---|---|---|
| Tachometer | a pulse train whose **frequency** tracks engine speed — **4 pulses per revolution** on this cluster (user, 2026-09-21; not yet bench-verified) | variable-frequency square wave at ~50 % duty, on one LEDC channel with its **own timer** | source data (RPM from CAN 0x0AA) verified on A `(sibling: 5B wiki → can-id-candidates)`; **not yet in the packet** |
| Coolant temp | a moving-coil/bimetal gauge that reads the **resistance to ground** of the sender | fixed-frequency PWM with variable **duty** into a low-side switch, so the gauge integrates it as an average resistance | coolant already in the packet as 0.1 °C |
The two need separate LEDC timers because one varies frequency and the other varies duty. ESP32-S3 LEDC has 8 channels on 4 timers `(unverified — datasheet figure from memory, confirm)`.
## Pin assignment (implemented 2026-09-21)
- **IO7** → tachometer pulse output — LEDC channel 2 (timer 1), **14-bit** resolution so the divider reaches low frequencies (~4.8 Hz .. ~4.9 kHz; 8-bit could not even do 100 Hz), 50 % duty
- **IO8** → coolant gauge PWM output — LEDC channel 4 (timer 2), 8-bit duty at `temp_pwm_hz` (default 1 kHz)
- LEDC channels 0/1 (timer 0) belong to the display backlight; never reuse them `(repo)`
**Bench-verified 2026-09-21** `(verified)`: with A's default 89 °C coolant, B drives IO8 at duty 183/255 (1.5·89 + 50); `cfg override rpm 2000` on A's console produced 133.3 Hz on IO7 (2000 × 4 / 60), and clearing it dropped the output. Both outputs stay low with no link or no valid value. Still unverified: the **needle** — nothing is connected to the cluster yet and the external driver stage does not exist.
Both on the right header, adjacent to GND-free pins, so add a ground wire from the left header. IO3 deliberately avoided (strapping pin). IO1 stays the UART RX.
## Electrical interface — mandatory external stage (unverified)
3.3 V logic drives neither gauge directly.
- **Tach input**: Patrol clusters expect roughly 12 V-level pulses (ignition-coil negative or ECU tach output, open-collector with the cluster's internal pull-up). Use an NPN or logic-level N-MOSFET as an open-collector pulling the tach wire to ground; if the cluster has no internal pull-up, add one to +12 V (a few kΩ). Never feed 12 V back into IO7.
- **Temp gauge**: sender wire goes to ground through the sender's thermistor. Emulate it with a logic-level N-MOSFET (or NPN) pulling the sender wire to ground under PWM, plus a series resistor sized so 100 % duty corresponds to the hottest resistance you want to show. The gauge's thermal/mechanical lag averages the PWM; use a few hundred Hz to 1 kHz so nothing flickers. Calibration is a lookup table from °C → duty, built empirically against the needle.
- Optocouplers between B and the 12 V cluster side are cheap insurance; the 5B's own DO stage (PC817 → transistor) is a reasonable pattern to copy.
## Characterising the gauges (procedure, unverified until run)
The coolant gauge is a **current meter, not a pulse input** — there is no pulse count to find. The sender is a thermistor to ground; the needle follows the current through the gauge coil, which follows the sender's resistance. What has to be measured is the resistance → needle curve.
Coolant gauge, ignition on, engine off, sender disconnected:
1. 1 kΩ linear pot (or decade box, or fixed 47/100/220/470 Ω) from the sender wire to chassis ground. Sweep; record the resistance at the cold mark, middle, hot mark and start of the red zone (read the pot disconnected).
2. At each point also record the sender-wire voltage. Constant regardless of resistance → regulated supply, bimetal gauge. Proportional drop → plain series coil.
3. Needle lag of seconds → bimetal; near-instant → air core. Both average PWM; bimetal is more forgiving.
4. Series current at the hot-mark resistance = the peak the MOSFET stage must sink (expect tens of mA).
PWM drive from the curve:
- Logic-level N-MOSFET low side, drain to the sender wire through a fixed resistor equal to the hottest resistance ever to be shown; 100 % duty = hottest reading, the gauge integrates lower duties.
- 500 Hz – 2 kHz. Older Nissan clusters feed gauges from a chopped instrument voltage regulator at a few Hz `(unverified)`; PWM near that rate beats against it. Scope the sender wire once to see whether the chopper exists.
- Build the °C → duty lookup from where the needle actually lands at each duty step, not from any sender datasheet.
Tachometer, 4 ppr: f = rpm × 4 / 60 → ~47 Hz at 700 rpm, ~267 Hz at 4000 rpm. Before wiring: measure the tach input's open-circuit voltage with ignition on — battery voltage means the cluster has its own pull-up (open-collector output suffices); near zero means add a pull-up to 12 V. Verify the needle at two known LEDC frequencies before trusting the scaling.
## 2N2222 output stage — concrete wiring (2026-09-28, user's part choice; **verified on the cluster the same day**)
> [!note] Built and verified 2026-09-28: base resistors are **2.6 kΩ** (no 1 kΩ at hand) on both IO7 and IO8, and the instrument cluster responded. ~1 mA base current saturates the 2N2222 up to ~50 mA collector current, enough for both wires. Still to confirm: the transistor stays cool after minutes of running, and `cfg override duty 255` takes the temp needle to the hot mark; if not, drop to 1 kΩ.
One NPN per output, low-side open-collector switch. TO-92 P2N2222A / PN2222A with the flat face toward you: **E - B - C** left to right (metal-can 2N2222: the tab is next to the emitter). Check the datasheet of the exact part - the EBC order is not universal.
| Pin | Goes to | Why |
|---|---|---|
| Emitter | GND, shared by B, the transistor and the cluster (chassis) | the switch pulls the cluster wire toward this ground |
| Base | B's GPIO (IO7 tach, IO8 temp) through **1 kΩ** | 3.3 V pin → ~2.6 mA base current → saturated for anything up to ~100 mA collector; without it the pin drives a diode directly |
| Base | also **10 kΩ to GND** | keeps the transistor OFF while the ESP32 boots and the pin floats |
| Collector | the cluster wire (see below) | open collector: pulls low when the GPIO is high |
- **Tach (IO7)**: collector straight to the cluster's tach input. Meter the input first, ignition on: ~12 V = the cluster has its own pull-up, done; ~0 V = add **4.7 kΩ from the collector to +12 V**.
- **Temp (IO8)**: collector → **series resistor** → sender wire; the resistor equals the hottest sender resistance the needle should ever show (100 % duty = that value, the meter integrates lower duties). Add a **1N4148 from collector (cathode) to +12 V (anode)** — the gauge coil is inductive enough to spike the collector when the PWM switches; 40 V rating on the 2N2222 is otherwise fine.
- Never let the 12 V side reach a GPIO: the base resistor and the collector being the only 12 V-side node take care of that. No diode/resistor is needed on the GPIO itself.
- A logic-level N-MOSFET (e.g. IRLZ44N, AO3400) does the same job without base current: gate to GPIO through 100 Ω, 10 kΩ gate-to-GND, source to GND, drain as the collector above.
## Prior art: the user's CANNY 5.3 pico program (source: temp-stage-cfd)
The user drove this gauge before, from the BMW CAN bus, with a CannyLab program → [[temp-stage-cfd]]. Recovered from the file:
- Same decode the 5B later verified: CAN 0x1D0 byte 0, raw − 48 °C.
- **Duty = 1.5 × T + 50 on a 0–255 scale**, i.e. 110/255 at 40 °C, 200/255 at 100 °C, saturating near 137 °C. Duty *rises* with temperature (NTC sender emulation).
- Output only enabled above a minimum temperature (constant 0 in the file, 40 °C in the comment).
- Output was the controller's own push-pull pin with PWM pull-up enabled; the external circuit and the PWM frequency are **not** in the file.
Starting point for the LEDC implementation: 8-bit duty, `duty = clamp(1.5·T + 50, 0, 255)`, output disabled while the coolant value is invalid or the link is down. Verify against the needle before trusting it — the old drive circuit may differ from the new MOSFET stage, which shifts the whole curve.
## Tuning parameters — declared on A, executed on B (implemented 2026-09-21)
Calibration lives on Main CTRL (NVS; the **Gauge Tuning touchscreen page** — Home → right → Driveline → right — or the `cfg` serial console) and is pushed to B in CONFIG frames; B executes and caches the last config in its own NVS. `link_config_t` in the shared protocol header:
| Group | Parameter | Default | Note |
|---|---|---|---|
| Tach | pulses per rev | 4 | user figure |
| Tach | fine trim % | 0 | needle reads N % off at a known rpm |
| Tach | min / max output Hz | tbd | clamp against bad rpm values |
| Temp | PWM frequency Hz | **unknown** | from the CANNY measurement ([[temp-stage-cfd]]) |
| Temp | duty slope, offset (fixed-point) | 1.5, 50 | from the CANNY law |
| Temp | min temperature gate °C | 40 | output off below |
| Temp | duty clamp | 255 | |
| Both | output enable | on | per gauge |
| Both | **calibration override** | off | force a fixed rpm / fixed duty from A's screen and watch the needle |
Transport: CONFIG frames in protocol v2, sent on start, on every change (`cfg_version` bump) and every 5 s; overrides are pushed but never saved on either side. Console: `cfg show`, `cfg set <name> <value>`, `cfg override rpm <n> | duty <n> | both <rpm> <duty> | off`, `cfg reset` → [[controller-link]].
Measuring the CANNY's PWM without a scope: use a free 1.3 header GPIO with the ESP32 pulse-counter peripheral as a frequency/duty meter (resistor divider if the CANNY output exceeds 3.3 V). Same sketch later verifies B's own outputs. Planned as a separate PlatformIO environment in this repo.
## Calibration procedure (touchscreen or console)
Tachometer, with the driver stage on the tach wire:
1. Force 1000 rpm (override). The needle should read 1000.
2. A constant-factor error means the wrong pulses/rev — change it until 1000 reads 1000.
3. Force 3000 rpm; a small residual is the trim (%).
4. Override off.
Coolant gauge, with the MOSFET stage on the sender wire:
1. Force duty 110 (the CANNY program's 40 °C point) and note the needle; sweep 140 / 170 / 200 / 230.
2. From two marks (Tc at duty A, Th at duty B): slope = (B − A)/(Th − Tc), offset = A − slope × Tc; enter slope × 100 and offset.
3. Needle flicker or wander → raise PWM frequency. No response at all → driver stage / pull-up, not the numbers.
4. Override off. Values are saved on A and pushed to B at once.
## What must be established before any of this is wired
1. **Which Patrol cluster** (Y60/Y61, petrol vs diesel). User states **4 pulses/rev** (2026-09-21); confirm on the bench with the procedure above, since diesel clusters fed from an alternator W-terminal and petrol clusters fed from ignition differ `(unverified)`. This fixes the frequency range and therefore the LEDC resolution/prescaler. → [[open-questions]]
2. Whether the cluster's tach input has an internal pull-up, and to what voltage (meter it).
3. The temp sender's resistance-vs-temperature curve as the gauge sees it (meter the original sender at cold/hot, or sweep a pot until the needle sits at known marks). The CANNY-era duty law in [[temp-stage-cfd]] is the starting table; it still needs a needle check with the new driver stage, plus the old circuit's series resistor and PWM frequency.
4. Protocol change: add `engine_rpm` (u16, 1/min) and a `valid_flags` bit to the packet; bump the version on both repos → [[controller-link]].
5. The outage policy: what the needles do when the link is lost → [[link-redundancy]].
## Related
[[esp32-s3-lcd-1-3]] · [[controller-link]] · [[link-redundancy]] · [[open-questions]]