Arduino Debugging and Testing

How to test your Arduino code ?

The testing of the software and hardware is carried out in several steps. During code programming, the programmer can check the syntax errors using the compiler directly in the Arduino IDE (using Verify button). However, this syntax error check does not give any indication of incorrect structuring or sequence of the program.

Testing the correct sequence of the code should be done directly on the board with external components connected. However, it’s a good idea to perform a hardware test for each component by writing small test programs for each external component individually.

By connecting all hardware parts and running the whole program for the first time, the first important test is done. Often the built solution does not run or runs partially faulty during the first tests. Now the debugging of the individual actions and functions of the program begins.

The display of individual variable values or signal states within the program sequence can be realized using the serial monitor by outputting the desired values to the serial interface. In the console the data are displayed during the program run and support the programmer in troubleshooting.

What is Arduino serial monitor ?

The serial monitor is a useful tool within the Arduino IDE and allows the display of the transmitted data via the serial port. When you open the Serial monitor, via the toolbar or Tools->Serial Monitor, a new window opens a new window without any content.

Serial Monitor

The serial monitor can now be used to receive data from the Arduino or to send data. To show data from the Arduino on the serial monitor, the Arduino Board must execute serial print instruction as shown in the example below.

void setup()
{
  // Initialize serial at baudrate of 9600
  Serial.begin(9600);  
}

void loop()
{
  Serial.println("Hallo world");
}

Serial.begin(9600); instruction initialize the serial transmission with a transmission rate (called baud rate) of 9600 bits per second. This should match the baud rate on the serial monitor window in order to see sent data.

char letter = '0';

void setup()
{
  // Initialize serial at baudrate of 9600
  Serial.begin(9600);
}
void loop() {
  if (Serial.available() > 0){
    letter = Serial.read();
    if(letter == 'A'){
      Serial.println("Letter is A");
    }
    else if(letter == 'B'){
      Serial.println("Letter is B");
    }
  }
  delay(500);
}

Serial.begin(9600); instruction initialize the serial transmission with a transmission rate (called baud rate) of 9600 bits per second. This should match the baud rate on the serial monitor window in order to see sent data.

To receive the data from the Arduino board, the instruction Serial.println( ); is used to print out what’s inside the brackets on the serial monitor. For sending data from the serial monitor, write a text in the input field and press the button Send or press Enter.

How debug Arduino code ?

Where programming is done, errors occur. You will notice this quickly when you first lines of code for your Arduino board. A missing semicolon or a forgotten declaration and the IDE complains when compiling the program code. With smaller projects, you usually found the error quickly and fixed it then recompile again. Such errors are called compile errors.

But an error does not necessarily mean a missing semicolon or a typo in the code. Another type of error is called runtime error, where your program compiled successfully but your project does not work as expected.

Possible causes can vary: In the program structure a function is not called, a wrong formula used in the calculations, or even a variable declared with a wrong data type.

The approach to look deeper into the code to find the error is called debugging. The first important step is to make sure that the external components like a sensor or limit switch are wired properly. Second, check that they deliver correct values or signals. This can be tested using small sketches to check that they are functioning correctly.

Another way is to inject debugging instruction inside the code. One example is to use Serial print to print out sensor values or Computation output on the serial monitor. This way you can see what and where is things going wrong.

#define DEBUG

#define SENSOR_PIN  A0  // Sensor analog input pin 
#define PWM_PIN       9      // PWM output pin

int valSensor= 0;        // variable for sensor value
int valPwm= 0;        // variable for PWM output value

void setup()
{
  // Initialize serial at baudrate of 9600
  Serial.begin(9600); 
  
  #ifdef DEBUG
    Serial.println("Debugging aktiviert");
  #endif

}

void loop()
{

   // read the sensor value
  valSensor= analogRead(SENSOR_PIN);
  // map to PWM range
  valPwm= map(valSensor, 0, 1023, 0, 255);
  // Update the PWM value:
  analogWrite(PWM_PIN, valPwm);

  #ifdef DEBUG
    // Print on the Serial Monitor:
    Serial.print("sensor value = ");
    Serial.print(valSensor);
    Serial.print("t PWM output value = ");
    Serial.println(valPwm);
  #endif


  // wait 500 milliseconds
  delay(500);
  
}

By defining DEBUG, the values can be checked on the serial monitor. Once done with debugging, undefine DEBGU (replace #define with #undef) and no output is sent to the serial port.

Digital states can be visualized not only by the output on the serial monitor but also using LEDs. For this purpose, connect an LED and series resistors on the required port of the Arduino board. Then the LED will turn-on or off according to the actual voltage on the ouput pin.

Read More

Arduino Functions

What are functions in Arduino

A function is a closed unit within a program and is called by its name. Usually with the function call still values are passed, which are processed then within the function. These values are called “parameters”. After execution of the function, which can be a calculation, a conversion or a signal output to an output port, this “small program” is executed and the main program, which called the function, is continued.

Often, however, the called function returns a response (called function return). This can be the result of a calculation or a status of a state.

Function Structure

The structure of the function is as follows:

Type FunctionName (type parameter){
//Function body
rerun val;
}

An example looks like this:

Function Structure

The type of a function indicates what kind of value is returned from the function. This can be int, char, etc. Accordingly, the data type of this return value is specified in the call of the function before the function name. The call of a function is usually made from the main program.

Function Example in Arduino

Program 1

Program 1 shows the call of the function sensorRead from the main program (loop). The return value is the analog value with the int data type. For a fun.ction that does not have a return value, the keyword void is used for the type designation.

Program 2

In Program 2, the loop calls the function sensorReadPrintSerial which has no return value and no input parameter. Consequently, this it calls another function printVal, which has an input parameter an no return.  

Read More