Hardware-in-the-Loop Testing Strategies

Hardware-in-the-Loop Testing Strategies

Hardware-in-the-loop (HIL) testing is the practice of running firmware against real or simulated hardware while automating the stimulus and measurement. It sits between pure software simulation and fully manual hardware testing — and for many embedded projects, it is the only practical way to verify timing-sensitive behavior, sensor fusion algorithms, and actuator control loops.

This guide covers when to use HIL versus software-in-the-loop (SIL), how to build a minimal HIL bench, techniques for signal injection, and how to automate HIL test execution in CI.

HIL vs SIL: Choosing the Right Level

Software-in-the-loop (SIL) runs the firmware binary on a host machine, replacing all hardware access with software models. SIL is fast, cheap, and CI-friendly. It works well for testing algorithms, state machines, and communication protocol stacks where timing tolerances are loose. The weakness is fidelity: simulated ADC readings, fake GPIO states, and model-based sensors cannot reproduce the noise, latency, and non-linearity of real hardware.

Hardware-in-the-loop (HIL) keeps the real target microcontroller (or a representative one) in the loop and uses external test equipment or a dedicated HIL controller to stimulate inputs and measure outputs. The firmware runs on actual silicon with actual peripherals. This catches:

  • ADC saturation behavior under real noise conditions
  • PWM dead-time violations visible only at real switching frequencies
  • RTOS scheduling jitter that the simulation gets wrong
  • Brownout reset behavior during power transitions
  • CAN bus errors triggered by actual bus loading

A practical embedded test strategy uses both: SIL for logic and unit tests (fast, every commit), HIL for integration and validation tests (slower, every build, every nightly).

Building a Minimal HIL Test Bench

A minimal HIL bench consists of five elements:

  1. Target hardware — the actual PCB or development board running firmware under test
  2. Test controller — a second microcontroller or single-board computer (Raspberry Pi works well) that generates stimuli and reads back responses
  3. Signal conditioning — level shifters, current limiters, or analog signal generators to match real-world sensor outputs
  4. Instrumentation — logic analyzer, oscilloscope, or embedded debug probe (J-Link, OpenOCD) for ground truth measurements
  5. Test host — a laptop or CI runner that orchestrates the test controller and collects results

A practical bench using a Raspberry Pi as test controller:

[Test Host (CI Runner)]
        |
        | SSH / USB serial
        v
[Raspberry Pi (Test Controller)]
   GPIO -----> Target GPIO inputs
   GPIO <----- Target GPIO outputs
   UART -----> Target UART RX
   UART <----- Target UART TX
   I2C  <----> Simulated sensor (software-driven)
   ADC  <----- Target DAC output
        |
        v
[Target MCU running firmware under test]
        |
        v
[Actuators / LEDs / Motor driver (for output verification)]

The Raspberry Pi drives the GPIO pins that the firmware reads as sensor inputs, and it reads back the GPIO pins the firmware drives as actuator outputs. This lets you stimulate any input condition and assert any output state from a Python test script.

Signal Injection Patterns

Digital Signal Injection

For digital inputs (button presses, limit switches, digital sensor outputs):

import RPi.GPIO as GPIO
import time
import serial

GPIO.setmode(GPIO.BCM)
TARGET_INPUT_PIN = 17   # Pi output → Target input
TARGET_OUTPUT_PIN = 27  # Target output → Pi input

GPIO.setup(TARGET_INPUT_PIN, GPIO.OUT)
GPIO.setup(TARGET_OUTPUT_PIN, GPIO.IN)

def inject_button_press(duration_ms=50):
    """Simulate a button press with debounce-realistic timing."""
    GPIO.output(TARGET_INPUT_PIN, GPIO.HIGH)
    time.sleep(duration_ms / 1000.0)
    GPIO.output(TARGET_INPUT_PIN, GPIO.LOW)

def read_target_output():
    return GPIO.input(TARGET_OUTPUT_PIN)

Analog Signal Injection

For ADC inputs, a DAC (MCP4725 over I2C) can drive calibrated voltages:

from smbus2 import SMBus

DAC_ADDRESS = 0x62

def set_analog_voltage(bus: SMBus, voltage: float, vref: float = 3.3):
    """Set DAC output voltage (0 to vref)."""
    value = int((voltage / vref) * 4095)
    value = max(0, min(4095, value))
    high_byte = (value >> 4) & 0xFF
    low_byte  = (value << 4) & 0xFF
    bus.write_i2c_block_data(DAC_ADDRESS, 0x40, [high_byte, low_byte])

# Simulate a temperature sensor at 25°C (1.25V output from NTC circuit)
with SMBus(1) as bus:
    set_analog_voltage(bus, 1.25)
    time.sleep(0.1)  # Allow firmware ADC to sample
    # Now read back firmware's interpreted temperature via UART

UART Stimulus and Response

Many firmware builds expose a debug UART for test commands. A test harness can inject commands and parse responses:

import serial
import json

class FirmwareHarness:
    def __init__(self, port='/dev/ttyUSB0', baud=115200):
        self.ser = serial.Serial(port, baud, timeout=2.0)

    def send_command(self, cmd: str) -> dict:
        self.ser.write((cmd + '\n').encode())
        response = self.ser.readline().decode().strip()
        return json.loads(response)

    def get_sensor_reading(self) -> float:
        resp = self.send_command('READ_SENSOR')
        return resp['value']

    def trigger_actuator(self, channel: int, duty: int) -> bool:
        resp = self.send_command(f'SET_PWM {channel} {duty}')
        return resp['status'] == 'OK'

Writing Automated HIL Tests

Structure HIL tests like any other automated test suite — setup, action, assertion, teardown:

import pytest
import time

@pytest.fixture
def bench():
    h = FirmwareHarness('/dev/ttyUSB0')
    yield h
    h.ser.close()

class TestTemperatureController:

    def test_heater_activates_below_setpoint(self, bench, gpio_controller):
        # Inject cold temperature (0.5V → ~5°C equivalent)
        gpio_controller.set_analog_voltage(0.5)
        time.sleep(0.2)  # Firmware sample interval

        heater_state = gpio_controller.read_digital(HEATER_OUTPUT_PIN)
        assert heater_state == GPIO.HIGH, "Heater should be ON when below setpoint"

    def test_heater_deactivates_above_setpoint(self, bench, gpio_controller):
        # Inject hot temperature (2.8V → ~75°C equivalent)
        gpio_controller.set_analog_voltage(2.8)
        time.sleep(0.2)

        heater_state = gpio_controller.read_digital(HEATER_OUTPUT_PIN)
        assert heater_state == GPIO.LOW, "Heater should be OFF above setpoint"

    def test_pwm_duty_ramps_near_setpoint(self, bench, gpio_controller):
        # Inject temperature 2°C below setpoint — expect partial duty
        gpio_controller.set_analog_voltage(1.85)  # ~28°C with 30°C setpoint
        time.sleep(0.5)  # Allow PID loop to settle

        duty = bench.get_pwm_duty(channel=0)
        assert 10 < duty < 80, f"Expected partial PWM duty near setpoint, got {duty}%"

Resetting Target State Between Tests

Reliable HIL test suites require clean state between tests. Use the target's hardware reset line:

RESET_PIN = 22  # Pi GPIO → Target NRST

def reset_target():
    GPIO.output(RESET_PIN, GPIO.LOW)
    time.sleep(0.01)
    GPIO.output(RESET_PIN, GPIO.HIGH)
    time.sleep(0.5)  # Allow boot sequence to complete

@pytest.fixture(autouse=True)
def reset_between_tests():
    reset_target()
    yield
    # No teardown needed — next test will reset

Timing Verification in HIL

One of HIL's most valuable capabilities is measuring real timing. A logic analyzer or the test controller's timer can verify that firmware meets timing requirements:

import time

def measure_response_latency(gpio_controller) -> float:
    """Measure time from input stimulus to output response."""
    start = time.perf_counter()
    gpio_controller.inject_pulse(INPUT_PIN, duration_ms=1)

    # Poll for output response with timeout
    timeout = time.perf_counter() + 0.1  # 100ms timeout
    while time.perf_counter() < timeout:
        if gpio_controller.read_digital(OUTPUT_PIN) == GPIO.HIGH:
            elapsed = time.perf_counter() - start
            return elapsed * 1000  # Return milliseconds
    raise TimeoutError("No response within 100ms")

def test_interrupt_response_under_5ms(gpio_controller):
    latencies = [measure_response_latency(gpio_controller) for _ in range(100)]
    assert max(latencies) < 5.0, f"Worst-case latency {max(latencies):.2f}ms exceeds 5ms"
    assert sum(latencies)/len(latencies) < 2.0, "Average latency exceeds 2ms"

Running this test one hundred times catches scheduling jitter that a single measurement would miss.

Integrating HIL into CI

Self-hosted GitHub Actions runners attached to a HIL bench make automated HIL tests part of every build:

# .github/workflows/hil-tests.yml
name: HIL Integration Tests
on:
  push:
    branches: [main, develop]
jobs:
  hil:
    runs-on: [self-hosted, hil-bench]
    steps:
      - uses: actions/checkout@v3
      - name: Build firmware
        run: make firmware.bin
      - name: Flash target
        run: openocd -f target.cfg -c "program firmware.bin verify reset exit"
      - name: Run HIL tests
        run: pytest tests/hil/ -v --timeout=30
      - name: Upload test report
        uses: actions/upload-artifact@v3
        with:
          name: hil-results
          path: test-results.xml

The [self-hosted, hil-bench] label ensures the job runs only on the runner physically connected to the test bench.

Continuous Monitoring Beyond the Lab

HIL testing verifies firmware before it ships. Once devices are in the field, you need visibility into real-world behavior. HelpMeTest provides continuous endpoint monitoring for firmware that exposes network interfaces — whether that is an MQTT broker, a REST API on an embedded Linux device, or a cloud dashboard aggregating device telemetry. Set up monitors that check sensor readings, command latency, and OTA update status around the clock, and get alerted before customers notice a regression.

HIL testing is an investment, but the return is proportional: the cost of a bench that catches a motor control bug before production is a rounding error compared to a recall.

Read more

Start now free