Table of Contents
Test Arduino code
The testing of the software and hardware is carried out in several steps. During code programming, the you 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.
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.

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);
}
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.
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 activated");
#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.println(valSensor);
Serial.print("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 output pin.