HIL testing in Arduino

I am trying out Hardware-in-the loop simulation on a new Arduino project for a client. Test driven development is unfortunately not very big in the Arduino community (yet) so I decided to implement something by myself. The setup is simple:

  1. One Arduino is running the application software.
  2. Another Arduino connects to inputs and outputs on the first Arduino. This Arduino will include all the test code.

2015-05-14 16.08.09

A test case could for example be, when the user presses a button a LED should light up. The second Arduino will output a signal to the button input on the first Arduino, and then check if the LED pin output is high. Example code is shown below.

void loop() {
  // Simulate that user presses button
  digitalWrite(BUTTON_PIN, 1);

  // check that the LED lights up
 assert(LED_PIN, 1);


  // check that some actuator starts running
  assert(ACTUATOR_PIN, 1);

  // Simulate that user releases button
 digitalWrite(BUTTON_PIN, 0);

  // Led and actuator should turn off
 assert(LED_PIN, 1);
 assert(ACTUATOR_PIN, 1);

  // stop execution
 while (1) {}

bool assert(uint8_t pin, bool expectedState) {
 bool state = digitalRead(pin);
  if (state != expectedState) {
   Serial.print("## FAILURE: pin ");
    Serial.print(" == ");
    Serial.print(" != ");
   return false;
 else {
    Serial.print("## OK: pin ");
    Serial.print(" == ");
   return true;


Why the hassle?

It might seem unnecessary, (and it is for simple problems), but it does increase code quality and decreases the risk of bugs being introduced when writing new features to the code.

Making it better

I would like to write my test code in Python on a laptop and control an Arduino (via Firmata for example). Then I would have proper tools for testing and generating test reports. For now the Arduino solution is sufficient though.

Taggad med:
Kategori: Arduino, Blog
2 Comments »HIL testing in Arduino
  1. Danee skriver:

    Hello Sebastian Nilsson,

    This article is so interesting for because I am an automotive professional.

    I have many years of experience in HIL Testing with dspace. I would like to know more about HIL testing in Arduino. have you published any paper on this or any other blog with more details

    • sebnil skriver:

      Sorry for really late reply. Missed your comment.

      No sorry, no paper just some playing around. But please if you have some links or some contribution to this field I would be quite interested as well.

      Hope you find what you are looking for,



E-postadressen publiceras inte. Obligatoriska fält är märkta *