The relay output module enables automatic control of 12V electrical relays based on sensor thresholds with hysteresis and manual override capability.
- Overview
- Features
- Hardware Setup
- Configuration
- Command Reference
- Use Cases
- Troubleshooting
- Technical Details
The relay control feature allows preOBD to automatically switch 12V relays ON/OFF based on sensor readings. This is useful for controlling:
- Electric cooling fans (turn on when temperature exceeds threshold)
- Water pumps (activate based on temperature or pressure)
- Warning lights or indicators
- Electric heaters (turn on when cold)
- Bypass valves or solenoids
- 2 independent relays with separate configurations
- Automatic control with hysteresis to prevent relay chattering
- Manual override for testing or emergency operation
- EEPROM persistence - configuration survives power cycles
- Safety features - relays default OFF, warmup protection
Relay turns ON when sensor value rises above threshold, OFF when it falls below.
Use case: Cooling fan
- Temperature rises → fan turns ON at 90°C
- Temperature falls → fan turns OFF at 85°C
- Hysteresis band (85-90°C) prevents rapid ON/OFF cycling
Temperature (°C)
↑
100 |
95 | ┌────────────
90 | ┌───────┘ ← Relay ON threshold
85 |════════════╝ ← Relay OFF threshold
80 |
└──────────────────────────────→ Time
OFF | ON | OFF
Relay turns ON when sensor value falls below threshold, OFF when it rises above.
Use case: Low oil pressure warning light
- Pressure drops → light ON at 0.5 bar
- Pressure rises → light OFF at 1.0 bar
- MANUAL_ON: Force relay ON (ignores sensor)
- MANUAL_OFF: Force relay OFF (ignores sensor)
- DISABLED: Relay completely disabled
- Safe Startup: All relays initialize to OFF state
- Warmup Protection: Relays stay OFF during sensor warmup period
- NaN Handling: Relay turns OFF if sensor value is invalid
- Disabled Sensor: Relay turns OFF if linked sensor is disabled
- EEPROM Persistence: Configuration survives power cycles and resets
Typical relay module (like common 5V/12V relay boards):
┌─────────────────────┐
│ Relay Module │
├─────────────────────┤
│ VCC ──→ 5V/12V │ Power supply (check your module)
│ GND ──→ GND │ Ground
│ IN ──→ GPIO Pin │ Control signal (e.g., pin 23)
│ │
│ COM ──┐ │ Common (to +12V battery)
│ NO ──┼──→ Load │ Normally Open (to fan, pump, etc.)
│ NC ──┘ │ Normally Closed (not used typically)
└─────────────────────┘
Components needed:
- 12V electric cooling fan
- 5V relay module (with optocoupler isolation)
- preOBD running on Teensy/Arduino
Wiring:
- Relay module power: VCC → 5V, GND → GND
- Control signal: IN → Teensy GPIO pin 23
- Fan power:
- Connect +12V battery → Relay COM terminal
- Connect Relay NO → Fan positive wire
- Connect Fan negative → Battery ground
Result: When relay activates, it completes the circuit powering the fan.
Available pins (platform-dependent):
- Teensy 4.1: Most digital pins (2-41)
- Arduino Mega: Digital pins (22-53 recommended to avoid conflicts)
- Avoid: Pins already used for sensors, CAN, I2C, SPI
Recommended pins for relays:
- Teensy 4.1: 23, 24, 25, 26 (not used by other peripherals)
- Arduino Mega: 22, 24, 26, 28 (avoid 50-53 for SPI)
This example configures a cooling fan to turn ON at 90°C and OFF at 85°C.
# 1. Configure coolant temperature sensor on pin A2
SET A2 APPLICATION COOLANT_TEMP
SET A2 SENSOR VDO_120C_STEINHART
ENABLE A2
# 2. Configure relay 0 for the fan
RELAY 0 PIN 23 # Control signal on GPIO 23
RELAY 0 INPUT A2 # Link to coolant temp sensor
RELAY 0 THRESHOLD 90 85 # ON at 90°C, OFF at 85°C
RELAY 0 MODE AUTO_HIGH # Automatic mode (high temp triggers)
# 3. Save configuration to EEPROM
SAVEDone! The fan will now automatically turn on when coolant reaches 90°C.
Before linking a relay, ensure the sensor is configured and enabled:
# Check available sensors
LIST SENSORS
# Configure sensor
SET A2 APPLICATION COOLANT_TEMP
SET A2 SENSOR VDO_120C_STEINHART
# Set alarm thresholds (optional, separate from relay thresholds)
SET A2 ALARM 50 110
# Enable sensor
ENABLE A2
# Verify sensor is reading
INFO A2# Set which GPIO pin controls the relay
RELAY 0 PIN 23Response: Relay 0 pin set to 23
# Connect relay to sensor input
RELAY 0 INPUT A2Response: Relay 0 linked to input on pin 54 (A2 = pin 54 on Teensy)
# Set ON and OFF thresholds
# Format: RELAY <index> THRESHOLD <on_value> <off_value>
RELAY 0 THRESHOLD 90 85For AUTO_HIGH mode (like cooling fan):
- First value (90) = Turn ON threshold
- Second value (85) = Turn OFF threshold
- OFF value should be < ON value
For AUTO_LOW mode (like low pressure warning):
- First value (0.5) = Turn ON threshold
- Second value (1.0) = Turn OFF threshold
- ON value should be < OFF value
# Enable automatic control
RELAY 0 MODE AUTO_HIGHAvailable modes:
AUTO_HIGH- Turn ON when value ≥ ON thresholdAUTO_LOW- Turn ON when value ≤ ON thresholdON- Force ON (manual override)OFF- Force OFF (manual override)
# Save to EEPROM (survives power cycles)
SAVEResponse: ✓ Configuration saved
Show status of all relays.
RELAY LISTOutput:
=================================
Relay 0
=================================
Output Pin: 23
Input: Pin 54 (COOL)
Current Value: 85.3 °C
Mode: AUTO_HIGH
Threshold ON: 90.00
Threshold OFF: 85.00
Current State: OFF
State Changes: 3
Last Change: 45 seconds ago
Show detailed status of a specific relay.
RELAY 0 STATUS
RELAY 1 STATUSAlias: Just typing RELAY 0 also shows status.
Set the GPIO pin for relay control signal.
RELAY 0 PIN 23
RELAY 1 PIN 24Parameters:
index: 0 or 1pin: GPIO pin number (e.g., 23, 24, 25)
Link relay to a sensor input.
RELAY 0 INPUT A2
RELAY 1 INPUT A3Parameters:
index: 0 or 1pin: Sensor pin (A0-A15, or digital pin)
Note: Sensor must be enabled first (ENABLE A2)
Set activation thresholds.
RELAY 0 THRESHOLD 90 85 # Cooling fan
RELAY 1 THRESHOLD 0.5 1.0 # Low pressure warningParameters:
index: 0 or 1on: Activation threshold (in sensor's current units)off: Deactivation threshold
Units: Thresholds use the sensor's configured units (°C, bar, PSI, etc.)
Set relay operating mode.
RELAY 0 MODE AUTO_HIGH # Automatic: ON when hot
RELAY 0 MODE AUTO_LOW # Automatic: ON when cold
RELAY 0 MODE ON # Manual: Force ON
RELAY 0 MODE OFF # Manual: Force OFFModes:
AUTO_HIGH: Automatic control, activates on high valuesAUTO_LOW: Automatic control, activates on low valuesON: Manual override, force relay ONOFF: Manual override, force relay OFF
Disable a relay completely.
RELAY 0 DISABLEEffect: Relay turns OFF and stops evaluating rules.
Scenario: Turn on cooling fan when coolant temperature exceeds 90°C.
Configuration:
SET A2 APPLICATION COOLANT_TEMP
SET A2 SENSOR VDO_120C_STEINHART
ENABLE A2
RELAY 0 PIN 23
RELAY 0 INPUT A2
RELAY 0 THRESHOLD 90 85
RELAY 0 MODE AUTO_HIGH
SAVEBehavior:
- Temperature < 85°C: Fan OFF
- Temperature 85-90°C: Fan maintains current state (hysteresis)
- Temperature ≥ 90°C: Fan ON
- When temperature drops ≤ 85°C: Fan OFF
Hysteresis benefit: Prevents fan from rapidly cycling ON/OFF if temperature hovers around 90°C.
Scenario: Turn on warning light when oil pressure drops below 0.5 bar.
Configuration:
SET A3 APPLICATION OIL_PRESSURE
SET A3 SENSOR VDO_5BAR_CURVE
ENABLE A3
RELAY 1 PIN 24
RELAY 1 INPUT A3
RELAY 1 THRESHOLD 0.5 1.0
RELAY 1 MODE AUTO_LOW
SAVEBehavior:
- Pressure > 1.0 bar: Light OFF
- Pressure 0.5-1.0 bar: Light maintains current state
- Pressure ≤ 0.5 bar: Light ON
- When pressure rises ≥ 1.0 bar: Light OFF
Scenario: Turn on electric water pump when coolant temperature exceeds 80°C.
Configuration:
SET A2 APPLICATION COOLANT_TEMP
ENABLE A2
RELAY 0 PIN 23
RELAY 0 INPUT A2
RELAY 0 THRESHOLD 80 75
RELAY 0 MODE AUTO_HIGH
SAVEBehavior:
- Pump turns ON at 80°C
- Pump turns OFF at 75°C
- 5°C hysteresis prevents pump cycling
Scenario: Test relay operation without automatic control.
Commands:
# Force relay ON
RELAY 0 MODE ON
# Wait and observe...
# (Check that fan/pump/light activates)
# Force relay OFF
RELAY 0 MODE OFF
# Return to automatic control
RELAY 0 MODE AUTO_HIGHUse case: Verify wiring, test relay module, check fan operation.
Scenario: Low speed fan at 85°C, high speed fan at 95°C.
Configuration:
# Low speed fan
RELAY 0 PIN 23
RELAY 0 INPUT A2
RELAY 0 THRESHOLD 85 80
RELAY 0 MODE AUTO_HIGH
# High speed fan
RELAY 1 PIN 24
RELAY 1 INPUT A2
RELAY 1 THRESHOLD 95 90
RELAY 1 MODE AUTO_HIGH
SAVEBehavior:
- < 80°C: Both fans OFF
- 80-85°C: Hysteresis for low speed
- ≥ 85°C: Low speed fan ON
- ≥ 95°C: Both fans ON (high speed)
Check 1: Verify relay configuration
RELAY 0 STATUS- Confirm
Output Pinis correct - Confirm
Inputis linked to correct sensor - Confirm
Modeis AUTO_HIGH or AUTO_LOW
Check 2: Verify sensor is reading
INFO A2- Confirm sensor is
Enabled - Confirm
Valueis not NaN - Confirm sensor is past warmup period
Check 3: Check threshold logic
RELAY 0 STATUS- For AUTO_HIGH:
Current Valuemust exceedThreshold ON - For AUTO_LOW:
Current Valuemust drop belowThreshold ON
Check 4: Test with manual override
RELAY 0 MODE ON- If relay clicks/activates → wiring is good, automatic logic issue
- If relay doesn't activate → wiring or hardware problem
Cause: Insufficient hysteresis (ON and OFF thresholds too close).
Solution: Increase hysteresis gap.
Example:
# Bad: 1°C hysteresis (may chatter)
RELAY 0 THRESHOLD 90 89
# Good: 5°C hysteresis (stable)
RELAY 0 THRESHOLD 90 85Recommended hysteresis:
- Temperature: 5-10°C
- Pressure: 0.2-0.5 bar
- Voltage: 0.5-1.0V
Check 1: Verify thresholds are correct
RELAY 0 STATUS- For AUTO_HIGH:
Threshold OFFshould be <Threshold ON - For AUTO_LOW:
Threshold OFFshould be >Threshold ON
Check 2: Verify sensor value
INFO A2- Confirm sensor value has actually dropped below OFF threshold
Check 3: Check if in manual mode
RELAY 0 STATUS- If
Mode: MANUAL_ON, return to auto:RELAY 0 MODE AUTO_HIGH
Cause: Configuration not saved to EEPROM.
Solution: Always run SAVE after configuration changes.
# After making changes:
SAVEVerification:
# Reboot system, then check:
RELAY 0 STATUSExpected behavior: Relays should stay OFF during sensor warmup.
If relay activates immediately:
- Check sensor warmup time:
INFO A2 ALARM - Increase warmup if needed:
SET A2 WARMUP 60000(60 seconds) - Save configuration:
SAVE
The relay module implements Schmitt trigger hysteresis to prevent chattering:
// AUTO_HIGH mode logic
if (!currentState && value >= thresholdOn) {
return true; // Turn ON
}
if (currentState && value <= thresholdOff) {
return false; // Turn OFF
}
return currentState; // Maintain state in hysteresis bandKey feature: Different ON and OFF thresholds create a "dead band" where the relay maintains its current state.
Per relay (16 bytes in EEPROM):
- Output pin: 1 byte
- Input index: 1 byte
- Mode: 1 byte
- Reserved: 1 byte
- Threshold ON: 4 bytes (float)
- Threshold OFF: 4 bytes (float)
- Reserved: 4 bytes
Total for 2 relays: 32 bytes
Runtime RAM (~24 bytes per relay):
- Current state: 1 byte (bool)
- Last state change: 4 bytes (uint32_t)
- State change count: 4 bytes (uint32_t)
The relay module is independent from the alarm system:
- Relays can have different thresholds than alarms
- Relay activation doesn't trigger alarms
- Alarms can trigger without relay activation
Example:
- Alarm threshold: 110°C (critical temperature)
- Relay threshold: 90°C (turn on cooling)
- Fan activates at 90°C, alarm triggers at 110°C
Relays are evaluated every 100ms (10 Hz) by default.
To change:
OUTPUT RELAY INTERVAL 50 # 50ms = 20 Hz (faster response)
OUTPUT RELAY INTERVAL 200 # 200ms = 5 Hz (slower, saves CPU)
SAVERecommendation: Keep default 100ms for most applications.
You can link multiple relays to the same sensor for staged control:
# Stage 1: Low speed fan at 85°C
RELAY 0 PIN 23
RELAY 0 INPUT A2
RELAY 0 THRESHOLD 85 80
RELAY 0 MODE AUTO_HIGH
# Stage 2: High speed fan at 95°C
RELAY 1 PIN 24
RELAY 1 INPUT A2
RELAY 1 THRESHOLD 95 90
RELAY 1 MODE AUTO_HIGHIf sensor is configured in Fahrenheit, thresholds use Fahrenheit:
SET A2 UNITS FAHRENHEIT
RELAY 0 INPUT A2
RELAY 0 THRESHOLD 190 180 # °F, not °CImportant: Thresholds always use the sensor's current unit setting.
You can use both alarms and relays:
# Alarm at critical temperature
SET A2 ALARM 50 110
SET A2 ALARM ENABLE
# Relay activates earlier for cooling
RELAY 0 INPUT A2
RELAY 0 THRESHOLD 90 85
RELAY 0 MODE AUTO_HIGHResult:
- 90°C: Fan turns ON (relay)
- 110°C: Alarm buzzer sounds (alarm system)
- Use isolated relay modules with optocouplers
- Fuse the load circuit appropriately for the fan/pump
- Check relay ratings (10A relays for high-current loads)
- Avoid back-EMF from inductive loads (use flyback diodes)
- Safe startup: Relays default to OFF on boot
- Warmup protection: Relays stay OFF during sensor warmup
- Fail-safe: Relay turns OFF if sensor fails (NaN)
- Manual override: Can force relay state for emergencies
- Test in manual mode first: Verify wiring with
RELAY 0 MODE ON - Use appropriate hysteresis: Prevent relay cycling (5-10°C recommended)
- Monitor state changes: Check
State Changescount for excessive switching - Save configuration: Always
SAVEafter changes - Document your setup: Note which relay controls which device
| Command | Purpose | Example |
|---|---|---|
RELAY LIST |
Show all relays | RELAY LIST |
RELAY <n> STATUS |
Show relay details | RELAY 0 STATUS |
RELAY <n> PIN <p> |
Set output pin | RELAY 0 PIN 23 |
RELAY <n> INPUT <p> |
Link to sensor | RELAY 0 INPUT A2 |
RELAY <n> THRESHOLD <on> <off> |
Set thresholds | RELAY 0 THRESHOLD 90 85 |
RELAY <n> MODE <m> |
Set mode | RELAY 0 MODE AUTO_HIGH |
RELAY <n> DISABLE |
Disable relay | RELAY 0 DISABLE |
SAVE |
Save to EEPROM | SAVE |
# 1. Configure sensor
SET A2 APPLICATION COOLANT_TEMP
ENABLE A2
# 2. Configure relay
RELAY 0 PIN 23
RELAY 0 INPUT A2
RELAY 0 THRESHOLD 90 85
RELAY 0 MODE AUTO_HIGH
# 3. Save
SAVE
# 4. Monitor
RELAY 0 STATUS- Main documentation: README.md
- Command reference: Type
HELPin serial console - Examples: Type
HELPand see Examples section - Source code: src/outputs/output_relay.cpp
Questions or issues? Report at https://github.com/preobd/preOBD/issues